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

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

Signed-off-by: Shuhei Matsumoto <shuhei.matsumoto.xt@hitachi.com>
Change-Id: I618b82a1d07769df7c775280fbf364cbcfdde403
Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/459721
Tested-by: SPDK CI Jenkins <sys_sgci@intel.com>
Reviewed-by: Changpeng Liu <changpeng.liu@intel.com>
Reviewed-by: Seth Howell <seth.howell5141@gmail.com>
Reviewed-by: Darek Stojaczyk <dariusz.stojaczyk@intel.com>
Reviewed-by: Paul Luse <paul.e.luse@intel.com>
This commit is contained in:
Shuhei Matsumoto 2019-06-28 14:40:27 +09:00 committed by Changpeng Liu
parent 09013306c3
commit 3d1995c35b

View File

@ -43,6 +43,7 @@
#include "spdk_internal/thread.h" #include "spdk_internal/thread.h"
#define SPDK_MSG_BATCH_SIZE 8 #define SPDK_MSG_BATCH_SIZE 8
#define SPDK_MAX_DEVICE_NAME_LEN 256
#define SPDK_MAX_THREAD_NAME_LEN 256 #define SPDK_MAX_THREAD_NAME_LEN 256
static pthread_mutex_t g_devlist_mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t g_devlist_mutex = PTHREAD_MUTEX_INITIALIZER;
@ -52,7 +53,7 @@ static size_t g_ctx_sz = 0;
struct io_device { struct io_device {
void *io_device; void *io_device;
char *name; char name[SPDK_MAX_DEVICE_NAME_LEN + 1];
spdk_io_channel_create_cb create_cb; spdk_io_channel_create_cb create_cb;
spdk_io_channel_destroy_cb destroy_cb; spdk_io_channel_destroy_cb destroy_cb;
spdk_io_device_unregister_cb unregister_cb; spdk_io_device_unregister_cb unregister_cb;
@ -865,9 +866,9 @@ spdk_io_device_register(void *io_device, spdk_io_channel_create_cb create_cb,
dev->io_device = io_device; dev->io_device = io_device;
if (name) { if (name) {
dev->name = strdup(name); snprintf(dev->name, sizeof(dev->name), "%s", name);
} else { } else {
dev->name = spdk_sprintf_alloc("%p", dev); snprintf(dev->name, sizeof(dev->name), "%p", dev);
} }
dev->create_cb = create_cb; dev->create_cb = create_cb;
dev->destroy_cb = destroy_cb; dev->destroy_cb = destroy_cb;
@ -885,7 +886,6 @@ spdk_io_device_register(void *io_device, spdk_io_channel_create_cb create_cb,
if (tmp->io_device == io_device) { if (tmp->io_device == io_device) {
SPDK_ERRLOG("io_device %p already registered (old:%s new:%s)\n", SPDK_ERRLOG("io_device %p already registered (old:%s new:%s)\n",
io_device, tmp->name, dev->name); io_device, tmp->name, dev->name);
free(dev->name);
free(dev); free(dev);
pthread_mutex_unlock(&g_devlist_mutex); pthread_mutex_unlock(&g_devlist_mutex);
return; return;
@ -904,7 +904,6 @@ _finish_unregister(void *arg)
dev->name, dev->io_device, dev->unregister_thread->name); dev->name, dev->io_device, dev->unregister_thread->name);
dev->unregister_cb(dev->io_device); dev->unregister_cb(dev->io_device);
free(dev->name);
free(dev); free(dev);
} }
@ -912,7 +911,6 @@ static void
_spdk_io_device_free(struct io_device *dev) _spdk_io_device_free(struct io_device *dev)
{ {
if (dev->unregister_cb == NULL) { if (dev->unregister_cb == NULL) {
free(dev->name);
free(dev); free(dev);
} else { } else {
assert(dev->unregister_thread != NULL); assert(dev->unregister_thread != NULL);