https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105348
Bug ID: 105348
Summary: Overly aggressive -Warray-bounds after conditional
Product: gcc
Version: 12.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: middle-end
Assignee: unassigned at gcc dot gnu.org
Reporter: thiago at kde dot org
Target Milestone: ---
Testcase:
#include <string.h>
char empty;
void sink(int);
bool cond(size_t);
void f(const char *s, size_t l)
{
int n;
if (cond(l)) {
memcpy(&n, s, sizeof(n));
sink(n);
}
}
void g()
{
f(&empty, 1);
}
#ifdef EXPAND
bool cond(size_t l)
{
return l >= sizeof(int);
}
#endif
$ gcc -DEXPAND -O3 -c -Wall -Wextra -Werror test.cpp && echo $?
0
$ gcc -O3 -c -Wall -Wextra -Werror test.cpp && echo $?
In function ‘void f(const char*, size_t)’,
inlined from ‘void f(const char*, size_t)’ at test.cpp:6:6,
inlined from ‘void g()’ at test.cpp:17:6:
test.cpp:10:15: error: array subscript ‘unsigned int[0]’ is partly outside
array bounds of ‘char [1]’ [-Werror=array-bounds]
10 | memcpy(&n, s, sizeof(n));
| ~~~~~~^~~~~~~~~~~~~~~~~~
test.cpp: In function ‘void g()’:
test.cpp:2:6: note: object ‘empty’ of size 1
2 | char empty;
| ^~~~~
cc1plus: all warnings being treated as errors
I've noticed this even when the other function was present and available for
inlining. Unfortunately, for reasons outside of my direct control, GCC decided
not to inline that function, which meant it considers the condition bad.