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>
This commit is contained in:
Corinna Vinschen 2019-01-18 14:31:01 +01:00
parent 7f983079d4
commit 397526dee8
6 changed files with 19 additions and 23 deletions

View File

@ -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;

View File

@ -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__*/

View File

@ -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.

View File

@ -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;

View File

@ -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 */

View File

@ -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 ();