Subject: Re: bug#72842: Compile error due to lto warning on Arch Linux -Werror=maybe-uninitialized

From: Pádraig Brady <[email-masked]>
Date: Wed, 28 Aug 2024 12:37:15 +0100

On 27/08/2024 22:34, Paul Eggert wrote:
> I'm not seeing that false alarm when building coreutils v9.5 on Ubuntu
> 24.04 LTS with "./configure --enable-gcc-warnings CC=gcc-14".
> 
> What GCC version are you using? If it's not GCC 14, try upgrading. I'm
> using gcc-14 (Ubuntu 14-20240412-0ubuntu1) 14.0.1 20240412
> (experimental) [master r14-9935-g67e1433a94f].

I think LTO is key here, as GCC has more context then to produce warnings.
With `-march=native -O3 -flto` on GCC 14.2.1 I see,
2 false warnings in gnulib (patch attached).
1 correct warning in coreutils (patch attached).

Marking this as done.
I'll apply the patches later today.

cheers,
Pádraig.

From 264edcf263bff02aebfe67f1d7343fbf0e96b5de Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <[email-masked]>
Date: Wed, 28 Aug 2024 12:10:43 +0100
Subject: [PATCH] avoid GCC -Wmaybe-uninitialized false positives with LTO

Avoids false warnings with GCC 14.2.1 with -flto

* lib/canonicalize.c: Initialize END_IDX.
* lib/getndelim2.c: Initicalise C.
---
 ChangeLog          | 8 ++++++++
 lib/canonicalize.c | 9 ++++++++-
 lib/getndelim2.c   | 8 +++++---
 3 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 8a7f812b67..fdcc79134e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2024-08-28  Pádraig Brady  <[email-masked]>
+
+	avoid GCC -Wmaybe-uninitialized false positives with LTO
+	Avoids false warnings with GCC 14.2.1 with -flto
+
+	* lib/canonicalize.c: Initialize END_IDX.
+	* lib/getndelim2.c: Initicalise C.
+
 2024-08-28  Bruno Haible  <[email-masked]>
 
 	doc: Add more details about O_EXEC and O_SEARCH.
diff --git a/lib/canonicalize.c b/lib/canonicalize.c
index 7d2a629024..2572b40558 100644
--- a/lib/canonicalize.c
+++ b/lib/canonicalize.c
@@ -34,6 +34,13 @@
 #include "hash-triple.h"
 #include "xalloc.h"
 
+/* Suppress bogus GCC -Wmaybe-uninitialized warnings.  */
+#if defined GCC_LINT || defined lint
+# define IF_LINT(Code) Code
+#else
+# define IF_LINT(Code) /* empty */
+#endif
+
 #ifndef DOUBLE_SLASH_IS_DISTINCT_ROOT
 # define DOUBLE_SLASH_IS_DISTINCT_ROOT false
 #endif
@@ -367,7 +374,7 @@ canonicalize_filename_mode_stk (const char *name, canonicalize_mode_t can_mode,
               buf[n] = '\0';
 
               char *extra_buf = bufs->extra.data;
-              idx_t end_idx;
+              idx_t end_idx IF_LINT (= 0);
               if (end_in_extra_buffer)
                 end_idx = end - extra_buf;
               size_t len = strlen (end);
diff --git a/lib/getndelim2.c b/lib/getndelim2.c
index 89989aefdd..db61e2a5e6 100644
--- a/lib/getndelim2.c
+++ b/lib/getndelim2.c
@@ -47,8 +47,10 @@
 #include "memchr2.h"
 
 /* Avoid false GCC warning "'c' may be used uninitialized".  */
-#if _GL_GNUC_PREREQ (4, 7)
-# pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
+#if defined GCC_LINT || defined lint
+# define IF_LINT(Code) Code
+#else
+# define IF_LINT(Code) /* empty */
 #endif
 
 /* The maximum value that getndelim2 can return without suffering from
@@ -102,7 +104,7 @@ getndelim2 (char **lineptr, size_t *linesize, size_t offset, size_t nmax,
       /* Here always ptr + size == read_pos + nbytes_avail.
          Also nbytes_avail > 0 || size < nmax.  */
 
-      int c;
+      int c IF_LINT (= EOF);
       const char *buffer;
       size_t buffer_len;
 
-- 
2.46.0

From 9b9763e6a7499d78373fb42bac3dc2ee68a14b69 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <[email-masked]>
Date: Wed, 28 Aug 2024 12:33:17 +0100
Subject: [PATCH] all: fix error checking in gl/lib/xdectoint.c

This issue was noticed with -flto on GCC 14.2.1

* gl/lib/xdectoint.c (__xnumtoint): Only inspect the
returned value if LONGINT_INVALID is not set,
as the returned value is uninitialized in that case.
Fixes https://bugs.gnu.org/72842
---
 gl/lib/xdectoint.c | 37 ++++++++++++++++++++-----------------
 1 file changed, 20 insertions(+), 17 deletions(-)

diff --git a/gl/lib/xdectoint.c b/gl/lib/xdectoint.c
index bb38431f3..05534701c 100644
--- a/gl/lib/xdectoint.c
+++ b/gl/lib/xdectoint.c
@@ -49,24 +49,27 @@ __xnumtoint (char const *n_str, int base, __xdectoint_t min, __xdectoint_t max,
   /* Errno value to report if there is an overflow.  */
   int overflow_errno;
 
-  if (tnum < min)
+  if (s_err != LONGINT_INVALID)
     {
-      r = min;
-      overflow_errno = flags & XTOINT_MIN_RANGE ? ERANGE : EOVERFLOW;
-      if (s_err == LONGINT_OK)
-        s_err = LONGINT_OVERFLOW;
-    }
-  else if (max < tnum)
-    {
-      r = max;
-      overflow_errno = flags & XTOINT_MAX_RANGE ? ERANGE : EOVERFLOW;
-      if (s_err == LONGINT_OK)
-        s_err = LONGINT_OVERFLOW;
-    }
-  else
-    {
-      r = tnum;
-      overflow_errno = EOVERFLOW;
+      if (tnum < min)
+        {
+          r = min;
+          overflow_errno = flags & XTOINT_MIN_RANGE ? ERANGE : EOVERFLOW;
+          if (s_err == LONGINT_OK)
+            s_err = LONGINT_OVERFLOW;
+        }
+      else if (max < tnum)
+        {
+          r = max;
+          overflow_errno = flags & XTOINT_MAX_RANGE ? ERANGE : EOVERFLOW;
+          if (s_err == LONGINT_OK)
+            s_err = LONGINT_OVERFLOW;
+        }
+      else
+        {
+          r = tnum;
+          overflow_errno = EOVERFLOW;
+        }
     }
 
   int e = s_err == LONGINT_OVERFLOW ? overflow_errno : 0;
-- 
2.46.0