time-util: fix overflow condition in usec_sub_signed()

If the delta specified is INT64_MIN, and we negate that we'd end up at
INT64_MAX+1 which is outside of the int64_t type. Hence let's treat this
case specifically to avoid unintended overflows.
This commit is contained in:
Lennart Poettering
2022-08-24 21:41:40 +01:00
committed by Luca Boccassi
parent 2306b4656a
commit 782c6e5c90
2 changed files with 23 additions and 2 deletions
+7 -2
View File
@@ -189,10 +189,15 @@ static inline usec_t usec_sub_unsigned(usec_t timestamp, usec_t delta) {
}
static inline usec_t usec_sub_signed(usec_t timestamp, int64_t delta) {
if (delta == INT64_MIN) { /* prevent overflow */
assert_cc(-(INT64_MIN + 1) == INT64_MAX);
assert_cc(USEC_INFINITY > INT64_MAX);
return usec_add(timestamp, (usec_t) INT64_MAX + 1);
}
if (delta < 0)
return usec_add(timestamp, (usec_t) (-delta));
else
return usec_sub_unsigned(timestamp, (usec_t) delta);
return usec_sub_unsigned(timestamp, (usec_t) delta);
}
#if SIZEOF_TIME_T == 8