From ccc2a258446e098e5d41c00cf14c525cb70b8577 Mon Sep 17 00:00:00 2001 From: Paolo Molaro Date: Wed, 7 Dec 2011 15:00:54 +0100 Subject: [PATCH] Fix overflow in the win32 monotonic time (xambug #183). --- mono/utils/mono-time.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/mono/utils/mono-time.c b/mono/utils/mono-time.c index 14bde6bfe63..5d926a930bb 100644 --- a/mono/utils/mono-time.c +++ b/mono/utils/mono-time.c @@ -25,12 +25,20 @@ gint64 mono_100ns_ticks (void) { static LARGE_INTEGER freq; + static UINT64 start_time; + UINT64 cur_time; LARGE_INTEGER value; - if (!freq.QuadPart && !QueryPerformanceFrequency (&freq)) - return mono_100ns_datetime (); + if (!freq.QuadPart) { + if (!QueryPerformanceFrequency (&freq)) + return mono_100ns_datetime (); + QueryPerformanceCounter (&value); + start_time = value.QuadPart; + } QueryPerformanceCounter (&value); - return value.QuadPart * MTICKS_PER_SEC / freq.QuadPart; + cur_time = value.QuadPart; + /* we use unsigned numbers and return the difference to avoid overflows */ + return (cur_time - start_time) * MTICKS_PER_SEC / freq.QuadPart; } /* -- 2.25.1