From: Paul Eggert Date: Sat, 26 Feb 2022 21:13:09 +0000 (-0800) Subject: gettime-res: fix unlikely overflow bug X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=0c17bd232d20413b2bca83c5370fb7b0b6f725fe;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 b1e7012a1d..7fb6ba1bbc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +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. + 2022-02-25 Darshit Shah modules/unicase/special-casing: Fix compilation error diff --git a/lib/gettime-res.c b/lib/gettime-res.c index 9ed5b93c7d..24baf6b40d 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;