On 4/23/22 07:35, Bernhard Voelker wrote:
lib/gettime-res.c:77:46: error: 'earlier.tv_sec' may be used uninitialized in 
this function \
[-Werror=maybe-uninitialized]

Thanks for reporting that. Although the unnecessary initialization is annoying, this time I'm not annoyed enough to complicate the code to pacify GCC, so I installed the attached which follows your suggestion.

This patch also lets GCC know that the numbers in question are all positive which I suppose might help code generation. It also replaces a U+00B5 MICRO SIGN with the recommended U+03BC GREEK SMALL LETTER MU.
From 2ef6006ffc4080cf8c0c1f4d4deeb4c357d7a695 Mon Sep 17 00:00:00 2001
From: Paul Eggert <egg...@cs.ucla.edu>
Date: Mon, 2 May 2022 09:52:48 -0700
Subject: [PATCH] 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.
---
 ChangeLog         |  5 +++++
 lib/gettime-res.c | 10 +++++-----
 2 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index f0c9d331d4..02be5e2317 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
 2022-05-02  Paul Eggert  <egg...@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:
diff --git a/lib/gettime-res.c b/lib/gettime-res.c
index bb4d0b191d..0a14cd360f 100644
--- a/lib/gettime-res.c
+++ b/lib/gettime-res.c
@@ -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;
-- 
2.34.1

Reply via email to