Skip to content

Commit 9e5e294

Browse files
cfriedtdkalowsk
authored andcommitted
lib: os: clock: fix for CID 529870
Fix for CID 529870, where Coverity found an issue where `timespec.tv_sec` is never greater than `UINT64_MAX / NSEC_PER_SEC` (18446744073). This is naturally true when `time_t` is only 32-bit, which is actually never the case for any Zephyr platform aside from `native_sim/native/32`. When `time_t` is a signed 64-bit value, at some point in the future, but maybe not in our lifetimes, `timespec.tv_sec` could exceed 18446744073, since `INT64_MAX > UINT64_MAX / NSEC_PER_SEC`. We should not see coverity issues errors like this in the future, once we have a consistent `time_t` representation across all Zephyr platforms. Signed-off-by: Chris Friedt <cfriedt@tenstorrent.com>
1 parent aa7901c commit 9e5e294

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

lib/os/clock.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ int z_impl_sys_clock_nanosleep(int clock_id, int flags, const struct timespec *r
164164
}
165165

166166
/* sleep for relative time duration */
167-
if (unlikely(rqtp->tv_sec >= UINT64_MAX / NSEC_PER_SEC)) {
167+
if ((sizeof(rqtp->tv_sec) == sizeof(int64_t)) &&
168+
unlikely(rqtp->tv_sec >= (time_t)(UINT64_MAX / NSEC_PER_SEC))) {
168169
uint64_t ns = (uint64_t)k_sleep(K_SECONDS(duration.tv_sec - 1)) * NSEC_PER_MSEC;
169170
struct timespec rem = {
170171
.tv_sec = (time_t)(ns / NSEC_PER_SEC),

0 commit comments

Comments
 (0)