Skip to content

Commit 3ad440b

Browse files
committed
posix: add timespec_is_valid() private internal function
Add a common private function timespec_is_valid() that can be used to check if a timespec object is valid, and use that consistently in lib/posix/options. Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
1 parent 96466fe commit 3ad440b

File tree

7 files changed

+41
-15
lines changed

7 files changed

+41
-15
lines changed

lib/posix/options/mqueue.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ int mq_timedsend(mqd_t mqdes, const char *msg_ptr, size_t msg_len,
258258
{
259259
mqueue_desc *mqd = (mqueue_desc *)mqdes;
260260

261+
if ((abstime == NULL) || !timespec_is_valid(abstime)) {
262+
errno = EINVAL;
263+
return -1;
264+
}
265+
261266
return send_message(mqd, msg_ptr, msg_len, K_MSEC(timespec_to_timeoutms(abstime)));
262267
}
263268

@@ -288,6 +293,11 @@ int mq_timedreceive(mqd_t mqdes, char *msg_ptr, size_t msg_len,
288293
{
289294
mqueue_desc *mqd = (mqueue_desc *)mqdes;
290295

296+
if ((abstime == NULL) || !timespec_is_valid(abstime)) {
297+
errno = EINVAL;
298+
return -1;
299+
}
300+
291301
return receive_message(mqd, msg_ptr, msg_len, K_MSEC(timespec_to_timeoutms(abstime)));
292302
}
293303

lib/posix/options/mutex.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,11 @@ int pthread_mutex_trylock(pthread_mutex_t *m)
211211
int pthread_mutex_timedlock(pthread_mutex_t *m,
212212
const struct timespec *abstime)
213213
{
214+
if ((abstime == NULL) || !timespec_is_valid(abstime)) {
215+
LOG_DBG("%s is invalid", "abstime");
216+
return EINVAL;
217+
}
218+
214219
return acquire_mutex(m, K_MSEC(timespec_to_timeoutms(abstime)));
215220
}
216221

lib/posix/options/posix_clock.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,20 @@
88
#ifndef ZEPHYR_LIB_POSIX_POSIX_CLOCK_H_
99
#define ZEPHYR_LIB_POSIX_POSIX_CLOCK_H_
1010

11+
#include <stdbool.h>
12+
#include <stddef.h>
1113
#include <stdint.h>
1214
#include <time.h>
1315

16+
#include <zephyr/sys_clock.h>
17+
#include <zephyr/sys/__assert.h>
18+
19+
static inline bool timespec_is_valid(const struct timespec *ts)
20+
{
21+
__ASSERT_NO_MSG(ts != NULL);
22+
return (ts->tv_nsec >= 0) && (ts->tv_nsec < NSEC_PER_SEC);
23+
}
24+
1425
uint32_t timespec_to_clock_timeoutms(clockid_t clock_id, const struct timespec *abstime);
1526
uint32_t timespec_to_timeoutms(const struct timespec *abstime);
1627

lib/posix/options/pthread.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,11 +1149,8 @@ static int pthread_timedjoin_internal(pthread_t pthread, void **status, k_timeou
11491149
*/
11501150
int pthread_timedjoin_np(pthread_t pthread, void **status, const struct timespec *abstime)
11511151
{
1152-
if (abstime == NULL) {
1153-
return EINVAL;
1154-
}
1155-
1156-
if (abstime->tv_sec < 0 || abstime->tv_nsec < 0 || abstime->tv_nsec >= NSEC_PER_SEC) {
1152+
if ((abstime == NULL) || !timespec_is_valid(abstime)) {
1153+
LOG_DBG("%s is invalid", "abstime");
11571154
return EINVAL;
11581155
}
11591156

lib/posix/options/rwlock.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ int pthread_rwlock_timedrdlock(pthread_rwlock_t *rwlock,
201201
uint32_t ret = 0U;
202202
struct posix_rwlock *rwl;
203203

204-
if (abstime->tv_nsec < 0 || abstime->tv_nsec > NSEC_PER_SEC) {
204+
if ((abstime == NULL) || !timespec_is_valid(abstime)) {
205+
LOG_DBG("%s is invalid", "abstime");
205206
return EINVAL;
206207
}
207208

@@ -271,7 +272,8 @@ int pthread_rwlock_timedwrlock(pthread_rwlock_t *rwlock,
271272
uint32_t ret = 0U;
272273
struct posix_rwlock *rwl;
273274

274-
if (abstime->tv_nsec < 0 || abstime->tv_nsec > NSEC_PER_SEC) {
275+
if ((abstime == NULL) || !timespec_is_valid(abstime)) {
276+
LOG_DBG("%s is invalid", "abstime");
275277
return EINVAL;
276278
}
277279

lib/posix/options/semaphore.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* SPDX-License-Identifier: Apache-2.0
66
*/
77

8+
#include "posix_clock.h"
9+
810
#include <errno.h>
911
#include <zephyr/kernel.h>
1012
#include <zephyr/sys/atomic.h>
@@ -163,9 +165,7 @@ int sem_timedwait(sem_t *semaphore, struct timespec *abstime)
163165
struct timespec current;
164166
int64_t current_ms, abstime_ms;
165167

166-
__ASSERT(abstime, "abstime pointer NULL");
167-
168-
if ((abstime->tv_sec < 0) || (abstime->tv_nsec >= NSEC_PER_SEC)) {
168+
if ((abstime == NULL) || !timespec_is_valid(abstime)) {
169169
errno = EINVAL;
170170
return -1;
171171
}

lib/posix/options/timer.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
*
55
* SPDX-License-Identifier: Apache-2.0
66
*/
7+
78
#undef _POSIX_C_SOURCE
89
#define _POSIX_C_SOURCE 200809L
10+
11+
#include "posix_clock.h"
12+
913
#include <errno.h>
1014

1115
#include <zephyr/kernel.h>
@@ -241,11 +245,8 @@ int timer_settime(timer_t timerid, int flags, const struct itimerspec *value,
241245
struct timer_obj *timer = (struct timer_obj *) timerid;
242246
uint32_t duration, current;
243247

244-
if (timer == NULL ||
245-
value->it_interval.tv_nsec < 0 ||
246-
value->it_interval.tv_nsec >= NSEC_PER_SEC ||
247-
value->it_value.tv_nsec < 0 ||
248-
value->it_value.tv_nsec >= NSEC_PER_SEC) {
248+
if ((timer == NULL) || !timespec_is_valid(&value->it_interval) ||
249+
!timespec_is_valid(&value->it_value)) {
249250
errno = EINVAL;
250251
return -1;
251252
}

0 commit comments

Comments
 (0)