https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91914
Bug ID: 91914
Summary: Invalid strlen optimization
Product: gcc
Version: 8.3.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: [email protected]
Target Milestone: ---
cat > t.c <<EOF
#include <stdio.h>
#include <string.h>
struct stringpool_t
{
char stringpool_str4[sizeof("Foo")];
char stringpool_str5[sizeof("Bar")];
};
static const struct stringpool_t stringpool_contents = {
"Foo",
"Bar",
};
#define stringpool ((const char *) &stringpool_contents)
int main( int argc, char** argv )
{
printf( "%zu\n", strlen( stringpool + 4 ) );
volatile int idx = 4;
printf( "%zu\n", strlen( stringpool + idx ) );
return 0;
}
EOF
gcc -o t t.c -Wall
## Output:
3
0
The first strlen() has the warning "offset '4' outside bounds
of constant string [-Warray-bounds]". I think this is a wrong
warning, because of the cast to char*. But the strlen() result is okay.
The second strlen() returns 0. This is more serious as this results in wrong
code.
The stringpool-code is generated by gperf.