public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/56600] New: loop goes indefinite when non-loop integer variable overflows
@ 2013-03-12  8:57 mskyeong at naver dot com
  2013-03-12  9:10 ` [Bug c/56600] " schwab@linux-m68k.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: mskyeong at naver dot com @ 2013-03-12  8:57 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56600

             Bug #: 56600
           Summary: loop goes indefinite when non-loop integer variable
                    overflows
    Classification: Unclassified
           Product: gcc
           Version: 4.7.2
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: mskyeong@naver.com


Created attachment 29648
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=29648
intermediate file

Hi, the following code compiles to infinite loop specifically with -O2 on gcc
4.7.2.

#include <stdio.h>

int main (int nargs, char** vargs){
    int i=0x7ffffffd;
    int j=0;
    for (j=0; j<4; ++j){
        ++i;
        printf("%i ",i>0);
        //printf("%i ",j);
        printf("%i\n",i);
    }
    return 0;
}

This may seem related to bug 52833, but it is actually quite different.

With -O1, the output is as expected.

1 2147483646
1 2147483647
0 -2147483648
0 -2147483647

With -O3, I assume the expression 'i>0' replaced with 1 and the loop was
unrolled. i>0 being 1 is no problem, as overflow is an undefined behavior.

1 2147483646
1 2147483647
1 -2147483648
1 -2147483647

But with -O2, it starts as above, and then goes on indefinitely! Since the loop
variable j is not overflowing or modified anywhere else, I think that this bug
may have serious implications on loop optimization. j is falsely being ignored.
Therefore I marked this bug as major.

Moreover, the result becomes the same as -O3 when (1) the printf line is
uncommented, and/or (2) i does not overflow.


FINDINGS:

the intermediate file is exactly the same with all of -O1, -O2 and -O3.

infinite loop does not happen on an older version of gcc (4.4.6).


CONDITIONS:

CPU: Intel i7-2620M (laptop)
gcc.x86_64 version 4.7.2-2.fc17 from Fedora repository
kernel 3.7.9-104.fc17.x86_64

$ gcc -v -O2 bug.c
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.7.2/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla
--enable-bootstrap --enable-shared --enable-threads=posix
--enable-checking=release --disable-build-with-cxx
--disable-build-poststage1-with-cxx --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions --enable-gnu-unique-object
--enable-linker-build-id --with-linker-hash-style=gnu
--enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin
--enable-initfini-array --enable-java-awt=gtk --disable-dssi
--with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre
--enable-libgcj-multifile --enable-java-maintainer-mode
--with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib
--with-ppl --with-cloog --with-tune=generic --with-arch_32=i686
--build=x86_64-redhat-linux
Thread model: posix
gcc version 4.7.2 20120921 (Red Hat 4.7.2-2) (GCC) 
COLLECT_GCC_OPTIONS='-v' '-O2' '-mtune=generic' '-march=x86-64'
 /usr/libexec/gcc/x86_64-redhat-linux/4.7.2/cc1 -quiet -v bug.c -quiet
-dumpbase bug.c -mtune=generic -march=x86-64 -auxbase bug -O2 -version -o
/tmp/ccYIOd07.s
GNU C (GCC) version 4.7.2 20120921 (Red Hat 4.7.2-2) (x86_64-redhat-linux)
        compiled by GNU C version 4.7.2 20120921 (Red Hat 4.7.2-2), GMP version
5.0.2, MPFR version 3.1.0, MPC version 0.9
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-redhat-linux/4.7.2/include-fixed"
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../x86_64-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-redhat-linux/4.7.2/include
 /usr/local/include
 /usr/include
End of search list.
GNU C (GCC) version 4.7.2 20120921 (Red Hat 4.7.2-2) (x86_64-redhat-linux)
        compiled by GNU C version 4.7.2 20120921 (Red Hat 4.7.2-2), GMP version
5.0.2, MPFR version 3.1.0, MPC version 0.9
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 1ddc7c1a4fba2159f711c3e228ac3970
COLLECT_GCC_OPTIONS='-v' '-O2' '-mtune=generic' '-march=x86-64'
 as -v --64 -o /tmp/ccq8Xual.o /tmp/ccYIOd07.s
GNU assembler version 2.22.52.0.1 (x86_64-redhat-linux) using BFD version
version 2.22.52.0.1-10.fc17 20120131
COMPILER_PATH=/usr/libexec/gcc/x86_64-redhat-linux/4.7.2/:/usr/libexec/gcc/x86_64-redhat-linux/4.7.2/:/usr/libexec/gcc/x86_64-redhat-linux/:/usr/lib/gcc/x86_64-redhat-linux/4.7.2/:/usr/lib/gcc/x86_64-redhat-linux/
LIBRARY_PATH=/usr/lib/gcc/x86_64-redhat-linux/4.7.2/:/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-O2' '-mtune=generic' '-march=x86-64'
 /usr/libexec/gcc/x86_64-redhat-linux/4.7.2/collect2 --build-id --no-add-needed
--eh-frame-hdr --hash-style=gnu -m elf_x86_64 -dynamic-linker
/lib64/ld-linux-x86-64.so.2
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/crt1.o
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/crti.o
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/crtbegin.o
-L/usr/lib/gcc/x86_64-redhat-linux/4.7.2
-L/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64 -L/lib/../lib64
-L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../..
/tmp/ccq8Xual.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed
-lgcc_s --no-as-needed /usr/lib/gcc/x86_64-redhat-linux/4.7.2/crtend.o
/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/crtn.o


Thank you!


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/56600] loop goes indefinite when non-loop integer variable overflows
  2013-03-12  8:57 [Bug c/56600] New: loop goes indefinite when non-loop integer variable overflows mskyeong at naver dot com
@ 2013-03-12  9:10 ` schwab@linux-m68k.org
  2013-03-12  9:36 ` jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: schwab@linux-m68k.org @ 2013-03-12  9:10 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56600

Andreas Schwab <schwab@linux-m68k.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID

--- Comment #1 from Andreas Schwab <schwab@linux-m68k.org> 2013-03-12 09:10:33 UTC ---
Integer overflow is causing undefined behaviour.  If you want wraparound
semantics either use unsigned int or -fwrapv.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/56600] loop goes indefinite when non-loop integer variable overflows
  2013-03-12  8:57 [Bug c/56600] New: loop goes indefinite when non-loop integer variable overflows mskyeong at naver dot com
  2013-03-12  9:10 ` [Bug c/56600] " schwab@linux-m68k.org
