]> Savannah Git Hosting - gnulib.git/commitdiff
gettime-res: fix unlikely overflow bug
authorPaul Eggert <eggert@cs.ucla.edu>
Sat, 26 Feb 2022 21:13:09 +0000 (13:13 -0800)
committerBruno Haible <bruno@clisp.org>
Wed, 31 Aug 2022 22:51:09 +0000 (00:51 +0200)
* lib/gettime-res.c (gettime_res): Fix bug when hz * tv_sec overflows.
With 64-bit ‘long’ and nanosecond resolution the bug can occur
starting in the year 2262, with probability about 2e-9.
With 32-bit ‘long’ the bug can occur now, with same probability.
The probability goes up on hosts with worse timestamp resolution.

ChangeLog
lib/gettime-res.c

index b1e7012a1d8a4046d0bf73dd277f918531a550ad..7fb6ba1bbcd1ab79aac7fd1877dddbc67ff6a31c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2022-02-26  Paul Eggert  <eggert@cs.ucla.edu>
+
+       gettime-res: fix unlikely overflow bug
+       * lib/gettime-res.c (gettime_res): Fix bug when hz * tv_sec overflows.
+       With 64-bit ‘long’ and nanosecond resolution the bug can occur
+       starting in the year 2262, with probability about 2e-9.
+       With 32-bit ‘long’ the bug can occur now, with same probability.
+       The probability goes up on hosts with worse timestamp resolution.
+
 2022-02-25  Darshit Shah  <darnir@gnu.org>
 
        modules/unicase/special-casing: Fix compilation error
index 9ed5b93c7db62b5a21198cfd34114e21d2fdeaf2..24baf6b40d0961736ec72d145adf07dfca2ccbed 100644 (file)
@@ -64,7 +64,7 @@ gettime_res (void)
   for (int i = 0; 1 < r && i < 32; i++)
     {
       struct timespec now = current_timespec ();
-      r = gcd (r, now.tv_nsec ? now.tv_nsec : hz * now.tv_sec);
+      r = gcd (r, now.tv_nsec ? now.tv_nsec : hz);
     }
 
   return r;