From 422444acf3a519b75709fb14c02118b75ecbc898 Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Fri, 19 Jul 2024 17:37:04 +0200 Subject: [PATCH] Fix overflow; "Timer: gettimeofday is broken" The rounding misbehaved when the tv_usec calculation overflows. Fixes issue #1782. --- common/rfb/Timer.cxx | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/common/rfb/Timer.cxx b/common/rfb/Timer.cxx index 4654d50c8d..fbc9bae95e 100644 --- a/common/rfb/Timer.cxx +++ b/common/rfb/Timer.cxx @@ -54,7 +54,10 @@ inline static timeval addMillis(timeval inTime, int millis) { } inline static int diffTimeMillis(timeval later, timeval earlier) { - return ((later.tv_sec - earlier.tv_sec) * 1000) + ((later.tv_usec - earlier.tv_usec + 999) / 1000); + long udiff; + udiff = ((later.tv_sec - earlier.tv_sec) * 1000000) + + (later.tv_usec - earlier.tv_usec); + return (udiff + 999) / 1000; } std::list Timer::pending;