[Bug c++/68489] arrays of flexible array members are silently accepted

msebor at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Mon Feb 1 20:56:00 GMT 2016


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68489

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2016-02-01
     Ever confirmed|0                           |1
      Known to fail|                            |6.0

--- Comment #1 from Martin Sebor <msebor at gcc dot gnu.org> ---
Bug 28865 provides useful background on this problem.  GCC (in C mode) used to
accept definitions of initialized array objects of structures with flexible
array members.  That feature was removed in response to bug 28865 which pointed
out that the size of such objects (emitted into the assembly in the form of the
.size directive) is incorrect.  G++ 6.0 suffers from both of these problems
because it accepts definitions of such objects.  Prior versions rejected such
code.

The following test case demonstrates the consequences of this problem:

$ cat t.c && /home/msebor/build/gcc-trunk-git/gcc/xgcc
-B/home/msebor/build/gcc-trunk-git/gcc -Wall -Wextra -Wpedantic -std=c++11
-xc++ t.c && ./a.out 
struct S { char x; int y []; };

struct S s[2] = {
    { 1, { 2, 3 } },
    { 4, { 5, 6 } }
};

int main (void) {
    __builtin_printf ("sizeof s = %zu\n"
                      "s[0] @%p = { %hhi, { %i, %i } }\n"
                      "s[1] @%p = { %hhi, { %i, %i } }\n",
                      sizeof (struct S),
                      &s [0], s[0].x, s[0].y [0], s[0].y [1],
                      &s [1], s[1].x, s[1].y [0], s[1].y [1]);

    if (   s [0].x != 1 || s [0].y [0] != 2 || s [0].y [1] != 3
        || s [1].x != 4 || s [1].y [0] != 5 || s [1].y [1] != 6)
        __builtin_abort ();
}

t.c:6:1: warning: initialization of a flexible array member [-Wpedantic]
 };
 ^
t.c:6:1: warning: initialization of a flexible array member [-Wpedantic]
t.c: In function ‘int main()’:
t.c:14:61: warning: format ‘%p’ expects argument of type ‘void*’, but argument
3 has type ‘S*’ [-Wformat=]
                       &s [1], s[1].x, s[1].y [0], s[1].y [1]);
                                                             ^
t.c:14:61: warning: format ‘%p’ expects argument of type ‘void*’, but argument
7 has type ‘S*’ [-Wformat=]
sizeof s = 4
s[0] @0x601048 = { 1, { 2, 3 } }
s[1] @0x60104c = { 2, { 3, 4 } }
Aborted (core dumped)


More information about the Gcc-bugs mailing list