https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122302
Bug ID: 122302
Summary: Wrong bytes read from byte array defined in struct
from second instantiation of struct onwards
Product: gcc
Version: 15.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: ipa
Assignee: unassigned at gcc dot gnu.org
Reporter: huisanh9 at gmail dot com
Target Milestone: ---
Created attachment 62571
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=62571&action=edit
Minimal reproducer
I've come across a problem with GCC15+ (GCC14 and older are unaffected) in
accessing an array of predefined bytes within a struct when that struct is used
more than once in a compilation unit, and when the array is initialised with
over a certain number of elements.
For example, in this reproducer (edited for brevity, complete version attached
and at https://godbolt.org/z/bYnYx5eKr), given this struct and data:
struct blob
{
// The data must be a uint8_t or unsigned char - problem does not seem to
occur with larger integer types
uint8_t data[130] = { 0x01, 0x02, ..., 0x82 };
};
This usage produces the wrong output from `func2()`:
void f1()
{
blob b;
printf("%s: [%02x %02x %02x %02x]\n",
__FUNCTION__,
(b.data[0] & 0xff),
(b.data[1] & 0xff),
(b.data[2] & 0xff),
(b.data[3] & 0xff));
// An additional blob instantiated here will also report the wrong bytes
}
void f2()
{
blob b;
printf("%s: [%02x %02x %02x %02x]\n",
__FUNCTION__,
(b.data[0] & 0xff),
(b.data[1] & 0xff),
(b.data[2] & 0xff),
(b.data[3] & 0xff));
}
int main(int argc, char** argv)
{
// Order of execution doesn't matter
f2();
f1();
f1();
f2();
return 0;
};
Building the example:
$ g++ reproducer.cpp -o reproducer
$ ./reproducer
func1: [01 02 03 04]
func2: [01 01 02 03]
func2: [01 01 02 03]
func1: [01 02 03 04]
If `data` is initialised with less than 130 bytes this problem does not occur
(the declared size doesn't appear to matter as long as it's large enough to
hold the number of initialising bytes).
If `data` is not within the struct, the problem does not occur.