timeval: Initialize 'unix_epoch' for Windows correctly.
authorGurucharan Shetty <gshetty@nicira.com>
Thu, 26 Jun 2014 04:27:36 +0000 (21:27 -0700)
committerGurucharan Shetty <gshetty@nicira.com>
Wed, 9 Jul 2014 23:30:35 +0000 (16:30 -0700)
Till now, we were initializing 'unix_epoch' through time_init().
But if there was a call directly to xgettimeofday(), we would
miss the initialization causing overflows. This commit fixes it
by pre-calculating the value and assigning it globally.

Also add-in a missing return statement.

Reported-by: Alessandro Pilotti <apilotti@cloudbasesolutions.com>
Reported-by: Linda Sun <lsun@vmware.com>
Signed-off-by: Gurucharan Shetty <gshetty@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
lib/timeval.c

index fec3cfa..66732b6 100644 (file)
@@ -53,7 +53,7 @@ typedef unsigned int clockid_t;
 #endif
 
 /* Number of 100 ns intervals from January 1, 1601 till January 1, 1970. */
-static ULARGE_INTEGER unix_epoch;
+const static unsigned long long unix_epoch = 116444736000000000;
 #endif /* _WIN32 */
 
 /* Structure set by unixctl time/warp command. */
@@ -123,16 +123,6 @@ do_init_time(void)
 {
     struct timespec ts;
 
-#ifdef _WIN32
-    /* Calculate number of 100-nanosecond intervals till 01/01/1970. */
-    SYSTEMTIME unix_epoch_st = { 1970, 1, 0, 1, 0, 0, 0, 0};
-    FILETIME unix_epoch_ft;
-
-    SystemTimeToFileTime(&unix_epoch_st, &unix_epoch_ft);
-    unix_epoch.LowPart = unix_epoch_ft.dwLowDateTime;
-    unix_epoch.HighPart = unix_epoch_ft.dwHighDateTime;
-#endif
-
     coverage_init();
 
     init_clock(&monotonic_clock, (!clock_gettime(CLOCK_MONOTONIC, &ts)
@@ -416,12 +406,14 @@ clock_gettime(clock_t id, struct timespec *ts)
         ULARGE_INTEGER current_time = xgetfiletime();
 
         /* Time from Epoch to now. */
-        ts->tv_sec = (current_time.QuadPart - unix_epoch.QuadPart) / 10000000;
-        ts->tv_nsec = ((current_time.QuadPart - unix_epoch.QuadPart) %
+        ts->tv_sec = (current_time.QuadPart - unix_epoch) / 10000000;
+        ts->tv_nsec = ((current_time.QuadPart - unix_epoch) %
                        10000000) * 100;
     } else {
         return -1;
     }
+
+    return 0;
 }
 #endif /* _WIN32 */
 
@@ -435,8 +427,8 @@ xgettimeofday(struct timeval *tv)
 #else
     ULARGE_INTEGER current_time = xgetfiletime();
 
-    tv->tv_sec = (current_time.QuadPart - unix_epoch.QuadPart) / 10000000;
-    tv->tv_usec = ((current_time.QuadPart - unix_epoch.QuadPart) %
+    tv->tv_sec = (current_time.QuadPart - unix_epoch) / 10000000;
+    tv->tv_usec = ((current_time.QuadPart - unix_epoch) %
                    10000000) / 10;
 #endif
 }