This is an automated email from the ASF dual-hosted git repository. acassis pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 5fca12c277c7b9054dcf9315490141b87cc27fa3 Author: hujun5 <[email protected]> AuthorDate: Wed Jan 28 18:41:19 2026 +0800 sched_waitid.c: HIS_metric_violation(HIS_GOTO) Replace all goto statements with structured control flow using while loops and conditional blocks. Consolidate early returns into a single error-handling path for better code structure and MISRA HIS standards compliance. Signed-off-by: hujun5 <[email protected]> --- sched/sched/sched_waitid.c | 190 +++++++++++++++++++++------------------------ 1 file changed, 87 insertions(+), 103 deletions(-) diff --git a/sched/sched/sched_waitid.c b/sched/sched/sched_waitid.c index 72ee9f38e93..530f2537f7e 100644 --- a/sched/sched/sched_waitid.c +++ b/sched/sched/sched_waitid.c @@ -156,9 +156,15 @@ int waitid(idtype_t idtype, id_t id, FAR siginfo_t *info, int options) #endif irqstate_t flags; sigset_t set; - int errcode; + int errcode = ENOSYS; int ret; + /* waitid() is a cancellation point */ + + enter_cancellation_point(); + + flags = enter_critical_section(); + /* MISSING LOGIC: If WNOHANG is provided in the options, then this * function should returned immediately. However, there is no mechanism * available now know if the thread has child: The children remember @@ -169,118 +175,91 @@ int waitid(idtype_t idtype, id_t id, FAR siginfo_t *info, int options) #ifdef CONFIG_DEBUG_FEATURES /* Only ID types P_PID and P_ALL are supported */ - if (idtype != P_PID && idtype != P_ALL) - { - set_errno(ENOSYS); - return ERROR; - } - - /* None of the options are supported except for WEXITED (which must be - * provided. Currently SIGCHLD always reports CLD_EXITED so we cannot - * distinguish any other events. - */ - - if ((options & WEXITED) == 0) - { - set_errno(ENOSYS); - return ERROR; - } - - if ((options & ~(WEXITED | WNOHANG)) != 0) - { - set_errno(ENOSYS); - return ERROR; - } + if ((idtype == P_PID || idtype == P_ALL) && + (options & WEXITED) != 0 && + (options & ~(WEXITED | WNOHANG)) == 0) #endif + { + /* Create a signal set that contains only SIGCHLD */ - /* waitid() is a cancellation point */ - - enter_cancellation_point(); - - /* Create a signal set that contains only SIGCHLD */ - - sigemptyset(&set); - nxsig_addset(&set, SIGCHLD); - flags = enter_critical_section(); + sigemptyset(&set); + nxsig_addset(&set, SIGCHLD); + errcode = OK; - /* Verify that this task actually has children and that the requested - * TCB is actually a child of this task. - */ + /* Verify that this task actually has children and that the requested + * TCB is actually a child of this task. + */ #ifdef CONFIG_SCHED_CHILD_STATUS - /* Does this task retain child status? */ + /* Does this task retain child status? */ - retains = ((rtcb->group->tg_flags & GROUP_FLAG_NOCLDWAIT) == 0); + retains = ((rtcb->group->tg_flags & GROUP_FLAG_NOCLDWAIT) == 0); - if (rtcb->group->tg_children == NULL && retains) - { - /* There are no children */ - - errcode = ECHILD; - goto errout; - } - else if (idtype == P_PID) - { - /* Get the TCB corresponding to this PID and make sure that the - * thread it is our child. - */ + if (rtcb->group->tg_children == NULL && retains) + { + /* There are no children */ - ctcb = nxsched_get_tcb((pid_t)id); - if (ctcb && ctcb->group) + errcode = ECHILD; + } + else if (idtype == P_PID) { - /* Make sure that the thread it is our child. */ + /* Get the TCB corresponding to this PID and make sure that the + * thread it is our child. + */ - if (ctcb->group->tg_ppid != rtcb->pid) + ctcb = nxsched_get_tcb((pid_t)id); + if (ctcb && ctcb->group) { - errcode = ECHILD; - goto errout; - } - } + /* Make sure that the thread it is our child. */ - /* Does this task retain child status? */ + if (ctcb->group->tg_ppid != rtcb->pid) + { + errcode = ECHILD; + } + } - if (retains) - { - /* Check if this specific pid has allocated child status? */ + /* Does this task retain child status? */ - if (group_find_child(rtcb->group, (pid_t)id) == NULL) + if (retains && errcode == OK) { - /* This specific pid is not a child */ + /* Check if this specific pid has allocated child status? */ - errcode = ECHILD; - goto errout; + if (group_find_child(rtcb->group, (pid_t)id) == NULL) + { + /* This specific pid is not a child */ + + errcode = ECHILD; + } } } - } #else - /* Child status is not retained. */ + /* Child status is not retained. */ - if (rtcb->group->tg_nchildren == 0) - { - /* There are no children */ + if (rtcb->group->tg_nchildren == 0) + { + /* There are no children */ - errcode = ECHILD; - goto errout; - } - else if (idtype == P_PID) - { - /* Get the TCB corresponding to this PID and make sure that the - * thread is our child. - */ + errcode = ECHILD; + } + else if (idtype == P_PID) + { + /* Get the TCB corresponding to this PID and make sure that the + * thread is our child. + */ - ctcb = nxsched_get_tcb((pid_t)id); + ctcb = nxsched_get_tcb((pid_t)id); - if (!ctcb || !ctcb->group || ctcb->group->tg_ppid != rtcb->pid) - { - errcode = ECHILD; - goto errout; + if (!ctcb || !ctcb->group || ctcb->group->tg_ppid != rtcb->pid) + { + errcode = ECHILD; + } } - } #endif + } /* Loop until the child that we are waiting for dies */ - for (; ; ) + while (errcode == OK) { #ifdef CONFIG_SCHED_CHILD_STATUS /* Check if the task has already died. Signals are not queued in @@ -345,7 +324,7 @@ int waitid(idtype_t idtype, id_t id, FAR siginfo_t *info, int options) */ errcode = ECHILD; - goto errout; + break; } } #else @@ -364,7 +343,7 @@ int waitid(idtype_t idtype, id_t id, FAR siginfo_t *info, int options) */ errcode = ECHILD; - goto errout; + break; } #endif @@ -390,7 +369,7 @@ int waitid(idtype_t idtype, id_t id, FAR siginfo_t *info, int options) if (ret < 0) { errcode = -ret; - goto errout; + break; } /* Make there this was SIGCHLD */ @@ -431,16 +410,16 @@ int waitid(idtype_t idtype, id_t id, FAR siginfo_t *info, int options) /* Return success */ #ifdef CONFIG_SCHED_CHILD_STATUS - if (retains) - { - child = group_find_child(rtcb->group, info->si_pid); + if (retains) + { + child = group_find_child(rtcb->group, info->si_pid); - if (child && - (child->ch_flags & CHILD_FLAG_EXITED) != 0) - { - exited_child(rtcb, child, NULL); - } + if (child && + (child->ch_flags & CHILD_FLAG_EXITED) != 0) + { + exited_child(rtcb, child, NULL); } + } #endif break; @@ -451,20 +430,25 @@ int waitid(idtype_t idtype, id_t id, FAR siginfo_t *info, int options) else /* if (idtype == P_PGID) */ { errcode = ENOSYS; - goto errout; + break; } } } leave_critical_section(flags); - leave_cancellation_point(); - return OK; -errout: - leave_critical_section(flags); leave_cancellation_point(); - set_errno(errcode); - return ERROR; + if (errcode != OK) + { + ret = ERROR; + set_errno(errcode); + } + else + { + ret = OK; + } + + return ret; } #endif /* CONFIG_SCHED_WAITPID && CONFIG_SCHED_HAVE_PARENT */
