From 732788db7acd64506a2c6d2432587a1fd6ac309e Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sat, 19 Sep 2009 10:11:19 +0200 Subject: [PATCH] tv_scale(): Avoid integer overflow. Just use an uint64_t and compute everything in microseconds which even simplifies the code a bit. --- time.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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); } /** -- 2.39.2