From: Paul Eggert Date: Sat, 26 Feb 2022 21:13:09 +0000 (-0800) Subject: gettime-res: fix unlikely overflow bug X-Git-Tag: v1.0~2344 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=bc25238849ed25a0a16b87f7d4bba3d091c49325;p=gnulib.git 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. --- 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;