http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49234
Summary: -Wstrict-overflow gives obviously unwarranted warning
Product: gcc
Version: 4.7.0
Status: UNCONFIRMED
Severity: minor
Priority: P3
Component: c
AssignedTo: [email protected]
ReportedBy: [email protected]
Given this code, in file k.c, -O2 -Wstrict-overflow evokes a warning.
However, since the only values assigned to "state" are 0, 1 and 2,
gcc should be able to determine that no overflow is possible,
and hence should issue no warning:
char *
trim2 (char *d)
{
int state = 0;
char *r;
int i;
for (i = 0; d[i]; i++)
{
if (state == 0 && d[i] == ' ')
continue;
if (state == 0) /* line 13 */
state = 1;
if (state == 1)
{
state = 2;
r = d + i;
}
}
if (state == 2)
*r = '\0';
return d;
}
$ gcc -O2 -Wstrict-overflow -c k.c
k.c: In function 'trim2':
k.c:13:10: warning: assuming signed overflow does not occur when simplifying
conditional to constant [-Wstrict-overflow]
For the record, until recently I would not have bothered enabling
-Wstrict-overflow, due to the high proportion of false-positives,
but since I've learned about the risk of this bug,
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33498
I am now more inclined to use -Wstrict-overflow in spite of that.