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 500727c220
Follow-up for 27d340c772
Follow-up for e537352b9b
Follow-up for 1007ec60e6
This commit is contained in:
Luca Boccassi
2026-03-28 21:28:56 +00:00
parent 96b085c4be
commit 331461a5a2
4 changed files with 7 additions and 0 deletions

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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 ||

View File

@@ -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);