From 75c8701df60956254c8bd57cd9015d72134dc6bf 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. (cherry picked from commit 422444acf3a519b75709fb14c02118b75ecbc898) --- 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 e77a9ac3cc..fc3a54d869 100644 --- a/common/rfb/Timer.cxx +++ b/common/rfb/Timer.cxx @@ -52,7 +52,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;