http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56463
Bug #: 56463 Summary: infinite loop when having integer overflow in a simple accumulator Classification: Unclassified Product: gcc Version: 4.6.3 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c AssignedTo: unassig...@gcc.gnu.org ReportedBy: nakacri...@hotmail.com A colleague has showed me a problem and I have reduced it to this code: #include <stdio.h> int main() { int i,j; int a=0; for(i=0;i<20000;i++) for(j=0;j<600;j++) a=a+3435345234440; printf("%d\n",a); return 0; } With 'gcc test.c' or 'gcc test.c -O' works correctly, but with 'gcc test.c -O2' fails. More exactly, fails with 'gcc test.c -O -fstrict-overflow -ftree-pre -ftree-vrp'. With the optimization it becomes an infinite loop 00000000004004f4 <main>: 4004f4: b8 58 02 00 00 mov $0x258,%eax 4004f9: ba 58 02 00 00 mov $0x258,%edx 4004fe: 83 e8 01 sub $0x1,%eax 400501: 0f 44 c2 cmove %edx,%eax 400504: eb f8 jmp 4004fe <main+0xa> The overflow could make the variable 'a' to take any value, but never to make the loop infinite. My gcc version: gcc version 4.6.3 (Gentoo 4.6.3 p1.9, pie-0.5.2) I have checked that in gcc-4.5.3 works correctly.