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

            Bug ID: 104789
           Summary: [12 Regression] New -Wstringop-overflow false positive
                    since r12-5863-g9354a7d70caef1c9
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: marxin at gcc dot gnu.org
                CC: msebor at gcc dot gnu.org
  Target Milestone: ---

It's originally reported here:
https://github.com/godotengine/godot/issues/58747

$ cat godot-stringop.cpp
#include <cstdint>
#include <cstdio>

uint32_t some_func(const uint32_t components) {
        uint8_t header[8];
        uint32_t header_bytes = 0;
        for (uint32_t i = 0; i < components; i++) {
                header_bytes += 2;
        }
        header_bytes += 2;
        // This works it around, but shouldn't be needed AFAICT.
        //while (header_bytes != 8 && header_bytes % 4 != 0) {
        while (header_bytes % 4 != 0) {
                header[header_bytes++] = 0;
        }
        for (uint32_t i = 0; i < header_bytes; i++) {
                printf("%d\n", header[i]);
        }
        return header_bytes;
}

int main() {
        some_func(1);
        some_func(3);
        return 0;
}

$ g++ godot-stringop.cpp -c -Werror=all -O3
godot-stringop.cpp: In function ‘uint32_t some_func(uint32_t)’:
godot-stringop.cpp:14:40: error: writing 1 byte into a region of size 0
[-Werror=stringop-overflow=]
   14 |                 header[header_bytes++] = 0;
      |                 ~~~~~~~~~~~~~~~~~~~~~~~^~~
godot-stringop.cpp:5:17: note: at offset 8 into destination object ‘header’ of
size 8
    5 |         uint8_t header[8];
      |                 ^~~~~~
cc1plus: some warnings being treated as errors

Reply via email to