@ 2013-03-12  9:36 ` jakub at gcc dot gnu.org
  2013-03-16 11:44 ` jasonwucj at gmail dot com
  2013-12-02 19:04 ` Joost.VandeVondele at mat dot ethz.ch
  3 siblings, 0 replies; 5+ messages in thread
From: jakub at gcc dot gnu.org @ 2013-03-12  9:36 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56600

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> 2013-03-12 09:35:38 UTC ---
Related to PR53265, where gcc 4.8+ could warn about this case.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/56600] loop goes indefinite when non-loop integer variable overflows
  2013-03-12  8:57 [Bug c/56600] New: loop goes indefinite when non-loop integer variable overflows mskyeong at naver dot com
  2013-03-12  9:10 ` [Bug c/56600] " schwab@linux-m68k.org
  2013-03-12  9:36 ` jakub at gcc dot gnu.org
@ 2013-03-16 11:44 ` jasonwucj at gmail dot com
  2013-12-02 19:04 ` Joost.VandeVondele at mat dot ethz.ch
  3 siblings, 0 replies; 5+ messages in thread
From: jasonwucj at gmail dot com @ 2013-03-16 11:44 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56600

Chung-Ju Wu <jasonwucj at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jasonwucj at gmail dot com

--- Comment #3 from Chung-Ju Wu <jasonwucj at gmail dot com> 2013-03-16 11:44:03 UTC ---
(In reply to comment #1)
> Integer overflow is causing undefined behaviour.  If you want wraparound
> semantics either use unsigned int or -fwrapv.

Hi, Hyun-Ho Kyeong,

More information about such undefined behavior~ :)

According to C99 standard:

C99 6.3.1.4 Point 1:
"... If the value of the integral part cannot be represented
 by the integer type, the behavior is undefined."

C99 6.5 Point 5:
"... if the result is ... not in the range of representable
 values for its type... the behavior is undefined."


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/56600] loop goes indefinite when non-loop integer variable overflows
  2013-03-12  8:57 [Bug c/56600] New: loop goes indefinite when non-loop integer variable overflows mskyeong at naver dot com
                   ` (2 preceding siblings ...)
  2013-03-16 11:44 ` jasonwucj at gmail dot com
@ 2013-12-02 19:04 ` Joost.VandeVondele at mat dot ethz.ch
  3 siblings, 0 replies; 5+ messages in thread
From: Joost.VandeVondele at mat dot ethz.ch @ 2013-12-02 19:04 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56600

Joost VandeVondele <Joost.VandeVondele at mat dot ethz.ch> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |Joost.VandeVondele at mat dot ethz
                   |                            |.ch
         Resolution|INVALID                     |DUPLICATE

--- Comment #4 from Joost VandeVondele <Joost.VandeVondele at mat dot ethz.ch> ---


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


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2013-12-02 19:04 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-03-12  8:57 [Bug c/56600] New: loop goes indefinite when non-loop integer variable overflows mskyeong at naver dot com
2013-03-12  9:10 ` [Bug c/56600] " schwab@linux-m68k.org
2013-03-12  9:36 ` jakub at gcc dot gnu.org
2013-03-16 11:44 ` jasonwucj at gmail dot com
2013-12-02 19:04 ` Joost.VandeVondele at mat dot ethz.ch

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).