[Bug c/57180] Structures with a flexible arrray member have wrong size

mikpe at it dot uu.se gcc-bugzilla@gcc.gnu.org
Thu May 9 19:20:00 GMT 2013


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57180

--- Comment #2 from Mikael Pettersson <mikpe at it dot uu.se> ---
This test case also fails on x86_64-linux with every gcc release from 3.2.3 up
to today's 4.9 (r198748).  Looking at the assembly code for the x[] initializer
it's easy to see why:

        .type   x, @object
        .size   x, 64
x:
        .zero   8
        .string "abc123"
        .zero   24
        .zero   8
        .string "xyz"
        .zero   24

The ".zero 24" is there to pad the initializer up to the type size, but it
isn't adjusted for the flex array initializer, so too much data is emitted for
x[0], causing x[1]'s initializer to start at the wrong address.

The error check that x[1].s.c[0] != 'x' is compiled as:

        cmpb    $120, x+40(%rip)

and it triggers because the 'x' is actually at x+8+7+24+8 i.e. x+47.

I can't say I'm a fan of flex arrays in global variables, but they clearly are
severely broken when those variables are arrays.



More information about the Gcc-bugs mailing list