From: Lennart Poettering Date: Tue, 2 Mar 2021 20:33:35 +0000 (+0100) Subject: time-util: simplify overflow check X-Git-Tag: v248-rc3~79 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ab05bee1dd9e59a34c2f4866a1285dc29c010904;p=thirdparty%2Fsystemd.git time-util: simplify overflow check And don't rely on 2s complement. --- diff --git a/src/basic/time-util.h b/src/basic/time-util.h index da6f3cd382c..d716074fbe3 100644 --- a/src/basic/time-util.h +++ b/src/basic/time-util.h @@ -155,16 +155,14 @@ usec_t jiffies_to_usec(uint32_t jiffies); bool in_utc_timezone(void); static inline usec_t usec_add(usec_t a, usec_t b) { - usec_t c; /* Adds two time values, and makes sure USEC_INFINITY as input results as USEC_INFINITY in output, and doesn't * overflow. */ - c = a + b; - if (c < a || c < b) /* overflow check */ + if (a > USEC_INFINITY - b) /* overflow check */ return USEC_INFINITY; - return c; + return a + b; } static inline usec_t usec_sub_unsigned(usec_t timestamp, usec_t delta) {