From 09013306c3083c4eb7a90c0e0092c944e589e845 Mon Sep 17 00:00:00 2001 From: Shuhei Matsumoto Date: Fri, 28 Jun 2019 14:26:00 +0900 Subject: [PATCH] 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: Shuhei Matsumoto Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/459720 Tested-by: SPDK CI Jenkins Reviewed-by: Changpeng Liu Reviewed-by: Paul Luse Reviewed-by: Darek Stojaczyk --- lib/thread/thread.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/thread/thread.c b/lib/thread/thread.c index 471417ce1..e01ab00c3 100644 --- a/lib/thread/thread.c +++ b/lib/thread/thread.c @@ -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);