This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 0bc013bb2b4e7603b9229ca0c6c32a4a1331b19b Author: ganjing <[email protected]> AuthorDate: Mon Nov 17 21:11:37 2025 +0800 libc/pthread: Reject keys that were not created. pthread_key_delete() and pthread_setspecific() only checked that the key was in range. They returned success for a key that was never created or was already deleted, and pthread_setspecific() then stored a value in a free slot. Return EINVAL for such a key, as POSIX allows. A key that is out of range also returns EINVAL instead of failing an assertion. Assisted-by: Kiro:auto Signed-off-by: ganjing <[email protected]> --- libs/libc/pthread/pthread_keydelete.c | 15 ++++++++++++--- libs/libc/pthread/pthread_setspecific.c | 15 ++++++++------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/libs/libc/pthread/pthread_keydelete.c b/libs/libc/pthread/pthread_keydelete.c index 805c943801f..e74ca83a71b 100644 --- a/libs/libc/pthread/pthread_keydelete.c +++ b/libs/libc/pthread/pthread_keydelete.c @@ -64,7 +64,7 @@ int pthread_key_delete(pthread_key_t key) int ret = EINVAL; DEBUGASSERT(info != NULL && tls != NULL); - DEBUGASSERT(key >= 0 && key < CONFIG_TLS_NELEM); + if (key >= 0 && key < CONFIG_TLS_NELEM) { /* This is done while holding a semaphore here to avoid concurrent @@ -74,8 +74,17 @@ int pthread_key_delete(pthread_key_t key) ret = nxrmutex_lock(&info->ta_lock); if (ret == OK) { - info->ta_tlsdtor[key] = NULL; - tls->tl_elem[key] = 0; + if (info->ta_tlsdtor[key] != NULL) + { + info->ta_tlsdtor[key] = NULL; + tls->tl_elem[key] = 0; + ret = OK; + } + else + { + ret = EINVAL; + } + nxrmutex_unlock(&info->ta_lock); } else diff --git a/libs/libc/pthread/pthread_setspecific.c b/libs/libc/pthread/pthread_setspecific.c index 3b4939e1228..3262972e60e 100644 --- a/libs/libc/pthread/pthread_setspecific.c +++ b/libs/libc/pthread/pthread_setspecific.c @@ -75,19 +75,20 @@ int pthread_setspecific(pthread_key_t key, FAR const void *value) { - FAR struct tls_info_s *info; + FAR struct task_info_s *info = task_get_info(); + FAR struct tls_info_s *tls; - DEBUGASSERT(key >= 0 && key < CONFIG_TLS_NELEM); - if (key >= 0 && key < CONFIG_TLS_NELEM) + if (key >= 0 && key < CONFIG_TLS_NELEM && + info->ta_tlsdtor[key] != NULL) { /* Get the TLS info structure from the current threads stack */ - info = tls_get_info(); - DEBUGASSERT(info != NULL); + tls = tls_get_info(); + DEBUGASSERT(tls != NULL); - /* Set the element value int the TLS info. */ + /* Set the element value in the TLS info. */ - info->tl_elem[key] = (uintptr_t)value; + tls->tl_elem[key] = (uintptr_t)value; return OK; }
