bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] gettime-res: fix unlikely overflow bug


From: Paul Eggert
Subject: [PATCH] gettime-res: fix unlikely overflow bug
Date: Sat, 26 Feb 2022 13:13:51 -0800

* 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         | 7 +++++++
 lib/gettime-res.c | 2 +-
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index 430f81fd39..629ec803fd 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +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.
+
        Document clang -fsanitize=undefined glitch
        * doc/gnulib-intro.texi (Unsupported Platforms):
        Document incompatibility of ‘clang -fsanitize=undefined’
diff --git a/lib/gettime-res.c b/lib/gettime-res.c
index 3cc07de6da..611f83ad27 100644
--- a/lib/gettime-res.c
+++ b/lib/gettime-res.c
@@ -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;
-- 
2.35.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]