Fix overflow in the win32 monotonic time (xambug #183).
authorPaolo Molaro <lupus@oddwiz.org>
Wed, 7 Dec 2011 14:00:54 +0000 (15:00 +0100)
committerPaolo Molaro <lupus@oddwiz.org>
Wed, 7 Dec 2011 14:02:20 +0000 (15:02 +0100)
mono/utils/mono-time.c

index 14bde6bfe63b4b2a928f305d719286cb5ea4fe99..5d926a930bb2fb8d454cefcdb930ba7c83462f7f 100644 (file)
@@ -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;
 }
 
 /*