Bug 47937 - possible integer bug
Summary: possible integer bug
Status: RESOLVED DUPLICATE of bug 35634
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.6.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-03-01 07:27 UTC by John Regehr
Modified: 2011-03-01 07:35 UTC (History)
0 users

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work:
Known to fail:
Last reconfirmed:


Attachments
test case (175 bytes, text/x-csrc)
2011-03-01 07:27 UTC, John Regehr
Details

Note You need to log in before you can comment on or make changes to this bug.
Description John Regehr 2011-03-01 07:27:56 UTC
Created attachment 23498 [details]
test case

The attached program, compiled using r170512 on x86, produces the following output:

regehr@home:~$ current-gcc -O overflow.c -o overflow
regehr@home:~$ ./overflow 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 

I believe this output is correct.  However, at -O2 the function foo() is compiled to return constant true:

foo:
	movl	$1, %eax
	ret

I believe this is a miscompilation.  The C99 standard says:

  The expression ++E is equivalent to (E+=1).

Thus, x is promoted to integer before being incremented, and so no signed overflow can happen.  Therefore, foo() must check for the case where the incremented integer is truncated when cast back to char, instead of simply returning true.
Comment 1 Andrew Pinski 2011-03-01 07:35:21 UTC
dup of old bug.

*** This bug has been marked as a duplicate of bug 35634 ***