From ce992ee01f19e9615f43f6dbbdceb370223a09e6 Mon Sep 17 00:00:00 2001 From: Mozzg Date: Mon, 29 Mar 2021 19:24:55 +0300 Subject: [PATCH] Fix overflow when calculating time value for Clock --- src/SFML/System/Win32/ClockImpl.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/SFML/System/Win32/ClockImpl.cpp b/src/SFML/System/Win32/ClockImpl.cpp index d8d06b57e..2f69ece62 100644 --- a/src/SFML/System/Win32/ClockImpl.cpp +++ b/src/SFML/System/Win32/ClockImpl.cpp @@ -54,9 +54,9 @@ namespace priv //////////////////////////////////////////////////////////// Time ClockImpl::getCurrentTime() { - // Get the frequency of the performance counter - // (it is constant across the program lifetime) - static LARGE_INTEGER frequency = getFrequency(); + // Calculate inverse of frequency multiplied by 1000000 to prevent overflow in final calculation + // Frequency is constant across the program lifetime + static double inverse = 1000000.0 / getFrequency().QuadPart; // Detect if we are on Windows XP or older static bool oldWindows = isWindowsXpOrOlder(); @@ -80,7 +80,7 @@ Time ClockImpl::getCurrentTime() } // Return the current time as microseconds - return sf::microseconds(1000000 * time.QuadPart / frequency.QuadPart); + return sf::microseconds(static_cast(time.QuadPart * inverse)); } } // namespace priv