https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93181
Bug ID: 93181
Summary: -Wuninitialized fails to warn about uninitialized
value; -Wmaybe-uninitialized should also warn.
Product: gcc
Version: 9.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: intvnut at gmail dot com
Target Milestone: ---
GCC 4.1.2 previously warned about p being used uninitialized in code such as
the following:
#include <stddef.h>
void ub_express(int);
void test(void);
struct foo {
int count;
};
extern struct foo a, b;
struct foo a, b;
void ub_express(int x) {
struct foo *p;
if (x == 1) {
p = &a;
}
if (x == 2) {
p = &b;
}
p->count++;
}
void test(void) {
for (int i = 0; i < 3; ++i) {
ub_express(i);
}
}
https://godbolt.org/z/xd8vsL
<source>:21: warning: 'p' is used uninitialized in this function
With every later version of GCC I tried at Godbolt's compiler explorer up
through 9.2.0 and trunk (and the few I tried locally on my workstation), it no
longer warns.
Furthermore, 4.1.2 only warns if I retain the function 'test', as that
definitely includes a code path where 'p' is used uninitialized.
It seems like GCC should warn about ub_express() even if 'test' isn't present
according to the description for -Wmaybe-uninitialized. It's a similar code
pattern to the one that appears in the documentation to -Wmaybe-uninitialized:
{
int x;
switch (y)
{
case 1: x = 1;
break;
case 2: x = 4;
break;
case 3: x = 5;
}
foo (x);
}
I've tagged this as 9.1.0 and "C" as this oversight is present in that version,
but I see similar behavior across many GCC versions and in both the C and C++
compilers.