Commit 09013306 authored by Shuhei Matsumoto's avatar Shuhei Matsumoto Committed by Changpeng Liu
Browse files

thread: Use not malloc'ed but fixed size string for thread name



256 bytes will be enough but not too large for the name of SPDK
thread. Use fixed size string for the name of SPDK thread and
reduce the potential malloc failure. If the length of passed name
is longer then 256, it will be cut off without error.

Change-Id: I13a24997a73a8365c8bf5e093f2bd78861ba6660
Signed-off-by: default avatarShuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/459720


Tested-by: default avatarSPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: default avatarChangpeng Liu <changpeng.liu@intel.com>
Reviewed-by: default avatarPaul Luse <paul.e.luse@intel.com>
Reviewed-by: default avatarDarek Stojaczyk <dariusz.stojaczyk@intel.com>
parent 404d2726
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@
#include "spdk_internal/thread.h"

#define SPDK_MSG_BATCH_SIZE		8
#define SPDK_MAX_THREAD_NAME_LEN	256

static pthread_mutex_t g_devlist_mutex = PTHREAD_MUTEX_INITIALIZER;

@@ -103,7 +104,7 @@ struct spdk_poller {
struct spdk_thread {
	TAILQ_HEAD(, spdk_io_channel)	io_channels;
	TAILQ_ENTRY(spdk_thread)	tailq;
	char				*name;
	char				name[SPDK_MAX_THREAD_NAME_LEN + 1];

	bool				exit;

@@ -227,7 +228,6 @@ _free_thread(struct spdk_thread *thread)
	pthread_mutex_unlock(&g_devlist_mutex);

	spdk_cpuset_free(thread->cpumask);
	free(thread->name);

	msg = SLIST_FIRST(&thread->msg_cache);
	while (msg != NULL) {
@@ -300,9 +300,9 @@ spdk_thread_create(const char *name, struct spdk_cpuset *cpumask)
	}

	if (name) {
		thread->name = strdup(name);
		snprintf(thread->name, sizeof(thread->name), "%s", name);
	} else {
		thread->name = spdk_sprintf_alloc("%p", thread);
		snprintf(thread->name, sizeof(thread->name), "%p", thread);
	}

	SPDK_DEBUGLOG(SPDK_LOG_THREAD, "Allocating new thread %s\n", thread->name);