public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/36862]  New: Optimization O2 causes infinite loop
@ 2008-07-17  9:53 ben at zeus dot com
  2008-07-17  9:54 ` [Bug c/36862] " ben at zeus dot com
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: ben at zeus dot com @ 2008-07-17  9:53 UTC (permalink / raw)
  To: gcc-bugs

Sorry, not sure if this is a real bug or whether the code is broken and relying
on undefined behaviour when an integer overflows. The test case is very simple:

#include <stdio.h>

int main( int argc, char *argv[] )
{
   int bit;
   for( bit = 1; bit; bit *= 2 ) {
      printf( "%d\n", bit );
   }
   return 0;
}

Compiling this with just the argument '-O2' on GCC 4.3.1 will cause this loop
to run forever, printing out an endless stream of zeros. Lower levels of
optimization don't hit this problem. Changing the type of 'bit' to unsigned
also prevents an infinite loop. Earlier GCCs (GCC 4.2.3 and earlier) did not
have this behaviour.


-- 
           Summary: Optimization O2 causes infinite loop
           Product: gcc
           Version: 4.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: ben at zeus dot com


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


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

* [Bug c/36862] Optimization O2 causes infinite loop
  2008-07-17  9:53 [Bug c/36862] New: Optimization O2 causes infinite loop ben at zeus dot com
@ 2008-07-17  9:54 ` ben at zeus dot com
  2008-07-17 10:06 ` paolo dot carlini at oracle dot com
  2008-07-17 10:22 ` brian at dessent dot net
  2 siblings, 0 replies; 4+ messages in thread
From: ben at zeus dot com @ 2008-07-17  9:54 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from ben at zeus dot com  2008-07-17 09:53 -------
Sorry, forgot to include any compiler details:

[hera ben]$ /usr/local/gcc-4.3/bin/gcc -O2 -v -save-temps loop.c                

Using built-in specs.

Target: i686-pc-linux-gnu

Configured with: ../gcc-4.3.1/configure --prefix=/usr/local/gcc-4.3.1
--with-gmp=/usr/local/gmp-4.2.2 --with-mpfr=/usr/local/mpfr-2.3.1 :
(reconfigured) ../gcc-4.3.1/configure --prefix=/usr/local/gcc-4.3.1
--with-gmp=/usr/local/gmp-4.2.2 --with-mpfr=/usr/local/mpfr-2.3.1

Thread model: posix

gcc version 4.3.1 (GCC) 

COLLECT_GCC_OPTIONS='-O2' '-v' '-save-temps' '-mtune=generic'

 /usr/local/gcc-4.3.1/libexec/gcc/i686-pc-linux-gnu/4.3.1/cc1 -E -quiet -v
loop.c -mtune=generic -O2 -fpch-preprocess -o loop.i

ignoring nonexistent directory
"/usr/local/gcc-4.3.1/lib/gcc/i686-pc-linux-gnu/4.3.1/../../../../i686-pc-linux-gnu/include"

#include "..." search starts here:

#include <...> search starts here:

 /usr/local/include

 /usr/local/gcc-4.3.1/include

 /usr/local/gcc-4.3.1/lib/gcc/i686-pc-linux-gnu/4.3.1/include

 /usr/local/gcc-4.3.1/lib/gcc/i686-pc-linux-gnu/4.3.1/include-fixed

 /usr/include

End of search list.

COLLECT_GCC_OPTIONS='-O2' '-v' '-save-temps' '-mtune=generic'

 /usr/local/gcc-4.3.1/libexec/gcc/i686-pc-linux-gnu/4.3.1/cc1 -fpreprocessed
loop.i -quiet -dumpbase loop.c -mtune=generic -auxbase loop -O2 -version -o
loop.s

GNU C (GCC) version 4.3.1 (i686-pc-linux-gnu)

        compiled by GNU C version 4.3.1, GMP version 4.2.2, MPFR version 2.3.1.

GGC heuristics: --param ggc-min-expand=64 --param ggc-min-heapsize=64563

Compiler executable checksum: 93e4b24a169a3647e9429593228b6881

COLLECT_GCC_OPTIONS='-O2' '-v' '-save-temps' '-mtune=generic'

 as -V -Qy -o loop.o loop.s

