From: Andre Noll Date: Sat, 19 Sep 2009 08:11:19 +0000 (+0200) Subject: tv_scale(): Avoid integer overflow. X-Git-Tag: v0.3.5~1^2~6 X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=commitdiff_plain;h=732788db7acd64506a2c6d2432587a1fd6ac309e;hp=f21270523b4500797f7976906998128f5ff5da72 tv_scale(): Avoid integer overflow. Just use an uint64_t and compute everything in microseconds which even simplifies the code a bit. --- diff --git a/time.c b/time.c index 6e1d603d..0cb9babe 100644 --- a/time.c +++ b/time.c @@ -106,9 +106,10 @@ void tv_add(const struct timeval *a, const struct timeval *b, void tv_scale(const unsigned long mult, const struct timeval *tv, struct timeval *result) { - result->tv_sec = mult * tv->tv_sec; - result->tv_sec += tv->tv_usec * mult / 1000 / 1000; - result->tv_usec = tv->tv_usec * mult % (1000 * 1000); + uint64_t x = ((uint64_t)tv->tv_sec * 1000 * 1000 + tv->tv_usec) * mult; + + result->tv_sec = x / 1000 / 1000; + result->tv_usec = x % (1000 * 1000); } /**