From 331461a5a2ffe323190c4ca6b7bcd35944e36f92 Mon Sep 17 00:00:00 2001 From: Luca Boccassi Date: Sat, 28 Mar 2026 21:28:56 +0000 Subject: [PATCH] tree-wide: add assert_cc for time constant multiplications Coverity flags compile-time constant multiplications of USEC_PER_SEC, USEC_PER_MSEC, and USEC_PER_HOUR as potential overflows. Add assert_cc() to prove they fit at build time. CID#1548025 CID#1548048 CID#1548055 CID#1548059 Follow-up for 500727c220354b81b68ed6667d9a6f0fafe3ba19 Follow-up for 27d340c772fb1b251085dba7bd5420484f7c5892 Follow-up for e537352b9bfffe6f6286483bff2c7601c78407e3 Follow-up for 1007ec60e664da03b7aea4803c643d991fcf6530 --- src/fsck/fsck.c | 1 + src/shared/utmp-wtmp.c | 2 ++ src/test/test-path.c | 2 ++ src/test/test-time-util.c | 2 ++ 4 files changed, 7 insertions(+) diff --git a/src/fsck/fsck.c b/src/fsck/fsck.c index 9767568724f..43cc208e598 100644 --- a/src/fsck/fsck.c +++ b/src/fsck/fsck.c @@ -218,6 +218,7 @@ static int process_progress(int fd, FILE* console) { /* Only update once every 50ms */ t = now(CLOCK_MONOTONIC); + assert_cc(50 * USEC_PER_MSEC <= USEC_INFINITY); if (last + 50 * USEC_PER_MSEC > t) continue; diff --git a/src/shared/utmp-wtmp.c b/src/shared/utmp-wtmp.c index 6d150e7dcf3..cfc6ae5a3dd 100644 --- a/src/shared/utmp-wtmp.c +++ b/src/shared/utmp-wtmp.c @@ -17,6 +17,8 @@ static void init_timestamp(struct utmpx *store, usec_t t) { if (t <= 0) t = now(CLOCK_REALTIME); + /* Silence static analyzers */ + assert_cc(USEC_PER_SEC > 0); store->ut_tv.tv_sec = t / USEC_PER_SEC; store->ut_tv.tv_usec = t % USEC_PER_SEC; } diff --git a/src/test/test-path.c b/src/test/test-path.c index d282cfbf891..82b1f27ed75 100644 --- a/src/test/test-path.c +++ b/src/test/test-path.c @@ -78,6 +78,8 @@ static int _check_states(unsigned line, assert_se(m); assert_se(service); + /* Silence static analyzers */ + assert_cc(30 * USEC_PER_SEC <= USEC_INFINITY); usec_t end = now(CLOCK_MONOTONIC) + 30 * USEC_PER_SEC; while (path->state != path_state || service->state != service_state || diff --git a/src/test/test-time-util.c b/src/test/test-time-util.c index 04da9891cb7..8250a03e298 100644 --- a/src/test/test-time-util.c +++ b/src/test/test-time-util.c @@ -1113,6 +1113,8 @@ TEST(usec_shift_clock) { assert_se(usec_shift_clock(USEC_INFINITY, CLOCK_REALTIME, CLOCK_MONOTONIC) == USEC_INFINITY); + /* Silence static analyzers */ + assert_cc(9 * USEC_PER_HOUR <= USEC_INFINITY); assert_similar(usec_shift_clock(rt + USEC_PER_HOUR, CLOCK_REALTIME, CLOCK_MONOTONIC), mn + USEC_PER_HOUR); assert_similar(usec_shift_clock(rt + 2*USEC_PER_HOUR, CLOCK_REALTIME, CLOCK_BOOTTIME), bt + 2*USEC_PER_HOUR); assert_se(usec_shift_clock(rt + 3*USEC_PER_HOUR, CLOCK_REALTIME, CLOCK_REALTIME_ALARM) == rt + 3*USEC_PER_HOUR);