https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114952
Bug ID: 114952
Summary: False positive -Wmaybe-uninitialized starting at -O3
in libbpf
Product: gcc
Version: 14.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: kacper.slominski72 at gmail dot com
Target Milestone: ---
Created attachment 58105
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=58105&action=edit
Preprocessed elf.c source code
When compiling libbpf (both as a part of the Linux kernel build, and as a
standalone library), starting with at least GCC 11.1, there are some false
positive -Wmaybe-uninitalized warnings generated when compiling with -O3.
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;
| ^~~~~~
>From a quick look at the source code, as far as I can see, elf_fd is always
initialized at this point (looking at elf_find_func_offset_from_file), and I
think inlining has something to do with this (and hence it doesn't show up at
-O2).
Attached is a preprocessed copy of elf.c. To reproduce, run:
$ gcc preprocessed.c -c -O3 -Wmaybe-uninitialized