This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/56250] New: Wrong constant folding on unsigned int
- From: "ishiura-compiler at ml dot kwansei.ac.jp" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Fri, 08 Feb 2013 01:39:39 +0000
- Subject: [Bug c/56250] New: Wrong constant folding on unsigned int
- Auto-submitted: auto-generated
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56250
Bug #: 56250
Summary: Wrong constant folding on unsigned int
Classification: Unclassified
Product: gcc
Version: 4.7.2
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
AssignedTo: unassigned@gcc.gnu.org
ReportedBy: ishiura-compiler@ml.kwansei.ac.jp
GCC 4.7.2 and 4.8.0 miscompile the following code where UINT_MAX ==
4294967295.
$ cat bug.c
int main(void)
{
unsigned x = 2;
return ((unsigned) 0 - ( x / 2 ) ) / 2;
}
$ gcc bug.c
$ ./a.out
$ echo $?
0
Unsigned (0-(x/2))/2 should evaluate to 2147483647 instead of 0.
The following code with variable x replaced by "(unsigned) 2" is
correctly compiled.
$ cat nobug.c
int main(void)
{
return ((unsigned) 0 - ( (unsigned) 2 / 2 ) ) / 2;
}
$ gcc nobug.c
$ ./a.out
$ echo $?
255
The miscompile seems to occur on many versions of gcc (4.2 - 4.8, and
even 4.0 and 3.1 on i686-apple-darwin, 4.4 - 4.6 on
i686-linux-gnu-gcc, etc.)