https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69697
Bug ID: 69697 Summary: incorrect initialization of static flexible array members Product: gcc Version: 6.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- Similar to bug 69696, G++ accepts the questionable program below but emits incorrect code overlaying the flexible array member over the variable j. This can be seen in the generated assembly file (below). The underlying problem is that G++ emits a .size directive that corresponds to the size of the type as opposed to the ultimate size of the object (or its largest initializer). This was fixed in the C front end via bug 28865. .globl i .bss .align 4 .type i, @object .size i, 4 i: .zero 4 .globl a .align 4 .type a, @object .size a, 4 a: .zero 4 .globl j .align 4 .type j, @object .size j, 4 j: .zero 4 G++ should either reject such code with an error or it should lay down the global variables consecutively. $ cat x.c && /home/msebor/build/gcc-trunk-svn/gcc/xgcc -B/home/msebor/build/gcc-trunk-svn/gcc -Wall -Wextra -Wpedantic -std=c++14 -xc++ x.c && ./a.out int i; struct A { int n, a[]; } a = i ? A({ 1, { 2 } }) : A({ 2, { 3, 4 } }); int j; int main () { __builtin_printf ("i = %i, j = %i, a = { %i, { ", i, j, a.n); for (int i = 0; i != a.n; ++i) __builtin_printf ("%i, ", a.a[i]); __builtin_printf ("} }\n"); } x.c:2:48: warning: initialization of a flexible array member [-Wpedantic] struct A { int n, a[]; } a = i ? A({ 1, { 2 } }) : A({ 2, { 3, 4 } }); ^ x.c:2:69: warning: initialization of a flexible array member [-Wpedantic] struct A { int n, a[]; } a = i ? A({ 1, { 2 } }) : A({ 2, { 3, 4 } }); ^ i = 0, j = 3, a = { 2, { 3, 4, } }