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

            Bug ID: 122473
           Summary: False-positive -Wstringop-overflow warning when using
                    `-march=x86-64-v3 -O3`
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: abbeyj+gcc at gmail dot com
  Target Milestone: ---

When compiling the following with `-march=x86-64-v3 -O3`, a -Wstringop-overflow
warning is produced:

```
struct my_descriptor {
  int length;
  char *pointer;
};

void sink(const char *);

void print(const my_descriptor *desc)
{
  char output[9];
  char *p = output;
  for (int i = desc->length; i >= 0; i--) {
    p[i] = desc->pointer[i];
  }
  sink(output);
}

```

Output:
> $ g++ -march=x86-64-v3 -O3 -c repro.cpp
> repro.cpp: In function ‘void print(const my_descriptor*)’:
> repro.cpp:13:10: warning: writing 16 bytes into a region of size 9 
> [-Wstringop-overflow=]
>    13 |     p[i] = desc->pointer[i];
>       |     ~~~~~^~~~~~~~~~~~~~~~~~
> repro.cpp:10:8: note: destination object ‘output’ of size 9
>    10 |   char output[9];
>       |        ^~~~~~


Instead of `-O3`, one could also use `-O2 -fvect-cost-model=dynamic` or `-O1
-foptimize-strlen -ftree-loop-vectorize -fvect-cost-model=dynamic`.

This looks like it started in GCC 12.1 and is still reproducible on the current
trunk version available on Godbolt: https://godbolt.org/z/foMd1Pq4s

I thought this might be a duplicate of Bug 106020, but the testcase there no
longer reproduces on GCC 15 or later while this testcase does.  Maybe also Bug
109852 but that has a lot of warnings and no minimized reproducer so it is hard
to tell.

Reply via email to