public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/39736]  New: signed overflow in loop induction variable: missing warning and wrong code
@ 2009-04-11 12:30 edwintorok at gmail dot com
  2009-04-11 12:52 ` [Bug tree-optimization/39736] " joseph at codesourcery dot com
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: edwintorok at gmail dot com @ 2009-04-11 12:30 UTC (permalink / raw)
  To: gcc-bugs

[This bug was discovered by kantorzsolt@yahoo.com]

When compiling the testcase below at -O2, using gcc 4.3.3 instead of an infite
loop, where the variable 'from' takes values from -32768 to 32767, 
it goes up way beyond that limit:

$ gcc -O2 tes -Wall -W -Wstrict-overflow=5 && ./a.out
0
1
...
2253491

When using -fwrapv, or -fno-strict-overflow 'from' iterates between -32768 and
32767 in an infinite loop, as expected.

There are 2 bugs here:
- no warning is given with -Wstrict-overflow=5, although -fstrict-overflow
changes the behaviour of the code
- from is of type short, so when sign extended to an int, it should only take
values allowed for a short: -32768 to 32767, not all values allowed for an int
(like 2253491)

I think this is a bug, because although signed overflow is undefined behaviour, 
that should affect only the value of 'from', which is still of type 'short',
and converting to int should obey the limits for the 'short' type, i.e. use a
sign-extension.

Here is a diff of the assembly, when using -O2, vs -O2 -fno-strict-overflow:
--- x2.s        2009-04-11 15:28:01.000000000 +0300
+++ x2-no-overflow.s    2009-04-11 15:27:58.000000000 +0300
@@ -10,15 +10,16 @@
 .LFB13:
        pushq   %rbx
 .LCFI0:
+       xorl    %esi, %esi
        xorl    %ebx, %ebx
        .p2align 4,,10
        .p2align 3
 .L2:
-       movl    %ebx, %esi
        movl    $.LC0, %edi
        xorl    %eax, %eax
-       call    printf
        addl    $1, %ebx
+       call    printf
+       movswl  %bx,%esi
        jmp     .L2
 .LFE13:
        .size   main, .-main


Testcase:
#include <stdio.h>
int
main ()
{
        int until = 40001;
        short from = 0;

        for (; from < until; from++)
                printf ("%d\n", from);

        return 0;
}

$ gcc -v
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.3.3-5'
--with-bugurl=file:///usr/share/doc/gcc-4.3/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared
--with-system-zlib --libexecdir=/usr/lib --without-included-gettext
--enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.3
--program-suffix=-4.3 --enable-clocale=gnu --enable-libstdcxx-debug
--enable-objc-gc --enable-mpfr --with-tune=generic --enable-checking=release
--build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.3.3 (Debian 4.3.3-5)

Also reproduced with trunk:
$ ~/gcc_inst/bin/gcc -v
Using built-in specs.
Target: x86_64-unknown-linux-gnu
Configured with: ../gcc/configure --disable-multilib --disable-static
--prefix=/home/edwin/gcc_inst/ --enable-languages=c,c++
Thread model: posix
gcc version 4.5.0 20090408 (experimental) [trunk revision 145769] (GCC)


-- 
           Summary: signed overflow in loop induction variable: missing
                    warning and wrong code
           Product: gcc
           Version: 4.3.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: edwintorok at gmail dot com
 GCC build triplet: x86_64-linux-gnu
  GCC host triplet: x86_64-linux-gnu
GCC target triplet: x86_64-linux-gnu


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


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

end of thread, other threads:[~2009-04-13  8:20 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-04-11 12:30 [Bug tree-optimization/39736] New: signed overflow in loop induction variable: missing warning and wrong code edwintorok at gmail dot com
2009-04-11 12:52 ` [Bug tree-optimization/39736] " joseph at codesourcery dot com
2009-04-12  9:11 ` mikpe at it dot uu dot se
2009-04-12  9:32 ` edwintorok at gmail dot com
2009-04-12 21:34 ` mikpe at it dot uu dot se
2009-04-13  6:56 ` edwintorok at gmail dot com
2009-04-13  7:53 ` schwab at linux-m68k dot org
2009-04-13  8:20 ` rguenth at gcc dot gnu dot org

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