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

            Bug ID: 122298
           Summary: -Warray-bounds -O2 false positive with bounds computed
                    using modulus
           Product: gcc
           Version: 15.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ldr709 at gmail dot com
  Target Milestone: ---

GCC 15 seems to have introduced a `-Warray-bounds` false positive. This is as
small as I've been able to minimize the test example.

inline void bar(unsigned int* x, unsigned int round)
{
    unsigned int offset = (round % 3 + 1) * 2;
    x += offset;
    for (unsigned int i = 0; i < 6 - offset; ++i) x[i] += 1;
}

void foo()
{
    unsigned int x[6] = {0};

    for (unsigned int round = 2; round <= 10; round += 3)
        bar(x, round);
}

(Or see https://godbolt.org/z/7MEb14oEv)

Compiler warnings and version:

# gcc test.c -c -o test.o -Warray-bounds -O2
In function ‘bar’,
    inlined from ‘foo’ at test.c:13:9:
test.c:5:52: warning: array subscript 6 is outside array bounds of ‘unsigned
int[6]’ [-Warray-bounds=]
    5 |     for (unsigned int i = 0; i < 6 - offset; ++i) x[i] += 1;
      |                                                   ~^~~
test.c: In function ‘foo’:
test.c:10:18: note: at offset 24 into object ‘x’ of size 24
   10 |     unsigned int x[6] = {0};
      |                  ^
In function ‘bar’,
    inlined from ‘foo’ at test.c:13:9:
test.c:5:56: warning: array subscript 6 is outside array bounds of ‘unsigned
int[6]’ [-Warray-bounds=]
    5 |     for (unsigned int i = 0; i < 6 - offset; ++i) x[i] += 1;
      |                                                   ~~~~~^~~~
test.c: In function ‘foo’:
test.c:10:18: note: at offset 24 into object ‘x’ of size 24
   10 |     unsigned int x[6] = {0};
      |                  ^

# gcc --version
gcc (GCC) 15.2.1 20250813
Copyright (C) 2025 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Reply via email to