https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99944
Bug ID: 99944
Summary: incorrect maybe-uninitialized warning on variable
defined as an array
Product: gcc
Version: 11.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: vincent-gcc at vinc17 dot net
Target Milestone: ---
Consider the following testcase derived from the initial testcase of PR85777
and its cleaned-up testcase (which is actually a bit different since an enum
was replaced by an int, and it matters here).
int d;
int h(void);
void e1(void)
{
int f[2];
int g = 0;
if (d)
g++;
if (d == 1)
f[g++] = 2;
(void) (f[0] || (g && h()));
}
void e2(void)
{
enum { a } f[2];
int g = 0;
if (d)
g++;
if (d == 1)
f[g++] = a;
(void) (f[0] || (g && h()));
}
With a GCC snapshot built a few hour ago from the master branch:
cventin% gcc --version
gcc (GCC) 11.0.1 20210406 (experimental)
[...]
cventin% gcc -Werror=maybe-uninitialized -O2 -c file.c
file.c: In function ‘e1’:
file.c:11:3: error: ‘f[0]’ may be used uninitialized
[-Werror=maybe-uninitialized]
11 | (void) (f[0] || (g && h()));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
file.c: In function ‘e2’:
file.c:21:3: error: ‘*(unsigned int *)(&f[0])’ may be used uninitialized
[-Werror=maybe-uninitialized]
21 | (void) (f[0] || (g && h()));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1: some warnings being treated as errors
The error for e1 is correct, but not the one for e2 (for e2, previous GCC
versions were outputting ‘f’ instead of ‘*(unsigned int *)(&f[0])’, but this is
about the same thing).
cventin% gcc -Werror=maybe-uninitialized -O2 -c file.c -fsanitize=undefined
file.c: In function ‘e1’:
file.c:11:12: error: ‘f’ may be used uninitialized
[-Werror=maybe-uninitialized]
11 | (void) (f[0] || (g && h()));
| ~^~~
file.c:5:7: note: ‘f’ declared here
5 | int f[2];
| ^
file.c: In function ‘e2’:
file.c:21:12: error: ‘f’ may be used uninitialized
[-Werror=maybe-uninitialized]
21 | (void) (f[0] || (g && h()));
| ~^~~
file.c:15:14: note: ‘f’ declared here
15 | enum { a } f[2];
| ^
cc1: some warnings being treated as errors
Here, with the option -fsanitize=undefined added, both errors are incorrect.