https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87038
Bug ID: 87038
Summary: diagnostics: Please add warning for jumping over
initializers with switch/case in C mode
Product: gcc
Version: 8.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: steinar+gcc at gunderson dot no
Target Milestone: ---
The other day, I had to debug (production!) code that essentially did this:
void func(int x) {
switch (x) {
case 1: {
int foo = 3;
case 0:
printf("foo is %d\n", foo);
}
}
}
If func is called with 0, “foo” is uninitialized, even though it doesn't look
like it. There's a similar construction with goto:
void func2(int x) {
if (x == 0) goto lbl;
int foo = 3;
lbl:
printf("foo is %d\n", foo);
}
In C++ mode, both constructs are rightfully rejected as an error. However, when
compiling as C, there's not even a warning; sometimes, control flow analysis
may detect that foo is used uninitialized, but not always.
Would it be possible to have a warning in C mode that mirrors the C++ error?
Ie. “jump to case label crosses initialization of 'int foo'”.