From 33e1f4b0e2e0999cb81f8e2ae40ca4fce04f63d6 Mon Sep 17 00:00:00 2001 From: sunshihao520 Date: Fri, 6 Nov 2020 09:39:09 +0800 Subject: [PATCH] lib/thread: return NULL when id is invalid before search form tailq When call spdk_thread_get_by_id function to use thread->id to get thread point, if the id value is invaild, just return NULL before search from g_threads tailq. Signed-off-by: sunshihao Change-Id: Ic6c35d2c2b5093c9b513618742b8b9835599ba63 Reviewed-on: https://review.spdk.io/gerrit/c/spdk/spdk/+/5031 Tested-by: SPDK CI Jenkins Reviewed-by: Shuhei Matsumoto Reviewed-by: Changpeng Liu Reviewed-by: Aleksey Marchuk Reviewed-by: Jim Harris --- lib/thread/thread.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/thread/thread.c b/lib/thread/thread.c index dd80c4d6a..b523b130d 100644 --- a/lib/thread/thread.c +++ b/lib/thread/thread.c @@ -835,17 +835,18 @@ spdk_thread_get_by_id(uint64_t id) { struct spdk_thread *thread; + if (id == 0 || id >= g_thread_id) { + SPDK_ERRLOG("invalid thread id: %" PRIu64 ".\n", id); + return NULL; + } pthread_mutex_lock(&g_devlist_mutex); TAILQ_FOREACH(thread, &g_threads, tailq) { if (thread->id == id) { - pthread_mutex_unlock(&g_devlist_mutex); - - return thread; + break; } } pthread_mutex_unlock(&g_devlist_mutex); - - return NULL; + return thread; } int