https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77294
Bug ID: 77294 Summary: __builtin_object_size inconsistent for member arrays Product: gcc Version: 7.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: middle-end Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- Bbesides bug 77293, further testing of my patch for bug 71831 also revealed that __builtin_object_size yields inconsistent results for member arrays depending on how an element of the array is referenced (using slightly different but equivalent expressions) and on whether or not an offset into the array is an integer constant. The following test case shows the inconsistency both between the iterations of the first and within the first one. The output is expected to be consistent both between the two iterations but also within each one of them (i.e., I would expect each line of output to show the same two numbers). When (type & 1) is set, I would also expect to see a larger result than when the bit is clear based on the manual saying "if [the least significant bit ] is set, a closest surrounding subobject is considered the object a pointer points to." $ (set -x && cat xyz.c && for N in 1 i; do /build/gcc-trunk-svn/gcc/xgcc -B /build/gcc-trunk-svn/gcc -DN=$N -O2 xyz.c && ./a.out; done) + cat xyz.c struct __attribute__ ((packed)) A { char a [3]; char b [5]; }; struct A a; int main (void) { int i = 1; __builtin_printf ("type 0: %zu %zu\n" "type 1: %zu %zu\n" "type 2: %zu %zu\n" "type 3: %zu %zu\n", __builtin_object_size (&a.a[0] + N, 0), __builtin_object_size (&a.a[N] + 0, 0), __builtin_object_size (&a.a[0] + N, 1), __builtin_object_size (&a.a[N] + 0, 1), __builtin_object_size (&a.a[0] + N, 2), __builtin_object_size (&a.a[N] + 0, 2), __builtin_object_size (&a.a[0] + N, 3), __builtin_object_size (&a.a[N] + 0, 3)); } + for N in 1 i + /build/gcc-trunk-svn/gcc/xgcc -B /build/gcc-trunk-svn/gcc -DN=1 -O2 xyz.c + ./a.out type 0: 7 7 type 1: 2 2 type 2: 7 7 type 3: 7 2 + for N in 1 i + /build/gcc-trunk-svn/gcc/xgcc -B /build/gcc-trunk-svn/gcc -DN=i -O2 xyz.c + ./a.out type 0: 7 7 type 1: 7 7 type 2: 7 7 type 3: 7 7