From 9017f70561a854d1ea019290a63d8a0fb1841b39 Mon Sep 17 00:00:00 2001 From: zhangyuan21 Date: Fri, 10 Feb 2023 14:41:36 +0800 Subject: [PATCH] sched/pthread: check pthread group after find joininfo Need check tcb or group info after find joininfo, otherwise the wrong value will be returned. Signed-off-by: zhangyuan21 --- sched/pthread/pthread_findjoininfo.c | 3 ++- sched/pthread/pthread_join.c | 12 +++--------- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/sched/pthread/pthread_findjoininfo.c b/sched/pthread/pthread_findjoininfo.c index cf9101ef5e..72eb111559 100644 --- a/sched/pthread/pthread_findjoininfo.c +++ b/sched/pthread/pthread_findjoininfo.c @@ -141,7 +141,8 @@ FAR struct join_s *pthread_findjoininfo(FAR struct task_group_s *group, FAR struct tcb_s *tcb = nxsched_get_tcb((pthread_t)pid); if (tcb != NULL && (tcb->flags & TCB_FLAG_DETACHED) == 0 && - (tcb->flags & TCB_FLAG_TTYPE_MASK) == TCB_FLAG_TTYPE_PTHREAD) + (tcb->flags & TCB_FLAG_TTYPE_MASK) == TCB_FLAG_TTYPE_PTHREAD && + tcb->group == group) { pjoin = pthread_createjoininfo((FAR struct pthread_tcb_s *)tcb); } diff --git a/sched/pthread/pthread_join.c b/sched/pthread/pthread_join.c index b864b64e75..398e35100d 100644 --- a/sched/pthread/pthread_join.c +++ b/sched/pthread/pthread_join.c @@ -75,7 +75,6 @@ int pthread_join(pthread_t thread, FAR pthread_addr_t *pexit_value) FAR struct tcb_s *rtcb = this_task(); FAR struct task_group_s *group = rtcb->group; FAR struct join_s *pjoin; - FAR struct tcb_s *tcb; int ret; sinfo("thread=%d group=%p\n", thread, group); @@ -85,18 +84,11 @@ int pthread_join(pthread_t thread, FAR pthread_addr_t *pexit_value) enter_cancellation_point(); - tcb = nxsched_get_tcb(thread); - if (tcb != NULL && tcb->group != group) - { - leave_cancellation_point(); - return EINVAL; - } - /* First make sure that this is not an attempt to join to * ourself. */ - if (tcb == rtcb) + if ((pid_t)thread == gettid()) { leave_cancellation_point(); return EDEADLK; @@ -122,6 +114,8 @@ int pthread_join(pthread_t thread, FAR pthread_addr_t *pexit_value) { /* Determine what kind of error to return */ + FAR struct tcb_s *tcb = nxsched_get_tcb((pthread_t)thread); + swarn("WARNING: Could not find thread data\n"); /* Case (1) or (3) -- we can't tell which. Assume (3) */