diff --git a/winsup/cygwin/aio.cc b/winsup/cygwin/aio.cc index b245bdd02..f38ea4d11 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 c05bf477c..538b4b284 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 36e9ed9cf..86734c96a 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 5759cc4d6..fdde26058 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 f0f7b0a5f..c7b7e5157 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 c97274538..e817dab81 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 ();