Skip to content

Commit 270cee2

Browse files
committed
posix: timers: use newly added timespec util functions
Use the newly added timespec util functions to manipulate and compare timespec structures with overflow detection. Signed-off-by: Chris Friedt <[email protected]>
1 parent fe4f5c4 commit 270cee2

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

lib/posix/options/clock_common.c

+12-7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <zephyr/posix/unistd.h>
1616
#include <zephyr/internal/syscall_handler.h>
1717
#include <zephyr/sys/sem.h>
18+
#include <zephyr/sys/timeutil.h>
1819

1920
/*
2021
* `k_uptime_get` returns a timestamp based on an always increasing
@@ -82,11 +83,10 @@ int z_clock_gettime(clockid_t clock_id, struct timespec *ts)
8283
/* For ns 32 bit conversion can be used since its smaller than 1sec. */
8384
ts->tv_nsec = (int32_t)k_ticks_to_ns_floor32(nremainder);
8485

85-
ts->tv_sec += base.tv_sec;
86-
ts->tv_nsec += base.tv_nsec;
87-
if (ts->tv_nsec >= NSEC_PER_SEC) {
88-
ts->tv_sec++;
89-
ts->tv_nsec -= NSEC_PER_SEC;
86+
if (unlikely(timespec_normalize_overflow(ts)) ||
87+
unlikely(timespec_add_overflow(ts, &base))) {
88+
errno = EOVERFLOW;
89+
return -1;
9090
}
9191

9292
return 0;
@@ -101,7 +101,7 @@ int z_clock_settime(clockid_t clock_id, const struct timespec *tp)
101101
return -1;
102102
}
103103

104-
if (tp->tv_nsec < 0 || tp->tv_nsec >= NSEC_PER_SEC) {
104+
if (!timespec_is_valid(tp)) {
105105
errno = EINVAL;
106106
return -1;
107107
}
@@ -112,6 +112,11 @@ int z_clock_settime(clockid_t clock_id, const struct timespec *tp)
112112
base.tv_sec = delta / NSEC_PER_SEC;
113113
base.tv_nsec = delta % NSEC_PER_SEC;
114114

115+
if (unlikely(timespec_normalize_overflow(&base))) {
116+
errno = EOVERFLOW;
117+
return -1;
118+
}
119+
115120
SYS_SEM_LOCK(&rt_clock_base_lock) {
116121
rt_clock_base = base;
117122
}
@@ -137,7 +142,7 @@ int z_clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp
137142
return -1;
138143
}
139144

140-
if ((rqtp->tv_sec < 0) || (rqtp->tv_nsec < 0) || (rqtp->tv_nsec >= NSEC_PER_SEC)) {
145+
if ((rqtp->tv_sec < 0) || !timespec_is_valid(rqtp)) {
141146
errno = EINVAL;
142147
return -1;
143148
}

lib/posix/options/posix_clock.h

+2-7
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,14 @@
1616
#include <zephyr/sys_clock.h>
1717
#include <zephyr/sys/__assert.h>
1818
#include <zephyr/posix/sys/time.h>
19+
#include <zephyr/sys/timeutil.h>
1920

2021
#ifdef __cplusplus
2122
extern "C" {
2223
#endif
2324

2425
/** @cond INTERNAL_HIDDEN */
2526

26-
static inline bool timespec_is_valid(const struct timespec *ts)
27-
{
28-
__ASSERT_NO_MSG(ts != NULL);
29-
return (ts->tv_nsec >= 0) && (ts->tv_nsec < NSEC_PER_SEC);
30-
}
31-
3227
static inline int64_t ts_to_ns(const struct timespec *ts)
3328
{
3429
return ts->tv_sec * NSEC_PER_SEC + ts->tv_nsec;
@@ -47,7 +42,7 @@ static inline void tv_to_ts(const struct timeval *tv, struct timespec *ts)
4742

4843
static inline bool tp_ge(const struct timespec *a, const struct timespec *b)
4944
{
50-
return ts_to_ns(a) >= ts_to_ns(b);
45+
return timespec_compare(a, b) >= 0;
5146
}
5247

5348
static inline int64_t tp_diff(const struct timespec *a, const struct timespec *b)

0 commit comments

Comments
 (0)