GNU assembler version 2.15 (i386-linux) using BFD version 2.15

COMPILER_PATH=/usr/local/gcc-4.3.1/libexec/gcc/i686-pc-linux-gnu/4.3.1/:/usr/local/gcc-4.3.1/libexec/gcc/i686-pc-linux-gnu/4.3.1/:/usr/local/gcc-4.3.1/libexec/gcc/i686-pc-linux-gnu/:/usr/local/gcc-4.3.1/lib/gcc/i686-pc-linux-gnu/4.3.1/:/usr/local/gcc-4.3.1/lib/gcc/i686-pc-linux-gnu/

LIBRARY_PATH=/usr/local/gcc-4.3.1/lib/gcc/i686-pc-linux-gnu/4.3.1/:/usr/local/gcc-4.3.1/lib/gcc/i686-pc-linux-gnu/4.3.1/../../../:/lib/:/usr/lib/

COLLECT_GCC_OPTIONS='-O2' '-v' '-save-temps' '-mtune=generic'

 /usr/local/gcc-4.3.1/libexec/gcc/i686-pc-linux-gnu/4.3.1/collect2
--eh-frame-hdr -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 /usr/lib/crt1.o
/usr/lib/crti.o /usr/local/gcc-4.3.1/lib/gcc/i686-pc-linux-gnu/4.3.1/crtbegin.o
-L/usr/local/gcc-4.3.1/lib/gcc/i686-pc-linux-gnu/4.3.1
-L/usr/local/gcc-4.3.1/lib/gcc/i686-pc-linux-gnu/4.3.1/../../.. loop.o -lgcc
--as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed
/usr/local/gcc-4.3.1/lib/gcc/i686-pc-linux-gnu/4.3.1/crtend.o /usr/lib/crtn.o


-- 


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


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

* [Bug c/36862] Optimization O2 causes infinite loop
  2008-07-17  9:53 [Bug c/36862] New: Optimization O2 causes infinite loop ben at zeus dot com
  2008-07-17  9:54 ` [Bug c/36862] " ben at zeus dot com
@ 2008-07-17 10:06 ` paolo dot carlini at oracle dot com
  2008-07-17 10:22 ` brian at dessent dot net
  2 siblings, 0 replies; 4+ messages in thread
From: paolo dot carlini at oracle dot com @ 2008-07-17 10:06 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from paolo dot carlini at oracle dot com  2008-07-17 10:06 -------
(In reply to comment #0)
>    int bit;
>    for( bit = 1; bit; bit *= 2 )

This is pretty straightforward, isn't it? At some point bit - a *signed*
integral type - overflows and this leads to undefined bahavior.


-- 

paolo dot carlini at oracle dot com changed:

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


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


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

* [Bug c/36862] Optimization O2 causes infinite loop
  2008-07-17  9:53 [Bug c/36862] New: Optimization O2 causes infinite loop ben at zeus dot com
  2008-07-17  9:54 ` [Bug c/36862] " ben at zeus dot com
  2008-07-17 10:06 ` paolo dot carlini at oracle dot com
@ 2008-07-17 10:22 ` brian at dessent dot net
  2 siblings, 0 replies; 4+ messages in thread
From: brian at dessent dot net @ 2008-07-17 10:22 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from brian at dessent dot net  2008-07-17 10:21 -------
Subject: Re:   New: Optimization O2 causes infinite loop

Yes, this code is broken as indeed overflow of signed integers is
undefined, so transforming this to an infinite loop is legitimate.  Here
are some links to read:

http://gcc.gnu.org/gcc-4.2/changes.html
http://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#index-fwrapv-1854
http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#index-fstrict_002doverflow-698
http://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wstrict_002doverflow-316
http://www.gnu.org/software/autoconf/manual/html_node/Integer-Overflow.html


-- 


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


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

end of thread, other threads:[~2008-07-17 10:22 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-07-17  9:53 [Bug c/36862] New: Optimization O2 causes infinite loop ben at zeus dot com
2008-07-17  9:54 ` [Bug c/36862] " ben at zeus dot com
2008-07-17 10:06 ` paolo dot carlini at oracle dot com
2008-07-17 10:22 ` brian at dessent dot net

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).