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 e046ec2db1a7e3cbe93587774bc31db2072a0ba5
Author: ouyangxiangzhen <[email protected]>
AuthorDate: Fri Nov 27 15:30:24 2026 +0800

    sched/clock: extract clock_timespec_validate helper
    
    Extract timespec validation from timer_settime into clock.h as
    clock_timespec_validate, and reuse it in sem_clockwait.
    
    This also fixes a bug in nxsem_clockwait_slow where a negative
    abstime->tv_sec (e.g. -1) was not rejected, since only tv_nsec
    was validated before.
    
    Signed-off-by: ouyangxiangzhen <[email protected]>
---
 include/nuttx/clock.h                     | 24 ++++++++++++++++++++++++
 libs/libc/pthread/pthread_condclockwait.c |  3 +--
 sched/clock/clock_settime.c               |  2 +-
 sched/mqueue/mq_receive.c                 |  2 +-
 sched/mqueue/mq_send.c                    |  2 +-
 sched/semaphore/sem_clockwait.c           |  2 +-
 sched/signal/sig_clockwait.c              |  2 +-
 7 files changed, 30 insertions(+), 7 deletions(-)

diff --git a/include/nuttx/clock.h b/include/nuttx/clock.h
index 896fdd37611..dcf574b6a92 100644
--- a/include/nuttx/clock.h
+++ b/include/nuttx/clock.h
@@ -34,6 +34,8 @@
 #include <stdint.h>
 #include <time.h>
 
+#include <stdbool.h>
+
 #include <nuttx/compiler.h>
 #include <nuttx/lib/math32.h>
 
@@ -455,6 +457,28 @@ extern "C"
    ((ts1)->tv_sec > (ts2)->tv_sec) ? 1 : \
    (ts1)->tv_nsec - (ts2)->tv_nsec)
 
+/****************************************************************************
+ * Name: clock_timespec_validate
+ *
+ * Description:
+ *   Validate that a timespec has legal values.
+ *
+ * Input Parameters:
+ *   ts - Pointer to the timespec to validate.
+ *
+ * Returned Value:
+ *   true if the timespec is valid, false otherwise.
+ *
+ ****************************************************************************/
+
+static inline_function
+bool clock_timespec_validate(FAR const struct timespec *ts)
+{
+  DEBUGASSERT(ts);
+  return ts->tv_sec >= 0 && ts->tv_nsec >= 0 &&
+         ts->tv_nsec < NSEC_PER_SEC;
+}
+
 /****************************************************************************
  * Name: clock_abstime2ticks
  *
diff --git a/libs/libc/pthread/pthread_condclockwait.c 
b/libs/libc/pthread/pthread_condclockwait.c
index 23687307b74..a3ff8aca3fa 100644
--- a/libs/libc/pthread/pthread_condclockwait.c
+++ b/libs/libc/pthread/pthread_condclockwait.c
@@ -113,8 +113,7 @@ int pthread_cond_clockwait(FAR pthread_cond_t *cond,
    * specified by cond or the mutex specified by mutex.
    */
 
-  else if (abstime && (abstime->tv_nsec < 0 ||
-           abstime->tv_nsec >= NSEC_PER_SEC))
+  else if (abstime && !clock_timespec_validate(abstime))
     {
       ret = EINVAL;
     }
diff --git a/sched/clock/clock_settime.c b/sched/clock/clock_settime.c
index e028c39a163..d8869c063a3 100644
--- a/sched/clock/clock_settime.c
+++ b/sched/clock/clock_settime.c
@@ -145,7 +145,7 @@ int clock_settime(clockid_t clock_id, FAR const struct 
timespec *tp)
 {
   int ret = -EINVAL;
 
-  if (tp != NULL && tp->tv_nsec >= 0 && tp->tv_nsec < 1000000000)
+  if (tp != NULL && clock_timespec_validate(tp))
     {
       if (clock_id == CLOCK_REALTIME)
         {
diff --git a/sched/mqueue/mq_receive.c b/sched/mqueue/mq_receive.c
index 4a2b9e1317f..9339e2f6485 100644
--- a/sched/mqueue/mq_receive.c
+++ b/sched/mqueue/mq_receive.c
@@ -160,7 +160,7 @@ ssize_t file_mq_timedreceive_internal(FAR struct file *mq, 
FAR char *msg,
 
   /* Verify the input parameters */
 
-  if (abstime && (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000))
+  if (abstime && !clock_timespec_validate(abstime))
     {
       return -EINVAL;
     }
diff --git a/sched/mqueue/mq_send.c b/sched/mqueue/mq_send.c
index 9cebc7a35e7..029d8e01f4f 100644
--- a/sched/mqueue/mq_send.c
+++ b/sched/mqueue/mq_send.c
@@ -302,7 +302,7 @@ int file_mq_timedsend_internal(FAR struct file *mq, FAR 
const char *msg,
 
   /* Verify the input parameters */
 
-  if (abstime && (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000))
+  if (abstime && !clock_timespec_validate(abstime))
     {
       return -EINVAL;
     }
diff --git a/sched/semaphore/sem_clockwait.c b/sched/semaphore/sem_clockwait.c
index ea5bb9c5427..18930771bd2 100644
--- a/sched/semaphore/sem_clockwait.c
+++ b/sched/semaphore/sem_clockwait.c
@@ -107,7 +107,7 @@ int nxsem_clockwait(FAR sem_t *sem, clockid_t clockid,
    * enabled while we are blocked waiting for the semaphore.
    */
 
-  if (abstime->tv_nsec >= 0 && abstime->tv_nsec < 1000000000)
+  if (clock_timespec_validate(abstime))
     {
       flags = enter_critical_section();
 
diff --git a/sched/signal/sig_clockwait.c b/sched/signal/sig_clockwait.c
index f7c9b03db52..c82edb1ba86 100644
--- a/sched/signal/sig_clockwait.c
+++ b/sched/signal/sig_clockwait.c
@@ -203,7 +203,7 @@ int nxsig_clockwait(int clockid, int flags,
   bool own_unbinfo = false;
 #endif
 
-  if (rqtp && (rqtp->tv_nsec < 0 || rqtp->tv_nsec >= 1000000000))
+  if (rqtp && !clock_timespec_validate(rqtp))
     {
       ret = -EINVAL;
     }

Reply via email to