https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89689
Bug ID: 89689
Summary: [7/8 regression] false warning -Wstringop-overflow=
Product: gcc
Version: 8.3.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: chantry.xavier at gmail dot com
Target Milestone: ---
Created attachment 45953
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=45953&action=edit
test case
I am testing gcc version 8.3.1 20190228
With "gcc -O" I get the following warning:
In function ‘a’,
inlined from ‘o’ at result2.c:31:5:
result2.c:7:12: warning: ‘__builtin_memcpy’ writing 4 bytes into a region of
size 1 overflows the destination [-Wstringop-overflow=]
return __builtin___memcpy_chk(c, d, n, __builtin_object_size(c, 0));
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
With "gcc -O -Wall" I get the following warning:
In function ‘a’,
inlined from ‘o’ at result2.c:31:5:
result2.c:7:12: warning: ‘__builtin_memcpy’ forming offset [2, 4] is out of the
bounds [0, 1] of object ‘__sb_slop’ with type ‘const char[1]’ [-Warray-bounds]
return __builtin___memcpy_chk(c, d, n, __builtin_object_size(c, 0));
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
result2.c: In function ‘o’:
result2.c:13:12: note: ‘__sb_slop’ declared here
const char __sb_slop[1];
^~~~~~~~~
In both cases it looks buggy, because h.data points to "char buf[5]" and not to
"const char __sb_slop[1]".
According to my tests on godbolt.org, the first warning appeared on gcc 7, the
second warning appeared on gcc 8. There was no warning with gcc <= 6.
This test case does not make sense but it was derived from production code
using creduce.
I do not want to disable Wstringop-overflow or Warray-bounds entirely because
they look useful sometimes, and I did not find a workaround by modifying the
code. Fortunately the warning can be disabled locally using pragma directives.