https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114178
Bug ID: 114178
Summary: incorrect -Wstringop-overflow with freestanding +
placement new w/ initialization + no sse
Product: gcc
Version: 13.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: evan.teran at gmail dot com
Target Milestone: ---
Created attachment 57580
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=57580&action=edit
dockerized example of the issue for both gcc 12 and gcc 13
This seems to be circumstantial, so I'll try to explain with as much detail as
I can. I have a personal OS project which I use c++ to develop for. Starting
with gcc-12 and gcc-13 I started getting an warnings triggered by
`-Wstringop-overflow`, which is a bit frustrating since I prefer to compile
with `-Werror`.
After spending some time narrowing it down, it boils down to this code
triggering the warning:
```
template
void dont_optimize_away(T &&value) {
asm volatile("" : "+r"(value));
}
int foo() {
auto contents = new char[128]();
dont_optimize_away(contents);
delete[] contents;
return 0;
}
```
As you can see, there really isn't much code here get wrong!
Compiling this file with the following flags results in the warning:
```
-std=c++17 # benign
-O3 # required for issue
-march=x86-64# benign
-ffreestanding # required for issue
-Wstringop-overflow # the warning in question
-mno-red-zone# benign (somewhat required for osdev)
-mno-avx # OS dev typically disables things like SSE and similar
-mno-avx2
-mno-mmx
-mno-sse
-mno-sse2
-mno-sse3
-mno-sse4
-mno-sse4.1
-mno-sse4.2
-mno-sse4a
-mno-ssse3
```
Here's where it gets confusing/interesting:
1. if I change `auto contents = new char[128]();` to `auto contents = new
char[128];` then i don't get the warning. Presumably, the issue is triggered by
the initialization of the array being created.
2. if I remove `-ffreestanding`, the warning goes away
3. if I remove `-mno-mmx`, the warning goes away
4. if I remove BOTH `-mno-sse` and `-mno-sse2`, the warning goes away
5. different combinations of the `-mno-` flags result in slight differences
to the warnings.
It seems to me, that with this setup, the compiler is emitting something
comparable to a memset to initialize the array and is highly unrolling it.
Something about this code with these flags is triggering the warning.
I don't know if the emitted code is legitimately reaching outside the bounds of
the buffer, or if warning is just getting confused.
I will be attaching a dockerized example of the issue which can be run by
simply untarring and running `./build.sh`
Thanks