public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/50922] New: infinite loop when optimized
@ 2011-10-30 15:44 pfister at pci dot uzh.ch
  2011-10-30 16:26 ` [Bug c/50922] " xunxun1982 at gmail dot com
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: pfister at pci dot uzh.ch @ 2011-10-30 15:44 UTC (permalink / raw)
  To: gcc-bugs

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

             Bug #: 50922
           Summary: infinite loop when optimized
    Classification: Unclassified
           Product: gcc
           Version: 4.4.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: pfister@pci.uzh.ch


When a for loop should run 2^31 times, when optimized with -O2 becomes an
infinite loop.
Here the example programm:

#include <stdio.h>

void lcdSendData(unsigned long c)
{
 if((c&0xFFFFFF)==0)
  printf("%02lX ",(c>>24)&0xFF);
}

int main(void)
{
 long x=0;
 unsigned long c;
 for(c=0;c<2147483648UL;c++)
   {
      lcdSendData(x++);
    }
 putc('\n',stdout);
 return 0;
}

and the outputs when compiling and running:

gcc -Wall -O1 loopbug2.c -o loopbug2
rolf@merkur:~/work/test/loopbug$ loopbug2
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19
1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33
34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D
4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 61 62 63 64 65 66 67
68 69 6A 6B 6C 6D 6E 6F 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F
rolf@merkur:~/work/test/loopbug$ gcc -Wall -O2 loopbug2.c -o loopbug2
rolf@merkur:~/work/test/loopbug$ loopbug2
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19
1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33
34 35 36 37 38 39 3A 3B 3C 3D 3E 3F 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D
4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F 60 61 62 63 64 65 66 67
68 69 6A 6B 6C 6D 6E 6F 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F 80 81
82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F 90 91 92 93 94 95 96 97 98 99 9A 9B
9C 9D 9E 9F A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF B0 B1 B2 B3 B4 B5
B6 B7 B8 B9 BA BB BC BD BE BF C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 CA CB CC CD CE CF
D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 DA DB DC DD DE DF E0 E1 E2 E3 E4 E5 E6 E7 E8 E9
EA EB EC ED EE EF F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 FA FB FC FD FE FF 00 01 02 03
04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D
1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37
38 39 3A 3B 3C 3D 3E 3F 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51
52 53 54 5^C
rolf@merkur:~/work/test/loopbug$ gcc --version
gcc (Ubuntu 4.4.1-4ubuntu9) 4.4.1

Same happens with gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1

Same problem in "avr-gcc (GCC) 4.3.3" with "char" and "uint8_t" instead of
"long" and "unsigned long". Then with 2^7 = 128 the bug occurs.


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

* [Bug c/50922] infinite loop when optimized
  2011-10-30 15:44 [Bug c/50922] New: infinite loop when optimized pfister at pci dot uzh.ch
@ 2011-10-30 16:26 ` xunxun1982 at gmail dot com
  2011-10-30 17:03 ` mikpe at it dot uu.se
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: xunxun1982 at gmail dot com @ 2011-10-30 16:26 UTC (permalink / raw)
  To: gcc-bugs

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

xunxun <xunxun1982 at gmail dot com> changed:

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

--- Comment #1 from xunxun <xunxun1982 at gmail dot com> 2011-10-30 16:26:26 UTC ---
I found that when combining -O1, -fstrict-overflow and -ftree-vrp, the infinite
loop occured.
And -O2 contains -O1, -fstrict-overflow and -ftree-vrp .


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

* [Bug c/50922] infinite loop when optimized
  2011-10-30 15:44 [Bug c/50922] New: infinite loop when optimized pfister at pci dot uzh.ch
  2011-10-30 16:26 ` [Bug c/50922] " xunxun1982 at gmail dot com
@ 2011-10-30 17:03 ` mikpe at it dot uu.se
  2011-10-30 19:15 ` pinskia at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: mikpe at it dot uu.se @ 2011-10-30 17:03 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Mikael Pettersson <mikpe at it dot uu.se> 2011-10-30 17:03:10 UTC ---
