[Bug c/59301] New: Please enable -Wstrict-overflow as part of -Wextra
j at uriah dot heep.sax.de
gcc-bugzilla@gcc.gnu.org
Tue Nov 26 15:32:00 GMT 2013
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59301
Bug ID: 59301
Summary: Please enable -Wstrict-overflow as part of -Wextra
Product: gcc
Version: 4.7.2
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: j at uriah dot heep.sax.de
The -fstrict-overflow behaviour can lead to surprising results. Consider
the following code that came up in a forum, complaining about why GCC
optimizes the first loop into an endless one:
int main (void)
{
int i = 0;
while (--i)
asm("nop");
for (;;);
}
The (obvious, in that short piece of code) expectation of the programmer
was that the NOP is executed a finite number of times (basically, just
"waiting a bit" that way), and the code flow then proceeds to the final
infinite loop.
Instead, the resulting code is an infinite loop around the NOP statement.
(The original question came out for the AVR target, but the behaviour is
completely independent of the actual target.)
Specifying the commonly used -Wall -Wextra options doesn't tell the innocent
programmer the compiler basically already detected some undefined behaviour,
and might have reordered the code due to that undefined behaviour. Only
by specifying -Wstrict-overflow, one gets:
foo.c: In function 'main':
foo.c:4:11: warning: assuming signed overflow does not occur when simplifying
conditional to constant [-Wstrict-overflow]
I think it would be much better to include -Wstrict-overflow into -Wextra,
so people get aware of the potential problems.
More information about the Gcc-bugs
mailing list