From 3458527a2ced40c8d7c28871b16b641ddbcf3a2e Mon Sep 17 00:00:00 2001 From: Christopher Friedt Date: Fri, 22 Dec 2023 18:07:17 -0500 Subject: [PATCH] posix: pthread_getspecific: fix for coverity issue cid 334909 * remove unneeded line of code that duplicated the first part of the SYS_SLIST_FOR_EACH_NODE() expansion. * return NULL if pthread_self() is not a valid pthread Signed-off-by: Christopher Friedt --- lib/posix/key.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/posix/key.c b/lib/posix/key.c index df9062362ed..611db537212 100644 --- a/lib/posix/key.c +++ b/lib/posix/key.c @@ -248,12 +248,17 @@ out: void *pthread_getspecific(pthread_key_t key) { pthread_key_obj *key_obj; - struct posix_thread *thread = to_posix_thread(pthread_self()); + struct posix_thread *thread; pthread_thread_data *thread_spec_data; void *value = NULL; sys_snode_t *node_l; k_spinlock_key_t key_key; + thread = to_posix_thread(pthread_self()); + if (thread == NULL) { + return NULL; + } + key_key = k_spin_lock(&pthread_key_lock); key_obj = get_posix_key(key); @@ -262,8 +267,6 @@ void *pthread_getspecific(pthread_key_t key) return NULL; } - node_l = sys_slist_peek_head(&(thread->key_list)); - /* Traverse the list of keys set by the thread, looking for key */ SYS_SLIST_FOR_EACH_NODE(&(thread->key_list), node_l) {