Bug 51309 - -Wstrict-overflow false alarm when overflow impossible in loop body
Summary: -Wstrict-overflow false alarm when overflow impossible in loop body
Status: RESOLVED WORKSFORME
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.6.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-11-26 07:14 UTC by Paul Eggert
Modified: 2017-07-26 03:33 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
"gcc -O2 -Wstrict-overflow" issues false alarm for this (258 bytes, text/x-csrc)
2011-11-26 07:14 UTC, Paul Eggert
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Paul Eggert 2011-11-26 07:14:43 UTC
Created attachment 25913 [details]
"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'
Comment 1 Eric Gallager 2017-07-25 19:45:07 UTC
I can't seem to reproduce the warning; all of these are silent:

$ /usr/local/bin/gcc -Wstrict-overflow -O2 -S u.c
$ /usr/local/bin/gcc -Wstrict-overflow=1 -O2 -S u.c
$ /usr/local/bin/gcc -Wstrict-overflow=2 -O2 -S u.c
$ /usr/local/bin/gcc -Wstrict-overflow=3 -O2 -S u.c
$ /usr/local/bin/gcc -Wstrict-overflow=4 -O2 -S u.c
$ /usr/local/bin/gcc -Wstrict-overflow=5 -O2 -S u.c
$ /usr/local/bin/gcc -Wstrict-overflow -O1 -S u.c
$ /usr/local/bin/gcc -Wstrict-overflow -O0 -S u.c
$ /usr/local/bin/gcc -Wstrict-overflow -Os -S u.c
$ /usr/local/bin/gcc -Wstrict-overflow -Og -S u.c
$ /usr/local/bin/gcc -Wstrict-overflow -O3 -S u.c
$ /usr/local/bin/gcc -Wstrict-overflow -Ofast -S u.c

And seeing as -Wstrict-overflow is being considered for deprecation in the next release of GCC anyways, I think I can close this.
Comment 2 Paul Eggert 2017-07-26 03:33:32 UTC
(In reply to Eric Gallager from comment #1)
> I can't seem to reproduce the warning

Thanks, I can no longer reproduce the warning either. I used GCC 7.1.1 20170622 (Red Hat 7.1.1-3). So it looks like the problem is fixed.