From: Yu Watanabe Date: Mon, 13 Feb 2023 19:27:27 +0000 (+0900) Subject: time-util: use usec_add() and usec_sub_unsigned() X-Git-Tag: v254-rc1~1163^2~5 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=db43717e982e1361eee4bdcd92167d6c47eb627c;p=thirdparty%2Fsystemd.git time-util: use usec_add() and usec_sub_unsigned() And move the check with USEC_TIMESTAMP_FORMATTABLE_MAX at the end, as usec_add() can handle overflow correctly. --- diff --git a/src/basic/time-util.c b/src/basic/time-util.c index 73def365a48..5ffd6bc5749 100644 --- a/src/basic/time-util.c +++ b/src/basic/time-util.c @@ -882,20 +882,17 @@ from_tm: if (x < 0) return -EINVAL; - usec = (usec_t) x * USEC_PER_SEC + x_usec; - if (usec > USEC_TIMESTAMP_FORMATTABLE_MAX) - return -EINVAL; + usec = usec_add(x * USEC_PER_SEC, x_usec); finish: - if (usec + plus < usec) /* overflow? */ - return -EINVAL; - usec += plus; - if (usec > USEC_TIMESTAMP_FORMATTABLE_MAX) + usec = usec_add(usec, plus); + + if (usec < minus) return -EINVAL; - if (usec >= minus) - usec -= minus; - else + usec = usec_sub_unsigned(usec, minus); + + if (usec > USEC_TIMESTAMP_FORMATTABLE_MAX) return -EINVAL; if (ret)