diff --git a/lib/posix/options/cond.c b/lib/posix/options/cond.c index 55e840210d9..40947aa3c45 100644 --- a/lib/posix/options/cond.c +++ b/lib/posix/options/cond.c @@ -175,6 +175,11 @@ int pthread_cond_wait(pthread_cond_t *cv, pthread_mutex_t *mut) int pthread_cond_timedwait(pthread_cond_t *cv, pthread_mutex_t *mut, const struct timespec *abstime) { + if ((abstime == NULL) || !timespec_is_valid(abstime)) { + LOG_DBG("%s is invalid", "abstime"); + return EINVAL; + } + return cond_wait(cv, mut, abstime); } diff --git a/lib/posix/options/mqueue.c b/lib/posix/options/mqueue.c index 99966786a0b..23f2ee73ccb 100644 --- a/lib/posix/options/mqueue.c +++ b/lib/posix/options/mqueue.c @@ -258,6 +258,11 @@ int mq_timedsend(mqd_t mqdes, const char *msg_ptr, size_t msg_len, { mqueue_desc *mqd = (mqueue_desc *)mqdes; + if ((abstime == NULL) || !timespec_is_valid(abstime)) { + errno = EINVAL; + return -1; + } + return send_message(mqd, msg_ptr, msg_len, K_MSEC(timespec_to_timeoutms(abstime))); } @@ -288,6 +293,11 @@ int mq_timedreceive(mqd_t mqdes, char *msg_ptr, size_t msg_len, { mqueue_desc *mqd = (mqueue_desc *)mqdes; + if ((abstime == NULL) || !timespec_is_valid(abstime)) { + errno = EINVAL; + return -1; + } + return receive_message(mqd, msg_ptr, msg_len, K_MSEC(timespec_to_timeoutms(abstime))); } diff --git a/lib/posix/options/mutex.c b/lib/posix/options/mutex.c index d61458d1e43..51bd3f53c7c 100644 --- a/lib/posix/options/mutex.c +++ b/lib/posix/options/mutex.c @@ -211,6 +211,11 @@ int pthread_mutex_trylock(pthread_mutex_t *m) int pthread_mutex_timedlock(pthread_mutex_t *m, const struct timespec *abstime) { + if ((abstime == NULL) || !timespec_is_valid(abstime)) { + LOG_DBG("%s is invalid", "abstime"); + return EINVAL; + } + return acquire_mutex(m, K_MSEC(timespec_to_timeoutms(abstime))); } diff --git a/lib/posix/options/posix_clock.h b/lib/posix/options/posix_clock.h index 18fe562ec4d..c0ebe66fde6 100644 --- a/lib/posix/options/posix_clock.h +++ b/lib/posix/options/posix_clock.h @@ -8,9 +8,20 @@ #ifndef ZEPHYR_LIB_POSIX_POSIX_CLOCK_H_ #define ZEPHYR_LIB_POSIX_POSIX_CLOCK_H_ +#include +#include #include #include +#include +#include + +static inline bool timespec_is_valid(const struct timespec *ts) +{ + __ASSERT_NO_MSG(ts != NULL); + return (ts->tv_nsec >= 0) && (ts->tv_nsec < NSEC_PER_SEC); +} + uint32_t timespec_to_clock_timeoutms(clockid_t clock_id, const struct timespec *abstime); uint32_t timespec_to_timeoutms(const struct timespec *abstime); diff --git a/lib/posix/options/pthread.c b/lib/posix/options/pthread.c index db0ec7ec8c5..da79330be17 100644 --- a/lib/posix/options/pthread.c +++ b/lib/posix/options/pthread.c @@ -1149,11 +1149,8 @@ static int pthread_timedjoin_internal(pthread_t pthread, void **status, k_timeou */ int pthread_timedjoin_np(pthread_t pthread, void **status, const struct timespec *abstime) { - if (abstime == NULL) { - return EINVAL; - } - - if (abstime->tv_sec < 0 || abstime->tv_nsec < 0 || abstime->tv_nsec >= NSEC_PER_SEC) { + if ((abstime == NULL) || !timespec_is_valid(abstime)) { + LOG_DBG("%s is invalid", "abstime"); return EINVAL; } diff --git a/lib/posix/options/rwlock.c b/lib/posix/options/rwlock.c index 84133859e86..8d1a35584f9 100644 --- a/lib/posix/options/rwlock.c +++ b/lib/posix/options/rwlock.c @@ -201,7 +201,8 @@ int pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock, uint32_t ret = 0U; struct posix_rwlock *rwl; - if (abstime->tv_nsec < 0 || abstime->tv_nsec > NSEC_PER_SEC) { + if ((abstime == NULL) || !timespec_is_valid(abstime)) { + LOG_DBG("%s is invalid", "abstime"); return EINVAL; } @@ -271,7 +272,8 @@ int pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock, uint32_t ret = 0U; struct posix_rwlock *rwl; - if (abstime->tv_nsec < 0 || abstime->tv_nsec > NSEC_PER_SEC) { + if ((abstime == NULL) || !timespec_is_valid(abstime)) { + LOG_DBG("%s is invalid", "abstime"); return EINVAL; } diff --git a/lib/posix/options/semaphore.c b/lib/posix/options/semaphore.c index 537b618ff6f..d3787862fb4 100644 --- a/lib/posix/options/semaphore.c +++ b/lib/posix/options/semaphore.c @@ -5,6 +5,8 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include "posix_clock.h" + #include #include #include @@ -163,9 +165,7 @@ int sem_timedwait(sem_t *semaphore, struct timespec *abstime) struct timespec current; int64_t current_ms, abstime_ms; - __ASSERT(abstime, "abstime pointer NULL"); - - if ((abstime->tv_sec < 0) || (abstime->tv_nsec >= NSEC_PER_SEC)) { + if ((abstime == NULL) || !timespec_is_valid(abstime)) { errno = EINVAL; return -1; } diff --git a/lib/posix/options/timer.c b/lib/posix/options/timer.c index e810b4576f7..41dcbefeb57 100644 --- a/lib/posix/options/timer.c +++ b/lib/posix/options/timer.c @@ -4,8 +4,12 @@ * * SPDX-License-Identifier: Apache-2.0 */ + #undef _POSIX_C_SOURCE #define _POSIX_C_SOURCE 200809L + +#include "posix_clock.h" + #include #include @@ -241,11 +245,8 @@ int timer_settime(timer_t timerid, int flags, const struct itimerspec *value, struct timer_obj *timer = (struct timer_obj *) timerid; uint32_t duration, current; - if (timer == NULL || - value->it_interval.tv_nsec < 0 || - value->it_interval.tv_nsec >= NSEC_PER_SEC || - value->it_value.tv_nsec < 0 || - value->it_value.tv_nsec >= NSEC_PER_SEC) { + if ((timer == NULL) || !timespec_is_valid(&value->it_interval) || + !timespec_is_valid(&value->it_value)) { errno = EINVAL; return -1; }