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

            Bug ID: 90387
           Summary: [9 Regression] __builtin_constant_p and -Warray-bounds
                    warnings
           Product: gcc
           Version: 9.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: b.buschinski at googlemail dot com
  Target Milestone: ---

Created attachment 46314
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=46314&action=edit
gcc9_constant_array_bounds_bug.c

The attached example does not compile with GCC 9.1.0.
It gives an Warray-bounds warning, which I think is invalid, as the code path
should not be triggered as the given length parameter is not compile time
const.

It worked fine with GCC 8.3.



$ gcc-8.3.0 -O2 -Wall -Werror -c gcc9_constant_array_bounds_bug.c
$ echo $?
0

$ gcc-9.1.0 -O2 -Wall -Werror -c gcc9_constant_array_bounds_bug.c 
gcc9_constant_array_bounds_bug.c: In function 'foo':
gcc9_constant_array_bounds_bug.c:6:58: error: array subscript [6, 9] is outside
array bounds of 'const char[5]' [-Werror=array-bounds]
    6 |         ((unsigned char*)src)[6] == ((unsigned char*)pat)[6] \
      |                                     ~~~~~~~~~~~~~~~~~~~~~^~~
gcc9_constant_array_bounds_bug.c:22:9: note: in expansion of macro 'MY_MEMCMP'
   22 |         MY_MEMCMP(p, pattern + state, p_len) == 0) {
      |         ^~~~~~~~~
gcc9_constant_array_bounds_bug.c:11:23: note: while referencing 'pattern'
   11 |     static char const pattern[] = "abcd";
      |                       ^~~~~~~
cc1: all warnings being treated as errors
$ echo $?
1



The code also works fine if I change the "#if 1" to "#if 0", which confuses me
greatly.

It seems that (with the #if 1) GCC 9.1 treats "p_len" as compile-time constant,
which it is clearly not, as this is a function parameter. Or does
-Warray-bounds always check all paths? Regardless if they are impossible?

And yes, the warning would be correct if "p_len" would be compile-time const.

NOTE: I reduced the code from ~500+ lines to this, but I guess you can reduce
it further :)

Reply via email to