From 48123656609fea92a154f08ab619ab5186276432 Mon Sep 17 00:00:00 2001 From: Lukasz Majewski Date: Thu, 24 Oct 2019 16:20:56 +0200 Subject: time: Introduce function to check correctness of nanoseconds value The valid_nanoseconds () static inline function has been introduced to check if nanoseconds value is in the correct range - greater or equal to zero and less than 1000000000. The explicit #include has been added to files where it was missing. The __syscall_slong_t type for ns has been used to avoid issues on x32. Tested with: - scripts/build-many-glibcs.py - make PARALLELMFLAGS="-j12" && make PARALLELMFLAGS="-j12" xcheck on x86_64 --- nptl/lll_timedlock_wait.c | 3 ++- nptl/pthread_cond_wait.c | 4 ++-- nptl/pthread_join_common.c | 3 ++- nptl/pthread_mutex_timedlock.c | 4 ++-- nptl/pthread_rwlock_common.c | 7 +++---- nptl/sem_clockwait.c | 3 ++- nptl/sem_timedwait.c | 3 ++- 7 files changed, 15 insertions(+), 12 deletions(-) (limited to 'nptl') diff --git a/nptl/lll_timedlock_wait.c b/nptl/lll_timedlock_wait.c index 03060e8..cd3cc3d 100644 --- a/nptl/lll_timedlock_wait.c +++ b/nptl/lll_timedlock_wait.c @@ -21,6 +21,7 @@ #include #include #include +#include int @@ -28,7 +29,7 @@ __lll_clocklock_wait (int *futex, clockid_t clockid, const struct timespec *abstime, int private) { /* Reject invalid timeouts. */ - if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) + if (! valid_nanoseconds (abstime->tv_nsec)) return EINVAL; /* Try locking. */ diff --git a/nptl/pthread_cond_wait.c b/nptl/pthread_cond_wait.c index bacae09..cf372bc 100644 --- a/nptl/pthread_cond_wait.c +++ b/nptl/pthread_cond_wait.c @@ -645,7 +645,7 @@ __pthread_cond_timedwait (pthread_cond_t *cond, pthread_mutex_t *mutex, { /* Check parameter validity. This should also tell the compiler that it can assume that abstime is not NULL. */ - if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) + if (! valid_nanoseconds (abstime->tv_nsec)) return EINVAL; /* Relaxed MO is suffice because clock ID bit is only modified @@ -668,7 +668,7 @@ __pthread_cond_clockwait (pthread_cond_t *cond, pthread_mutex_t *mutex, { /* Check parameter validity. This should also tell the compiler that it can assume that abstime is not NULL. */ - if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) + if (! valid_nanoseconds (abstime->tv_nsec)) return EINVAL; if (!futex_abstimed_supported_clockid (clockid)) diff --git a/nptl/pthread_join_common.c b/nptl/pthread_join_common.c index 9545ae4..8b55c38 100644 --- a/nptl/pthread_join_common.c +++ b/nptl/pthread_join_common.c @@ -19,6 +19,7 @@ #include "pthreadP.h" #include #include +#include static void cleanup (void *arg) @@ -40,7 +41,7 @@ timedwait_tid (pid_t *tidp, const struct timespec *abstime) { pid_t tid; - if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) + if (! valid_nanoseconds (abstime->tv_nsec)) return EINVAL; /* Repeat until thread terminated. */ diff --git a/nptl/pthread_mutex_timedlock.c b/nptl/pthread_mutex_timedlock.c index a0ce044..c9bb3b9 100644 --- a/nptl/pthread_mutex_timedlock.c +++ b/nptl/pthread_mutex_timedlock.c @@ -235,7 +235,7 @@ __pthread_mutex_clocklock_common (pthread_mutex_t *mutex, } /* We are about to block; check whether the timeout is invalid. */ - if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) + if (! valid_nanoseconds (abstime->tv_nsec)) return EINVAL; /* Work around the fact that the kernel rejects negative timeout values despite them being valid. */ @@ -561,7 +561,7 @@ __pthread_mutex_clocklock_common (pthread_mutex_t *mutex, if (oldval != ceilval) { /* Reject invalid timeouts. */ - if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) + if (! valid_nanoseconds (abstime->tv_nsec)) { result = EINVAL; goto failpp; diff --git a/nptl/pthread_rwlock_common.c b/nptl/pthread_rwlock_common.c index 7070b9c..9c05e03 100644 --- a/nptl/pthread_rwlock_common.c +++ b/nptl/pthread_rwlock_common.c @@ -24,6 +24,7 @@ #include #include #include +#include /* A reader--writer lock that fulfills the POSIX requirements (but operations @@ -290,8 +291,7 @@ __pthread_rwlock_rdlock_full (pthread_rwlock_t *rwlock, if the lock can be immediately acquired" (i.e., we need not but may check it). */ if (abstime && __glibc_unlikely (!futex_abstimed_supported_clockid (clockid) - || abstime->tv_nsec >= 1000000000 - || abstime->tv_nsec < 0)) + || ! valid_nanoseconds (abstime->tv_nsec))) return EINVAL; /* Make sure we are not holding the rwlock as a writer. This is a deadlock @@ -596,8 +596,7 @@ __pthread_rwlock_wrlock_full (pthread_rwlock_t *rwlock, if the lock can be immediately acquired" (i.e., we need not but may check it). */ if (abstime && __glibc_unlikely (!futex_abstimed_supported_clockid (clockid) - || abstime->tv_nsec >= 1000000000 - || abstime->tv_nsec < 0)) + || ! valid_nanoseconds (abstime->tv_nsec))) return EINVAL; /* Make sure we are not holding the rwlock as a writer. This is a deadlock diff --git a/nptl/sem_clockwait.c b/nptl/sem_clockwait.c index 9ed98c4..21628df 100644 --- a/nptl/sem_clockwait.c +++ b/nptl/sem_clockwait.c @@ -18,6 +18,7 @@ License along with the GNU C Library; if not, see . */ +#include #include "sem_waitcommon.c" int @@ -32,7 +33,7 @@ sem_clockwait (sem_t *sem, clockid_t clockid, return -1; } - if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) + if (! valid_nanoseconds (abstime->tv_nsec)) { __set_errno (EINVAL); return -1; diff --git a/nptl/sem_timedwait.c b/nptl/sem_timedwait.c index fbb50a5..a3fbe89 100644 --- a/nptl/sem_timedwait.c +++ b/nptl/sem_timedwait.c @@ -17,6 +17,7 @@ License along with the GNU C Library; if not, see . */ +#include #include "sem_waitcommon.c" /* This is in a separate file because because sem_timedwait is only provided @@ -24,7 +25,7 @@ int sem_timedwait (sem_t *sem, const struct timespec *abstime) { - if (abstime->tv_nsec < 0 || abstime->tv_nsec >= 1000000000) + if (! valid_nanoseconds (abstime->tv_nsec)) { __set_errno (EINVAL); return -1; -- cgit v1.1