]> Savannah Git Hosting - gnulib.git/commitdiff
gettime-res: help the compiler
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 2 May 2022 16:52:48 +0000 (09:52 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Mon, 2 May 2022 16:53:39 +0000 (09:53 -0700)
* lib/gettime-res.c (gettime_res): Pacify GCC versions that
incorrectly complain about earlier.tv_sec not being initialized.
Let GCC know that gcd args are always positive.

ChangeLog
lib/gettime-res.c

index f0c9d331d4daaf9e7a7e66d9380594c193b22123..02be5e231747d250eff8b4c38e92b33adffc68ed 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2022-05-02  Paul Eggert  <eggert@cs.ucla.edu>
 
+       gettime-res: help the compiler
+       * lib/gettime-res.c (gettime_res): Pacify GCC versions that
+       incorrectly complain about earlier.tv_sec not being initialized.
+       Let GCC know that gcd args are always positive.
+
        af_alg: port to Ubuntu 22.04
        Without this patch, maintainer builds of coreutils fail on Ubuntu
        22.04 with diagnostics like "./lib/gl_openssl.h:79:1: error:
index bb4d0b191d72f69978177242f9c01dda25244f93..0a14cd360f3e87113ab231d455d40f51a73c33c7 100644 (file)
@@ -52,14 +52,13 @@ gettime_res (void)
   /* On all Gnulib platforms the following calculations do not overflow.  */
 
   long int hz = TIMESPEC_HZ;
-  long int r = hz * res.tv_sec + res.tv_nsec;
-  struct timespec earlier;
-  earlier.tv_nsec = -1;
+  long int r = res.tv_nsec <= 0 ? hz : res.tv_nsec;
+  struct timespec earlier = { .tv_nsec = -1 };
 
   /* On some platforms, clock_getres (CLOCK_REALTIME, ...) yields a
      too-large resolution, under the mistaken theory that it should
      return the timer interval.  For example, on AIX 7.2 POWER8
-     clock_getres yields 10 ms even though clock_gettime yields 1 µs
+     clock_getres yields 10 ms even though clock_gettime yields 1 μs
      resolution.  Work around the problem with high probability by
      trying clock_gettime several times and observing the resulting
      bounds on resolution.  */
@@ -79,7 +78,8 @@ gettime_res (void)
         }
       earlier = now;
 
-      r = gcd (r, now.tv_nsec ? now.tv_nsec : hz);
+      if (0 < now.tv_nsec)
+        r = gcd (r, now.tv_nsec);
     }
 
   return r;