(In reply to comment #0)
> int main(void)
> {
>  long x=0;
>  unsigned long c;
>  for(c=0;c<2147483648UL;c++)
>    {
>       lcdSendData(x++);
>     }

You're causing a signed overflow in the "x++" expression.  Try making x
"unsigned long" instead.  Or pass "-fwrapv" to gcc.


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

* [Bug c/50922] infinite loop when optimized
  2011-10-30 15:44 [Bug c/50922] New: infinite loop when optimized pfister at pci dot uzh.ch
  2011-10-30 16:26 ` [Bug c/50922] " xunxun1982 at gmail dot com
  2011-10-30 17:03 ` mikpe at it dot uu.se
@ 2011-10-30 19:15 ` pinskia at gcc dot gnu.org
  2011-10-31  7:38 ` pfister at pci dot uzh.ch
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2011-10-30 19:15 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> 2011-10-30 19:14:49 UTC ---
Signed integer overflow is undefined so this is invalid.


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

* [Bug c/50922] infinite loop when optimized
  2011-10-30 15:44 [Bug c/50922] New: infinite loop when optimized pfister at pci dot uzh.ch
                   ` (2 preceding siblings ...)
  2011-10-30 19:15 ` pinskia at gcc dot gnu.org
@ 2011-10-31  7:38 ` pfister at pci dot uzh.ch
  2011-10-31 10:18 ` manu at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pfister at pci dot uzh.ch @ 2011-10-31  7:38 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Rolf Pfister <pfister at pci dot uzh.ch> 2011-10-31 07:38:01 UTC ---
Am 30.10.11 20:14, schrieb pinskia at gcc dot gnu.org:
> --- Comment #3 from Andrew Pinski<pinskia at gcc dot gnu.org>  2011-10-30 19:14:49 UTC ---
> Signed integer overflow is undefined so this is invalid.
>
If this is true, shouldnt the compiler give an error message or at least
a warning?
But I think incrementing is always well defined, even when overflowing.
And in the example the result after occuring the overflow is not
used anyway.

Rolf


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

* [Bug c/50922] infinite loop when optimized
  2011-10-30 15:44 [Bug c/50922] New: infinite loop when optimized pfister at pci dot uzh.ch
                   ` (3 preceding siblings ...)
  2011-10-31  7:38 ` pfister at pci dot uzh.ch
@ 2011-10-31 10:18 ` manu at gcc dot gnu.org
  2011-10-31 11:02 ` pfister at pci dot uzh.ch
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: manu at gcc dot gnu.org @ 2011-10-31 10:18 UTC (permalink / raw)
  To: gcc-bugs

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

Manuel López-Ibáñez <manu at gcc dot gnu.org> changed:

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

--- Comment #5 from Manuel López-Ibáñez <manu at gcc dot gnu.org> 2011-10-31 10:18:29 UTC ---
(In reply to comment #4)
> Am 30.10.11 20:14, schrieb pinskia at gcc dot gnu.org:
> > --- Comment #3 from Andrew Pinski<pinskia at gcc dot gnu.org>  2011-10-30 19:14:49 UTC ---
> > Signed integer overflow is undefined so this is invalid.
> >
> If this is true, shouldnt the compiler give an error message or at least
> a warning?
> But I think incrementing is always well defined, even when overflowing.
> And in the example the result after occuring the overflow is not
> used anyway.

http://gcc.gnu.org/wiki/FAQ#signed_overflow


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

* [Bug c/50922] infinite loop when optimized
  2011-10-30 15:44 [Bug c/50922] New: infinite loop when optimized pfister at pci dot uzh.ch
                   ` (4 preceding siblings ...)
  2011-10-31 10:18 ` manu at gcc dot gnu.org
@ 2011-10-31 11:02 ` pfister at pci dot uzh.ch
  2011-10-31 19:58 ` pfister at pci dot uzh.ch
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pfister at pci dot uzh.ch @ 2011-10-31 11:02 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Rolf Pfister <pfister at pci dot uzh.ch> 2011-10-31 11:01:33 UTC ---
Am 31.10.11 11:18, schrieb manu at gcc dot gnu.org:
>
> http://gcc.gnu.org/wiki/FAQ#signed_overflow
>
Im not really convinced that the reason is the overflow.
Where is the overflow in this case?

  long x=2147483647;
  ulong c;
  for(c=0;c<2147483648UL;c++)
    {
       lcdSendData(x--);
    }

At least with "gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-48)"
this also gives an infinite loop.

Rolf


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

* [Bug c/50922] infinite loop when optimized
  2011-10-30 15:44 [Bug c/50922] New: infinite loop when optimized pfister at pci dot uzh.ch
                   ` (5 preceding siblings ...)
  2011-10-31 11:02 ` pfister at pci dot uzh.ch
@ 2011-10-31 19:58 ` pfister at pci dot uzh.ch
  2011-10-31 20:39 ` manu at gcc dot gnu.org
  2011-11-01  7:26 ` pfister at pci dot uzh.ch
  8 siblings, 0 replies; 10+ messages in thread
From: pfister at pci dot uzh.ch @ 2011-10-31 19:58 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Rolf Pfister <pfister at pci dot uzh.ch> 2011-10-31 19:58:07 UTC ---
I wrote:

 > long x=2147483647;
 > ulong c;
 > for(c=0;c<2147483648UL;c++)
 >   {
 >      lcdSendData(x--);
 >   }

I tried this new variant with "gcc (Ubuntu 4.4.1-4ubuntu9) 4.4.1"
With this version it works correctly. Also with avr-gcc this variant 
works correctly.
So the problem seems really to be solved.

Rolf


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

* [Bug c/50922] infinite loop when optimized
  2011-10-30 15:44 [Bug c/50922] New: infinite loop when optimized pfister at pci dot uzh.ch
                   ` (6 preceding siblings ...)
  2011-10-31 19:58 ` pfister at pci dot uzh.ch
@ 2011-10-31 20:39 ` manu at gcc dot gnu.org
  2011-11-01  7:26 ` pfister at pci dot uzh.ch
  8 siblings, 0 replies; 10+ messages in thread
From: manu at gcc dot gnu.org @ 2011-10-31 20:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from Manuel López-Ibáñez <manu at gcc dot gnu.org> 2011-10-31 20:38:17 UTC ---
(In reply to comment #7)
> I tried this new variant with "gcc (Ubuntu 4.4.1-4ubuntu9) 4.4.1"
> With this version it works correctly. Also with avr-gcc this variant 
> works correctly.
> So the problem seems really to be solved.

I sincerely hope you are not doing something important with your code. Relying
on undefined behaviour sometimes "working" is like playing russian roulette.
That the last five times nothing bad happened does not tell you anything about
what will happen next time you try.


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

* [Bug c/50922] infinite loop when optimized
  2011-10-30 15:44 [Bug c/50922] New: infinite loop when optimized pfister at pci dot uzh.ch
                   ` (7 preceding siblings ...)
  2011-10-31 20:39 ` manu at gcc dot gnu.org
@ 2011-11-01  7:26 ` pfister at pci dot uzh.ch
  8 siblings, 0 replies; 10+ messages in thread
From: pfister at pci dot uzh.ch @ 2011-11-01  7:26 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #9 from Rolf Pfister <pfister at pci dot uzh.ch> 2011-11-01 07:25:53 UTC ---
Am 31.10.11 21:38, schrieb manu at gcc dot gnu.org:
>
> I sincerely hope you are not doing something important with your code. Relying

No, I dont use this code in my own programs. It was just the first
example program coming with a new hardware device.
Now when someone will ask again in the forum, I can tell them that it is
a bug in the program and not in the compiler.
http://myavr.info/myForum/viewtopic.php?t=2974

Rolf


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

end of thread, other threads:[~2011-11-01  7:26 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-10-30 15:44 [Bug c/50922] New: infinite loop when optimized pfister at pci dot uzh.ch
2011-10-30 16:26 ` [Bug c/50922] " xunxun1982 at gmail dot com
2011-10-30 17:03 ` mikpe at it dot uu.se
2011-10-30 19:15 ` pinskia at gcc dot gnu.org
2011-10-31  7:38 ` pfister at pci dot uzh.ch
2011-10-31 10:18 ` manu at gcc dot gnu.org
2011-10-31 11:02 ` pfister at pci dot uzh.ch
2011-10-31 19:58 ` pfister at pci dot uzh.ch
2011-10-31 20:39 ` manu at gcc dot gnu.org
2011-11-01  7:26 ` pfister at pci dot uzh.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).