mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sam James <sam@gentoo.org>
To: Andrii Nakryiko <andrii@kernel.org>,
	Eduard Zingerman <eddyz87@gmail.com>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	John Fastabend <john.fastabend@gmail.com>,
	KP Singh <kpsingh@kernel.org>,
	Stanislav Fomichev <sdf@fomichev.me>, Hao Luo <haoluo@google.com>,
	Jiri Olsa <jolsa@kernel.org>
Cc: "Jose E . Marchesi" <jose.marchesi@oracle.com>,
	"Andrew Pinski" <quic_apinski@quicinc.com>,
	"Kacper Słomiński" <kacper.slominski72@gmail.com>,
	"Arsen Arsenović" <arsen@gentoo.org>,
	"Sam James" <sam@gentoo.org>,
	bpf@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v3] libbpf: workaround -Wmaybe-uninitialized false positive
Date: Mon, 12 Aug 2024 11:37:59 +0100	[thread overview]
Message-ID: <12cec1262be71de5f1d9eae121b637041a5ae247.1723459079.git.sam@gentoo.org> (raw)

In `elf_close`, we get this with GCC 15 -O3 (at least):
```
In function ‘elf_close’,
    inlined from ‘elf_close’ at elf.c:53:6,
    inlined from ‘elf_find_func_offset_from_file’ at elf.c:384:2:
elf.c:57:9: warning: ‘elf_fd.elf’ may be used uninitialized [-Wmaybe-uninitialized]
   57 |         elf_end(elf_fd->elf);
      |         ^~~~~~~~~~~~~~~~~~~~
elf.c: In function ‘elf_find_func_offset_from_file’:
elf.c:377:23: note: ‘elf_fd.elf’ was declared here
  377 |         struct elf_fd elf_fd;
      |                       ^~~~~~
In function ‘elf_close’,
    inlined from ‘elf_close’ at elf.c:53:6,
    inlined from ‘elf_find_func_offset_from_file’ at elf.c:384:2:
elf.c:58:9: warning: ‘elf_fd.fd’ may be used uninitialized [-Wmaybe-uninitialized]
   58 |         close(elf_fd->fd);
      |         ^~~~~~~~~~~~~~~~~
elf.c: In function ‘elf_find_func_offset_from_file’:
elf.c:377:23: note: ‘elf_fd.fd’ was declared here
  377 |         struct elf_fd elf_fd;
      |                       ^~~~~~
```

In reality, our use is fine, it's just that GCC doesn't model errno
here (see linked GCC bug). Suppress -Wmaybe-uninitialized accordingly
by initializing elf_fd.elf to -1.

I've done this in two other functions as well given it could easily
occur there too (same access/use pattern).

Link: https://gcc.gnu.org/PR114952
Signed-off-by: Sam James <sam@gentoo.org>
---
v3: Initialize to -1 instead of using a pragma.

Range-diff against v2:
1:  8f5c3b173e4cb < -:  ------------- libbpf: workaround -Wmaybe-uninitialized false positive
-:  ------------- > 1:  12cec1262be71 libbpf: workaround -Wmaybe-uninitialized false positive

 tools/lib/bpf/elf.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/tools/lib/bpf/elf.c b/tools/lib/bpf/elf.c
index c92e02394159e..00ea3f867bbc8 100644
--- a/tools/lib/bpf/elf.c
+++ b/tools/lib/bpf/elf.c
@@ -374,7 +374,7 @@ long elf_find_func_offset(Elf *elf, const char *binary_path, const char *name)
  */
 long elf_find_func_offset_from_file(const char *binary_path, const char *name)
 {
-	struct elf_fd elf_fd;
+	struct elf_fd elf_fd = { .fd = -1 };
 	long ret = -ENOENT;
 
 	ret = elf_open(binary_path, &elf_fd);
@@ -412,7 +412,7 @@ int elf_resolve_syms_offsets(const char *binary_path, int cnt,
 	int err = 0, i, cnt_done = 0;
 	unsigned long *offsets;
 	struct symbol *symbols;
-	struct elf_fd elf_fd;
+	struct elf_fd elf_fd = { .fd = -1 };
 
 	err = elf_open(binary_path, &elf_fd);
 	if (err)
@@ -507,7 +507,7 @@ int elf_resolve_pattern_offsets(const char *binary_path, const char *pattern,
 	int sh_types[2] = { SHT_SYMTAB, SHT_DYNSYM };
 	unsigned long *offsets = NULL;
 	size_t cap = 0, cnt = 0;
-	struct elf_fd elf_fd;
+	struct elf_fd elf_fd = { .fd = -1 };
 	int err = 0, i;
 
 	err = elf_open(binary_path, &elf_fd);
-- 
2.45.2


             reply	other threads:[~2024-08-12 10:39 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-12 10:37 Sam James [this message]
2024-08-12 11:27 ` Jiri Olsa
2024-08-12 13:56 ` Alan Maguire
2024-08-12 21:02   ` Andrii Nakryiko
2024-08-13 19:38     ` Sam James

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=12cec1262be71de5f1d9eae121b637041a5ae247.1723459079.git.sam@gentoo.org \
    --to=sam@gentoo.org \
    --cc=andrii@kernel.org \
    --cc=arsen@gentoo.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=jose.marchesi@oracle.com \
    --cc=kacper.slominski72@gmail.com \
    --cc=kpsingh@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=quic_apinski@quicinc.com \
    --cc=sdf@fomichev.me \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®