]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: timeval_add/sub_usecs() - Add assert to make sure negative values aren't used
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Sat, 17 Aug 2019 10:43:21 +0000 (13:43 +0300)
committerAki Tuomi <aki.tuomi@open-xchange.com>
Mon, 19 Aug 2019 08:51:27 +0000 (08:51 +0000)
The current code doesn't work correctly if negative values are used.
The code could of course be changed to handle them, but maybe assert is
better to catch bugs.

src/lib/time-util.h

index f86fa1b73095cfdca6d7daf86b79444e3ea21866..33ddbd9c46349477454ac72b1a4aa71f12465b33 100644 (file)
@@ -17,6 +17,7 @@ long long timeval_diff_usecs(const struct timeval *tv1,
 static inline void
 timeval_add_usecs(struct timeval *tv, long long usecs)
 {
+       i_assert(usecs >= 0);
        tv->tv_sec += usecs / 1000000;
        tv->tv_usec += (usecs % 1000000);
        if (tv->tv_usec >= 1000000) {
@@ -28,6 +29,7 @@ timeval_add_usecs(struct timeval *tv, long long usecs)
 static inline void
 timeval_sub_usecs(struct timeval *tv, long long usecs)
 {
+       i_assert(usecs >= 0);
        tv->tv_sec -= usecs / 1000000;
        tv->tv_usec -= (usecs % 1000000);
        if (tv->tv_usec < 0) {