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

             Bug #: 51309
           Summary: -Wstrict-overflow false alarm when overflow impossible
                    in loop body
    Classification: Unclassified
           Product: gcc
           Version: 4.6.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: egg...@gnu.org


Created attachment 25913
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=25913
"gcc -O2 -Wstrict-overflow" issues false alarm for this

(I ran into this problem when building a test version of GNU Emacs.)

"gcc -Wstrict-overflow -O2" sometimes issues a bogus warning when an
integer cannot possibly overflow, because it is incremented in a
location where it is known to not have the maximum integer value.

Compile the attached file u.c with "gcc -Wstrict-overflow -O2 -S u.c".
There will be a diagnostic:

  u.c: In function 'wait_reading_process_output':
  u.c:19:10: warning: assuming signed overflow does not occur when simplifying
conditional to constant [-Wstrict-overflow]

Signed overflow cannot possibly occur when "nfds++" is executed, for
three independent reasons:

  1.  If nfds is INT_MAX, the line commented /*1*/ ensures that
      "nfds++" is unreachable.

  2.  If nfds is INT_MAX, the line commented /*2*/ ensures that
      "nfds++" is unreachable.

  3.  nfds starts at zero and is incremented in a loop body that is
      executed at most 1024 times, so it cannot possibly overflow.

Each reason by itself is enough to guarantee no overflow in u.c.

Mysteriously enough, changing the "1000" to "1" in line /*1*/ or line
/*2*/ (or both) makes the bogus diagnostic vanish.

For reference, here is the output of "gcc -v -save-temps
-Wstrict-overflow -O2 -S u.c".

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/home/eggert/opt/Linux-x86_64/gcc-4.6.2/libexec/gcc/x86_64-unknown-linux-gnu/4.6.2/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../gcc-4.6.2/configure
--prefix=/home/eggert/opt/Linux-x86_64/gcc-4.6.2 --disable-nls
Thread model: posix
gcc version 4.6.2 (GCC)
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wstrict-overflow' '-O2' '-S'
'-mtune=generic' '-march=x86-64'

/home/eggert/opt/Linux-x86_64/gcc-4.6.2/libexec/gcc/x86_64-unknown-linux-gnu/4.6.2/cc1
-E -quiet -v u.c -mtune=generic -march=x86-64 -Wstrict-overflow -O2
-fpch-preprocess -o u.i
ignoring nonexistent directory
"/home/eggert/opt/Linux-x86_64/gcc-4.6.2/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../x86_64-unknown-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:

/home/eggert/opt/Linux-x86_64/gcc-4.6.2/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/include
 /usr/local/include
 /home/eggert/opt/Linux-x86_64/gcc-4.6.2/include

/home/eggert/opt/Linux-x86_64/gcc-4.6.2/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/include-fixed
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wstrict-overflow' '-O2' '-S'
'-mtune=generic' '-march=x86-64'

/home/eggert/opt/Linux-x86_64/gcc-4.6.2/libexec/gcc/x86_64-unknown-linux-gnu/4.6.2/cc1
-fpreprocessed u.i -quiet -dumpbase u.c -mtune=generic -march=x86-64 -auxbase u
-O2 -Wstrict-overflow -version -o u.s
GNU C (GCC) version 4.6.2 (x86_64-unknown-linux-gnu)
    compiled by GNU C version 4.6.2, GMP version 4.3.2, MPFR version 3.0.0, MPC
version 0.8.3-dev
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU C (GCC) version 4.6.2 (x86_64-unknown-linux-gnu)
    compiled by GNU C version 4.6.2, GMP version 4.3.2, MPFR version 3.0.0, MPC
version 0.8.3-dev
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 26ba9716d225d191c012c3adc50d675b
u.c: In function 'wait_reading_process_output':
u.c:19:10: warning: assuming signed overflow does not occur when simplifying
conditional to constant [-Wstrict-overflow]
COMPILER_PATH=/home/eggert/opt/Linux-x86_64/gcc-4.6.2/libexec/gcc/x86_64-unknown-linux-gnu/4.6.2/:/home/eggert/opt/Linux-x86_64/gcc-4.6.2/libexec/gcc/x86_64-unknown-linux-gnu/4.6.2/:/home/eggert/opt/Linux-x86_64/gcc-4.6.2/libexec/gcc/x86_64-unknown-linux-gnu/:/home/eggert/opt/Linux-x86_64/gcc-4.6.2/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/:/home/eggert/opt/Linux-x86_64/gcc-4.6.2/lib/gcc/x86_64-unknown-linux-gnu/
LIBRARY_PATH=/home/eggert/opt/Linux-x86_64/gcc-4.6.2/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/:/home/eggert/opt/Linux-x86_64/gcc-4.6.2/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/home/eggert/opt/Linux-x86_64/gcc-4.6.2/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-Wstrict-overflow' '-O2' '-S'
'-mtune=generic' '-march=x86-64'

Reply via email to