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 3dcef3666144708a84a58d20def5ea84bf109e8c
Author: makejian <[email protected]>
AuthorDate: Wed Nov 25 13:09:52 2026 +0800

    signal: fix nxsig_clockwait misra coverity
    
    Keep one exit path in nxsig_clockwait(): hold the result in a local
    variable for the timeout and cancellation checks instead of returning
    from the middle of the function, and nest the wait body in the final
    else branch, as the MISRA C:2012 guidelines require.
    
    On master the function lives in sig_clockwait.c, which was split out
    of sig_timedwait.c, and wakes with its own siginfo buffer; that logic
    is unchanged, only the exit paths are restructured.
    
    Signed-off-by: makejian <[email protected]>
    Signed-off-by: zhangyuan29 <[email protected]>
---
 sched/signal/sig_clockwait.c | 231 ++++++++++++++++++++++---------------------
 1 file changed, 120 insertions(+), 111 deletions(-)

diff --git a/sched/signal/sig_clockwait.c b/sched/signal/sig_clockwait.c
index 2e688064b00..f7c9b03db52 100644
--- a/sched/signal/sig_clockwait.c
+++ b/sched/signal/sig_clockwait.c
@@ -195,6 +195,7 @@ int nxsig_clockwait(int clockid, int flags,
   irqstate_t        iflags;
   clock_t expect = 0u;
   clock_t stop   = 0u;
+  int ret = OK;
 #ifndef CONFIG_DISABLE_ALL_SIGNALS
   siginfo_t unbinfo;
   sigset_t intersection;
@@ -204,7 +205,7 @@ int nxsig_clockwait(int clockid, int flags,
 
   if (rqtp && (rqtp->tv_nsec < 0 || rqtp->tv_nsec >= 1000000000))
     {
-      return -EINVAL;
+      ret = -EINVAL;
     }
 
   /* If rqtp is zero, yield CPU and return
@@ -219,10 +220,10 @@ int nxsig_clockwait(int clockid, int flags,
    *    openbsd-compat/bsd-misc.c#L243
    */
 
-  if (rqtp && rqtp->tv_sec == 0 && rqtp->tv_nsec == 0)
+  else if (rqtp && rqtp->tv_sec == 0 && rqtp->tv_nsec == 0)
     {
       sched_yield();
-      return -EAGAIN;
+      ret = -EAGAIN;
     }
 
 #ifdef CONFIG_CANCELLATION_POINTS
@@ -231,159 +232,167 @@ int nxsig_clockwait(int clockid, int flags,
    * must exit immediately without waiting.
    */
 
-  if (check_cancellation_point())
+  else if (check_cancellation_point())
     {
       /* If there is a pending cancellation, then do not perform
        * the wait.  Exit now with ECANCELED.
        */
 
-      return -ECANCELED;
+      ret = -ECANCELED;
     }
 #endif
 
-  iflags = enter_critical_section();
-  rtcb = this_task();
+  else
+    {
+      iflags = enter_critical_section();
+      rtcb = this_task();
 
 #ifndef CONFIG_DISABLE_ALL_SIGNALS
-  /* If the caller did not install its own sigunbinfo (only nxsig_timedwait()
-   * does), install ours so that we can tell how we were awakened: a real
-   * signal writes its siginfo here and requires -EINTR.
-   */
-
-  if (rtcb->sigunbinfo == NULL)
-    {
-      rtcb->sigunbinfo = &unbinfo;
-      own_unbinfo = true;
-    }
+      /* If the caller did not install its own sigunbinfo (only
+       * nxsig_timedwait() does), install ours so that we can tell how we
+       * were awakened: a real signal writes its siginfo here and requires
+       * -EINTR.
+       */
 
-  /* nxsig_timedwait() publishes sigwaitmask before calling this function.
-   * A matching signal can be queued after that publication but before this
-   * task transitions to TSTATE_WAIT_SIG. Re-check the pending set here while
-   * interrupts are disabled so we do not sleep with a waited-for signal
-   * already pending.
-   */
+      if (rtcb->sigunbinfo == NULL)
+        {
+          rtcb->sigunbinfo = &unbinfo;
+          own_unbinfo = true;
+        }
 
-  if (!own_unbinfo)
-    {
-      /* Loop because the pending-set snapshot may be stale:
-       * nxsig_pendingset() releases tg_lock before
-       * nxsig_remove_pendingsignal() re-acquires it.  On SMP, another CPU
-       * may consume the signal in that window, causing remove() to return
-       * NULL even though the snapshot was non-empty.  Re-read and retry.
-       * Bounded because pending signals are finite.
+      /* nxsig_timedwait() publishes sigwaitmask before calling this
+       * function.  A matching signal can be queued after that publication
+       * but before this task transitions to TSTATE_WAIT_SIG. Re-check the
+       * pending set here while interrupts are disabled so we do not sleep
+       * with a waited-for signal already pending.
        */
 
-      intersection = nxsig_pendingset(rtcb);
-      sigandset(&intersection, &intersection, &rtcb->sigwaitmask);
-      while (!sigisemptyset(&intersection))
+      if (!own_unbinfo)
         {
-          sigpend = nxsig_remove_pendingsignal(rtcb,
-                                               nxsig_lowest(&intersection));
-          if (sigpend == NULL)
+          /* Loop because the pending-set snapshot may be stale:
+           * nxsig_pendingset() releases tg_lock before
+           * nxsig_remove_pendingsignal() re-acquires it.  On SMP, another
+           * CPU may consume the signal in that window, causing remove()
+           * to return NULL even though the snapshot was non-empty.
+           * Re-read and retry.  Bounded because pending signals are
+           * finite.
+           */
+
+          intersection = nxsig_pendingset(rtcb);
+          sigandset(&intersection, &intersection, &rtcb->sigwaitmask);
+          while (!sigisemptyset(&intersection))
             {
-              /* Snapshot was stale on SMP: another CPU consumed the signal
-               * between pendingset() and remove().  Re-read and retry.
-               */
-
-              intersection = nxsig_pendingset(rtcb);
-              sigandset(&intersection, &intersection, &rtcb->sigwaitmask);
-              continue;
+              sigpend =
+                nxsig_remove_pendingsignal(rtcb,
+                                           nxsig_lowest(&intersection));
+              if (sigpend == NULL)
+                {
+                  /* Snapshot was stale on SMP: another CPU consumed the
+                   * signal between pendingset() and remove().  Re-read
+                   * and retry.
+                   */
+
+                  intersection = nxsig_pendingset(rtcb);
+                  sigandset(&intersection, &intersection,
+                            &rtcb->sigwaitmask);
+                  continue;
+                }
+
+              memcpy(rtcb->sigunbinfo, &sigpend->info, sizeof(siginfo_t));
+              sigemptyset(&rtcb->sigwaitmask);
+              nxsig_release_pendingsignal(sigpend);
+
+              leave_critical_section(iflags);
+              return 0;
             }
-
-          memcpy(rtcb->sigunbinfo, &sigpend->info, sizeof(siginfo_t));
-          sigemptyset(&rtcb->sigwaitmask);
-          nxsig_release_pendingsignal(sigpend);
-
-          leave_critical_section(iflags);
-          return 0;
         }
-    }
 #endif
 
-  if (rqtp)
-    {
-      /* Start the watchdog timer */
-
-      if ((flags & TIMER_ABSTIME) == 0)
-        {
-          expect = clock_delay2abstick(clock_time2ticks(rqtp));
-        }
-      else if (clockid == CLOCK_REALTIME)
+      if (rqtp)
         {
+          /* Start the watchdog timer */
+
+          if ((flags & TIMER_ABSTIME) == 0)
+            {
+              expect = clock_delay2abstick(clock_time2ticks(rqtp));
+            }
+          else if (clockid == CLOCK_REALTIME)
+            {
 #ifdef CONFIG_CLOCK_TIMEKEEPING
-          clock_t delay;
+              clock_t delay;
 
-          clock_abstime2ticks(CLOCK_REALTIME, rqtp, &delay);
-          expect = clock_delay2abstick(delay);
+              clock_abstime2ticks(CLOCK_REALTIME, rqtp, &delay);
+              expect = clock_delay2abstick(delay);
 #else
-          clock_realtime2absticks(rqtp, &expect);
+              clock_realtime2absticks(rqtp, &expect);
 #endif
-        }
-      else
-        {
-          expect = clock_time2ticks(rqtp);
-        }
+            }
+          else
+            {
+              expect = clock_time2ticks(rqtp);
+            }
 
-      wd_start_abstick(&rtcb->waitdog, expect,
-                       nxsig_timeout, (uintptr_t)rtcb);
-    }
+          wd_start_abstick(&rtcb->waitdog, expect,
+                           nxsig_timeout, (uintptr_t)rtcb);
+        }
 
-  /* Remove the tcb task from the ready-to-run list. */
+      /* Remove the tcb task from the ready-to-run list. */
 
-  nxsched_remove_self(rtcb);
+      nxsched_remove_self(rtcb);
 
-  /* Add the task to the specified blocked task list */
+      /* Add the task to the specified blocked task list */
 
-  rtcb->task_state = TSTATE_WAIT_SIG;
-  dq_addlast((FAR dq_entry_t *)rtcb, list_waitingforsignal());
+      rtcb->task_state = TSTATE_WAIT_SIG;
+      dq_addlast((FAR dq_entry_t *)rtcb, list_waitingforsignal());
 
-  /* Now, perform the context switch if one is needed */
+      /* Now, perform the context switch if one is needed */
 
-  up_switch_context(this_task(), rtcb);
+      up_switch_context(this_task(), rtcb);
 
-  /* We no longer need the watchdog */
+      /* We no longer need the watchdog */
 
-  if (rqtp)
-    {
-      stop = clock_systime_ticks();
-    }
+      if (rqtp)
+        {
+          stop = clock_systime_ticks();
+        }
 
-  /* Retrieve the wake reason before dropping the critical section */
+      /* Retrieve the wake reason before dropping the critical section */
 
 #ifndef CONFIG_DISABLE_ALL_SIGNALS
-  if (own_unbinfo)
-    {
-      rtcb->sigunbinfo = NULL;
-    }
+      if (own_unbinfo)
+        {
+          rtcb->sigunbinfo = NULL;
+        }
 #endif
 
-  leave_critical_section(iflags);
-
-  if (rqtp && rmtp && expect)
-    {
-      clock_ticks2time(rmtp,
-                       clock_compare(stop, expect) ? expect - stop : 0);
-    }
+      leave_critical_section(iflags);
 
-#ifndef CONFIG_DISABLE_ALL_SIGNALS
-  if (own_unbinfo)
-    {
-      /* Awakened by a real signal?  Then the wait must fail with EINTR
-       * (POSIX: nanosleep/clock_nanosleep).  A pending cancellation is
-       * reported through the siginfo errno instead.
-       */
-
-      if (GOOD_SIGNO(unbinfo.si_signo))
+      if (rqtp && rmtp && expect)
         {
-          return -EINTR;
+          clock_ticks2time(rmtp,
+                           clock_compare(stop, expect) ? expect - stop : 0);
         }
 
-      if (unbinfo.si_signo == SIG_CANCEL_TIMEOUT)
+#ifndef CONFIG_DISABLE_ALL_SIGNALS
+      if (own_unbinfo)
         {
-          return -unbinfo.si_errno;
+          /* Awakened by a real signal?  Then the wait must fail with EINTR
+           * (POSIX: nanosleep/clock_nanosleep).  A pending cancellation is
+           * reported through the siginfo errno instead.
+           */
+
+          if (GOOD_SIGNO(unbinfo.si_signo))
+            {
+              ret = -EINTR;
+            }
+          else if (unbinfo.si_signo == SIG_CANCEL_TIMEOUT)
+            {
+              ret = -unbinfo.si_errno;
+            }
         }
-    }
 #endif
+    }
 
-  return 0;
+  return ret;
 }
+

Reply via email to