[newlib-cygwin] Cygwin: clock.h: add valid_timespec() to check timespec for validity

Corinna Vinschen corinna@sourceware.org
Sat Jan 19 19:00:00 GMT 2019


https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=397526dee858e4b43b0e4ecca470a1df92f09d5d

commit 397526dee858e4b43b0e4ecca470a1df92f09d5d
Author: Corinna Vinschen <corinna@vinschen.de>
Date:   Fri Jan 18 14:31:01 2019 +0100

    Cygwin: clock.h: add valid_timespec() to check timespec for validity
    
    Use throughout, drop local timespec_bad() in timer.cc.
    
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

Diff:
---
 winsup/cygwin/aio.cc       |  2 +-
 winsup/cygwin/clock.h      |  8 ++++++++
 winsup/cygwin/posix_ipc.cc |  4 +---
 winsup/cygwin/signal.cc    |  5 ++---
 winsup/cygwin/thread.cc    |  4 +---
 winsup/cygwin/timer.cc     | 19 ++++++-------------
 6 files changed, 19 insertions(+), 23 deletions(-)

diff --git a/winsup/cygwin/aio.cc b/winsup/cygwin/aio.cc
index b245bdd..f38ea4d 100644
--- a/winsup/cygwin/aio.cc
+++ b/winsup/cygwin/aio.cc
@@ -766,7 +766,7 @@ aiosuspend (const struct aiocb *const aiolist[],
   if (timeout)
     {
       to = *timeout;
-      if (to.tv_sec < 0 || to.tv_nsec < 0 || to.tv_nsec > NSPERSEC)
+      if (!valid_timespec (to))
         {
           set_errno (EINVAL);
           return -1;
diff --git a/winsup/cygwin/clock.h b/winsup/cygwin/clock.h
index c05bf47..538b4b2 100644
--- a/winsup/cygwin/clock.h
+++ b/winsup/cygwin/clock.h
@@ -166,4 +166,12 @@ ts_diff (const struct timespec &ts0, struct timespec &ts1)
   ts1.tv_sec -= ts0.tv_sec;
 }
 
+static inline bool
+valid_timespec (const timespec& ts)
+{
+  if (ts.tv_nsec < 0 || ts.tv_nsec >= NSPERSEC || ts.tv_sec < 0)
+    return false;
+  return true;
+}
+
 #endif /*__CLOCK_H__*/
diff --git a/winsup/cygwin/posix_ipc.cc b/winsup/cygwin/posix_ipc.cc
index 36e9ed9..86734c9 100644
--- a/winsup/cygwin/posix_ipc.cc
+++ b/winsup/cygwin/posix_ipc.cc
@@ -181,9 +181,7 @@ ipc_cond_timedwait (HANDLE evt, HANDLE mtx, const struct timespec *abstime)
     ++cnt;
   if (abstime)
     {
-      if (abstime->tv_sec < 0
-	       || abstime->tv_nsec < 0
-	       || abstime->tv_nsec >= NSPERSEC)
+      if (!valid_timespec (*abstime))
 	return EINVAL;
 
       /* If a timeout is set, we create a waitable timer to wait for.
diff --git a/winsup/cygwin/signal.cc b/winsup/cygwin/signal.cc
index 5759cc4..fdde260 100644
--- a/winsup/cygwin/signal.cc
+++ b/winsup/cygwin/signal.cc
@@ -69,7 +69,7 @@ clock_nanosleep (clockid_t clk_id, int flags, const struct timespec *rqtp,
 
   __try
     {
-      if (rqtp->tv_sec < 0 || rqtp->tv_nsec < 0 || rqtp->tv_nsec >= NSPERSEC)
+      if (!valid_timespec (*rqtp))
 	return EINVAL;
     }
   __except (NO_ERROR)
@@ -654,8 +654,7 @@ sigtimedwait (const sigset_t *set, siginfo_t *info, const timespec *timeout)
 
   if (timeout)
     {
-      if (timeout->tv_sec < 0
-	    || timeout->tv_nsec < 0 || timeout->tv_nsec > NSPERSEC)
+      if (!valid_timespec (*timeout))
 	{
 	  set_errno (EINVAL);
 	  return -1;
diff --git a/winsup/cygwin/thread.cc b/winsup/cygwin/thread.cc
index f0f7b0a..c7b7e51 100644
--- a/winsup/cygwin/thread.cc
+++ b/winsup/cygwin/thread.cc
@@ -2549,9 +2549,7 @@ pthread_convert_abstime (clockid_t clock_id, const struct timespec *abstime,
   struct timespec tp;
 
   /* According to SUSv3, the abstime value must be checked for validity. */
-  if (abstime->tv_sec < 0
-      || abstime->tv_nsec < 0
-      || abstime->tv_nsec >= NSPERSEC)
+  if (!valid_timespec (*abstime))
     return EINVAL;
 
   /* Check for immediate timeout before converting */
diff --git a/winsup/cygwin/timer.cc b/winsup/cygwin/timer.cc
index c972745..e817dab 100644
--- a/winsup/cygwin/timer.cc
+++ b/winsup/cygwin/timer.cc
@@ -342,17 +342,6 @@ timer_thread (VOID *x)
   return tt->thread_func ();
 }
 
-static inline bool
-timespec_bad (const timespec& t)
-{
-  if (t.tv_nsec < 0 || t.tv_nsec >= NSPERSEC || t.tv_sec < 0)
-    {
-      set_errno (EINVAL);
-      return true;
-    }
-  return false;
-}
-
 int
 timer_tracker::settime (int in_flags, const itimerspec *value, itimerspec *ovalue)
 {
@@ -366,8 +355,12 @@ timer_tracker::settime (int in_flags, const itimerspec *value, itimerspec *ovalu
 	  __leave;
 	}
 
-      if (timespec_bad (value->it_value) || timespec_bad (value->it_interval))
-	__leave;
+      if (!valid_timespec (value->it_value)
+	  || !valid_timespec (value->it_interval))
+	{
+	  set_errno (EINVAL);
+	  __leave;
+	}
 
       lock_timer_tracker here;
       cancel ();



More information about the Cygwin-cvs mailing list