-Wswitch vs -Wuninitialized
Andrew Cagney
ac131313@cygnus.com
Sun Nov 19 20:16:00 GMT 2000
Hello,
I'm looking at the interaction between -Wswitch and -Wuninitialized.
Given the ``buggy'' code:
code#1
enum e { E_A, E_B, E_C };
int
foo (enum e ev)
{
int i;
switch (ev)
{
case E_A: i = 1; break;
case E_B: i = 2; break;
}
return i;
}
GCC (2.95.2 19991024) reports:
enumeration value `E_C' not handled in switch
`i' might be used uninitialized in this function
Adding the missing enum vis:
code#2
...
switch (ev)
{
case E_A: i = 1; break;
case E_B: i = 2; break;
case E_C: i = 3; break;
}
...
still gives the warning:
`i' might be used uninitialized in this function
If instead a ``default:'' case had been present:
code#3
switch (ev)
{
case E_A: i = 1; break;
case E_B: i = 2; break;
default: abort (); /* bad switch */
}
the -Wuninitialized issue is addressed (GCC knows that all branches are
covered). Unfortunatly, it also supresses the -Wswitch warning - which
may still be a second bug.
Has anyone looked at refining the way -Wswitch works so that it is still
useful in cases such as the above? Alternativly, is there a suggested
coding pratice that addreses this?
enjoy,
Andrew
More information about the Gcc
mailing list