mirror of
https://github.com/systemd/systemd.git
synced 2026-08-07 08:30:57 +00:00
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 for500727c220Follow-up for27d340c772Follow-up fore537352b9bFollow-up for1007ec60e6
This commit is contained in:
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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 ||
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user