https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119005

Alejandro Colomar <alx at kernel dot org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|INVALID                     |FIXED

--- Comment #2 from Alejandro Colomar <alx at kernel dot org> ---
Hi Andrew,

I have rewritten the code to move the ++ out of the conditional, and I still
get the diagnostic, and I think it's a false positive, according to the
documentation.


alx@debian:~/tmp$ cat foo.c 
#include <limits.h>
int
f(void)
{
        static int i = 0;

        i++;
        if (i >= 3)
                return -1;

        return 0;
}
alx@debian:~/tmp$ gcc -Werror=strict-overflow=2 -Wall -S foo.c 
alx@debian:~/tmp$ gcc -Werror=strict-overflow=3 -Wall -S foo.c 
foo.c: In function ‘f’:
foo.c:12:1: warning: assuming signed overflow does not occur when changing X +-
C1 cmp C2 to X cmp C2 -+ C1 [-Wstrict-overflow]
   12 | }
      | ^
alx@debian:~/tmp$ MANWIDTH=72 man gcc 2>/dev/null | sed -n
'/-Wstrict-overflow=3/,/^$/p'
           -Wstrict-overflow=3
               Also warn  about  other  cases  where  a  comparison  is
               simplified.   For  example: "x + 1 > 1" is simplified to
               "x > 0".


Since that says 'also', we need to check the 1 and 2 levels:


           -Wstrict-overflow=1
               Warn  about  cases that are both questionable and easy to avoid.
               For example the compiler simplifies "x + 1  >  x"  to  1.   This
               level  of  -Wstrict-overflow  is enabled by -Wall; higher levels
               are not, and must be explicitly requested.

           -Wstrict-overflow=2
               Also warn about other cases where a comparison is simplified  to
               a  constant.   For  example:  "abs  (x) >= 0".  This can only be
               simplified when signed integer overflow  is  undefined,  because
               "abs (INT_MIN)" overflows to "INT_MIN", which is less than zero.
               -Wstrict-overflow    (with    no   level)   is   the   same   as
               -Wstrict-overflow=2.


While I agree that this might eventually overflow the int, I don't see how the
diagnostic matches the documentation.  The diagnostic seems to be about
conditionals, not about overflowing per se, and I don't see how this
conditional is questionable nor constant nor impossible (except for overflow).

Reply via email to