Skip to content

Commit f5bcfe6

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

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

lib/posix/options/clock_common.c

+11-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/timespec_util.h>
1819

1920
/*
2021
* `k_uptime_get` returns a timestamp based on an always increasing
@@ -82,11 +83,9 @@ 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(ts)) || unlikely(timespec_add(ts, &base))) {
87+
errno = EOVERFLOW;
88+
return -1;
9089
}
9190

9291
return 0;
@@ -101,7 +100,7 @@ int z_clock_settime(clockid_t clock_id, const struct timespec *tp)
101100
return -1;
102101
}
103102

104-
if (tp->tv_nsec < 0 || tp->tv_nsec >= NSEC_PER_SEC) {
103+
if (!timespec_is_valid(tp)) {
105104
errno = EINVAL;
106105
return -1;
107106
}
@@ -112,6 +111,11 @@ int z_clock_settime(clockid_t clock_id, const struct timespec *tp)
112111
base.tv_sec = delta / NSEC_PER_SEC;
113112
base.tv_nsec = delta % NSEC_PER_SEC;
114113

114+
if (unlikely(timespec_normalize(&base))) {
115+
errno = EOVERFLOW;
116+
return -1;
117+
}
118+
115119
SYS_SEM_LOCK(&rt_clock_base_lock) {
116120
rt_clock_base = base;
117121
}
@@ -137,7 +141,7 @@ int z_clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp
137141
return -1;
138142
}
139143

140-
if ((rqtp->tv_sec < 0) || (rqtp->tv_nsec < 0) || (rqtp->tv_nsec >= NSEC_PER_SEC)) {
144+
if ((rqtp->tv_sec < 0) || !timespec_is_valid(rqtp)) {
141145
errno = EINVAL;
142146
return -1;
143147
}

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/timespec_util.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)