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

            Bug ID: 119867
           Summary: Spurious warning about out-of-bound write with -O3 and
                    -D_GLIBCXX_ASSERTIONS
           Product: gcc
           Version: 14.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: yyc1992 at gmail dot com
  Target Milestone: ---

The following code, when compiled on arm (32bit) or aarch64 with `-O3
-D_GLIBCXX_ASSERTIONS` produces a warning for out of bound write.

```
<source>: In function 'auto f(unsigned int, unsigned int)':
<source>:7:16: warning: writing 16 bytes into a region of size 1
[-Wstringop-overflow=]
    7 |         res[i] = 1;
<source>:3:1: note: at offset [32, 33] into destination object ''result_decl'
not supported by dump_expr<expression error>' of size 33
    3 | auto f(unsigned idx1, unsigned idx2)
      | ^~~~
Compiler returned: 0
```

```
#include <array>

auto f(unsigned idx1, unsigned idx2)
{
    std::array<char,33> res;
    for (unsigned i = idx1 + 1; i < idx2; i++)
        res[i] = 1;
    return res;
}
```

Testing on compiler explorer suggests that this affect at least all gcc
versions >=14 up to trunk but seems to not affect non-arm architectures. The
code above could of course produce out of bound write but I don't think the
compiler is in the business of warning me about this and it also does not seem
to be what the warning is about. FWIW, directly writing `res[idx1] = 0` or
`res[idx2] = 0` does not produce a warning and adding a `i < 33` in the loop
condition also seems to suppress the warning. This makes me think the bug isn't
in libstdc++ but in the compiler vectorizes the loop with the wrong bound or
sth....

Reply via email to