From bc25238849ed25a0a16b87f7d4bba3d091c49325 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sat, 26 Feb 2022 13:13:09 -0800 Subject: [PATCH] gettime-res: fix unlikely overflow bug MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * 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 + 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.39.5