[COMMITTED, PATCH] readelf: Avoid may be used uninitialized warning from GCC 14

"H.J. Lu" <[email protected]>
Newsgroups gmane.comp.gnu.binutils
Message-ID <CAMe9rOqobP0weYWRB-5Fp5Dns5wy87NN8FjPsdx2aDsfqjbfHw@mail.gmail.com>
On Thu, Jul 2, 2026 at 6:21 AM H.J. Lu <[email protected]> wrote:
>
> On Wed, Jul 1, 2026 at 2:21 PM H.J. Lu <[email protected]> wrote:
> >
> > PR binutils/34324
> > * readelf.c (process_relocs): Return error on missing DT_REL and
> > DT_RELA dynamic tags.
> >
>
> I am checking it in as an obvious fix.

GCC 14 fails to recognize that rel_entsz and entsz_name are unused when
process_relocs returns early for error:

.../binutils/readelf.c:10127:18: error: ‘rel_entsz’ may be used
uninitialized [-Werror=maybe-uninitialized]
10127 |               if (rel_entsz == 0)
      |                  ^
...
.../binutils/readelf.c:10129:19: error: ‘entsz_name’ may be used
uninitialized [-Werror=maybe-uninitialized]
10129 |                   printf (_("<missing or corrupt dynamic tag: %s>\n"),
      |                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10130 |                           entsz_name);

Clear them before returning error.  Also replace reltype_unknown with
default since GCC 14 fails to see that reltype_unknown is the only
unhandled value for relocation_type enum.

PR binutils/34324
* readelf.c (process_relocs): Clear rel_entsz and entsz_name
before returning error on missing DT_REL and DT_RELA dynamic
tags.

-- 
H.J.
0001-readelf-Avoid-may-be-used-uninitialized-warning-from.patch (text/x-patch, 1.9 KB)
From c1c4795df872f48b7f3659cae5974a6ae4f917ea Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <[email protected]>
Date: Thu, 2 Jul 2026 07:02:48 +0800
Subject: [PATCH] readelf: Avoid may be used uninitialized warning from GCC 14
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

GCC 14 fails to recognize that rel_entsz and entsz_name are unused when
process_relocs returns early for error:

.../binutils/readelf.c:10127:18: error: ‘rel_entsz’ may be used uninitialized [-Werror=maybe-uninitialized]
10127 |               if (rel_entsz == 0)
      |                  ^
...
.../binutils/readelf.c:10129:19: error: ‘entsz_name’ may be used uninitialized [-Werror=maybe-uninitialized]
10129 |                   printf (_("<missing or corrupt dynamic tag: %s>\n"),
      |                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10130 |                           entsz_name);

Clear them before returning error.  Also replace reltype_unknown with
default since GCC 14 fails to see that reltype_unknown is the only
unhandled value for relocation_type enum.

	PR binutils/34324
	* readelf.c (process_relocs): Clear rel_entsz and entsz_name
	before returning error on missing DT_REL and DT_RELA dynamic
	tags.

Signed-off-by: H.J. Lu <[email protected]>
---
 binutils/readelf.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/binutils/readelf.c b/binutils/readelf.c
index 33b95da8b7e..59b9d906f08 100644
--- a/binutils/readelf.c
+++ b/binutils/readelf.c
@@ -10080,8 +10080,11 @@ process_relocs (Filedata * filedata)
 
 	  switch (rel_type)
 	    {
-	    case reltype_unknown:
+	    default:
 	      error (_("missing DT_REL and DT_RELA dynamic tags\n"));
+	      /* Avoid may be used uninitialized warning from GCC 14.  */
+	      rel_entsz = 0;
+	      entsz_name = NULL;
 	      return false;
 	    case reltype_rel:
 	      rel_entsz = filedata->dynamic_info[DT_RELENT];
-- 
2.54.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.