Bug 1676607

Summary: False? warning: assuming signed overflow does not occur when changing X +- C1 cmp C2 to X cmp C2 -+ C1 [-Wstrict-overflow]
Product: [Fedora] Fedora Reporter: Petr Lautrbach <plautrba>
Component: gccAssignee: Jakub Jelinek <jakub>
Status: CLOSED NOTABUG QA Contact: Fedora Extras Quality Assurance <extras-qa>
Severity: unspecified Docs Contact:
Priority: unspecified    
Version: rawhideCC: aoliva, davejohansen, dmalcolm, fweimer, jakub, jwakely, law, mpolacek, msebor, nickc
Target Milestone: ---   
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2019-02-12 16:44:31 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Attachments:
Description Flags
i--reproducer.i none

Description Petr Lautrbach 2019-02-12 16:34:53 UTC
Created attachment 1534148 [details]
i--reproducer.i

Version-Release number of selected component (if applicable):
gcc-9.0.1-0.3.fc30.x86_64

Description of problem:

I think gcc 9 generates a false warning in the following example when -Wstrict-overflow=3 is used. And it also doesn't identify the correct line/code, just the function where it happens.

Steps to Reproduce:

$ cat i--reproducer.c
#include <stdio.h>

int main() {
        int i, j;
        int len;

        scanf("%d", &len);
        if (len <= 0)
                return -1;
        i = len;
        for (--i; i >= 0; --i)
                j = i;

        return j;
}


$ gcc -O2 -Wstrict-overflow=3 i--reproducer.c
i--reproducer.c: In function ‘main’:
i--reproducer.c:15:1: warning: assuming signed overflow does not occur when changing X +- C1 cmp C2 to X cmp C2 -+ C1 [-Wstrict-overflow]
   15 | }
      | ^
i--reproducer.c:3:5: warning: assuming signed overflow does not occur when changing X +- C1 cmp C2 to X cmp C2 -+ C1 [-Wstrict-overflow]
    3 | int main() {
      |     ^~~~

$ gcc -v
Using built-in specs.
COLLECT_GCC=/usr/bin/gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/9/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl --enable-offload-targets=nvptx-none --without-cuda-driver --enable-gnu-indirect-function --enable-cet --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 9.0.1 20190203 (Red Hat 9.0.1-0.3) (GCC)

Comment 1 Jakub Jelinek 2019-02-12 16:44:31 UTC
Can you explain why you think it is a false positive?  This warning is totally useless warning, where the compiler just tells the user it does its job, optimizing code based on the assumption that undefined behavior does not happen.
Unless you know what you are doing, enabling this warning doesn't really make much sense.  The warning above is about len - 1 >= 0 being optimized into len >= 1, which can be done only because INT_MIN - 1 >= 0 would trigger undefined behavior.  As I said, the warning is just logging that the compiler has done some optimization (that checks the -Wstrict-overflow=*) and used the assumption there is no UB, it is not guarded by further analysis what the value range of the variable is etc. (using that wouldn't be very useful, because the value range computation also relies on signed integer overflow not happening heavily).

Comment 2 Petr Lautrbach 2019-02-12 18:38:50 UTC
I probably didn't use right words. This warning and apparently other warnings are new in gcc 9. gcc-8.2.1-6.fc29.x86_64 doesn't log that. Thanks for the explanation.