public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/57180] New: Structures with a flexible arrray member have wrong size
@ 2013-05-05 23:42 amodra at gmail dot com
  2013-05-06  8:53 ` [Bug c/57180] " mikpe at it dot uu.se
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: amodra at gmail dot com @ 2013-05-05 23:42 UTC (permalink / raw)
  To: gcc-bugs


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

             Bug #: 57180
           Summary: Structures with a flexible arrray member have wrong
                    size
    Classification: Unclassified
           Product: gcc
           Version: 4.9.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: amodra@gmail.com
                CC: algrant@acm.org, amodra@gmail.com,
                    fredrickprashanth@gmail.com, gcc-bugs@gcc.gnu.org,
                    lauro.venancio@gmail.com, qrczak@knm.org.pl,
                    sbsiddha@gmail.com
        Depends on: 28865


+++ This bug was initially created as a clone of Bug #28865 +++

Cloned from 28865, because the problem is more than just getting .size wrong.
The following testcase fails on all targets at all optimization levels.
The testcase was taken from glibc/nss/nss_files/files-init.c, which is
miscompiled on powerpc or any other target using -fsection-anchors.

union
{
  struct { long j; char c[]; } s;
  char pad[32];
} x[2] =
{
  { .s = { .c = "abc123" } },
  { .s = { .c = "xyz" } }
};

int
main (void)
{
  if (sizeof (x[0]) != 32)
    __builtin_abort ();
  if (x[1].s.c[0] != 'x')
    __builtin_abort ();
  return 0;
}


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c/57180] Structures with a flexible arrray member have wrong size
  2013-05-05 23:42 [Bug c/57180] New: Structures with a flexible arrray member have wrong size amodra at gmail dot com
@ 2013-05-06  8:53 ` mikpe at it dot uu.se
  2013-05-06  9:37 ` amodra at gmail dot com
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: mikpe at it dot uu.se @ 2013-05-06  8:53 UTC (permalink / raw)
  To: gcc-bugs


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

Mikael Pettersson <mikpe at it dot uu.se> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mikpe at it dot uu.se

--- Comment #1 from Mikael Pettersson <mikpe at it dot uu.se> 2013-05-06 08:53:37 UTC ---
This testcase fails on armv5tel-linux-gnueabi with (at least) gcc 4.4, 4.6,
4.7, and 4.8.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c/57180] Structures with a flexible arrray member have wrong size
  2013-05-05 23:42 [Bug c/57180] New: Structures with a flexible arrray member have wrong size amodra at gmail dot com
  2013-05-06  8:53 ` [Bug c/57180] " mikpe at it dot uu.se
@ 2013-05-06  9:37 ` amodra at gmail dot com
  2013-05-09 19:20 ` mikpe at it dot uu.se
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: amodra at gmail dot com @ 2013-05-06  9:37 UTC (permalink / raw)
  To: gcc-bugs


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

Alan Modra <amodra at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2013-05-06
     Ever Confirmed|0                           |1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c/57180] Structures with a flexible arrray member have wrong size
  2013-05-05 23:42 [Bug c/57180] New: Structures with a flexible arrray member have wrong size amodra at gmail dot com
  2013-05-06  8:53 ` [Bug c/57180] " mikpe at it dot uu.se
  2013-05-06  9:37 ` amodra at gmail dot com
@ 2013-05-09 19:20 ` mikpe at it dot uu.se
  2013-05-11  9:18 ` mikpe at it dot uu.se
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: mikpe at it dot uu.se @ 2013-05-09 19:20 UTC (permalink / raw)
  To: gcc-bugs

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.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c/57180] Structures with a flexible arrray member have wrong size
  2013-05-05 23:42 [Bug c/57180] New: Structures with a flexible arrray member have wrong size amodra at gmail dot com
                   ` (2 preceding siblings ...)
  2013-05-09 19:20 ` mikpe at it dot uu.se
@ 2013-05-11  9:18 ` mikpe at it dot uu.se
  2014-01-16 12:19 ` nickc at gcc dot gnu.org
  2014-01-23 11:45 ` mpolacek at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: mikpe at it dot uu.se @ 2013-05-11  9:18 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Mikael Pettersson <mikpe at it dot uu.se> ---
According to
<http://gcc.gnu.org/onlinedocs/gcc-4.8.0/gcc/Zero-Length.html#Zero-Length>,
arrays of structures with trailing flex arrays are invalid and rejected. The
page also gives an example of that, but changing it to use a char array with
either a string literal initializer or a { } one shows that only the { } form
is rejected:

> cat pr57180-2.c
struct foo { int x; char y[]; };
struct foo a[1] = { { 1, "ab" } };
struct foo b[1] = { { 1, { 'a', 'b', '\0' } } };
> gcc -Wall -S pr57180-2.c
pr57180-2.c:3:8: error: initialization of flexible array member in a nested
context
 struct foo b[1] = { { 1, { 'a', 'b', '\0' } } };
        ^
pr57180-2.c:3:8: error: (near initialization for 'b[0].y')

Accepting the a[] initializer while rejecting the b[] one seems broken.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c/57180] Structures with a flexible arrray member have wrong size
  2013-05-05 23:42 [Bug c/57180] New: Structures with a flexible arrray member have wrong size amodra at gmail dot com
                   ` (3 preceding siblings ...)
  2013-05-11  9:18 ` mikpe at it dot uu.se
@ 2014-01-16 12:19 ` nickc at gcc dot gnu.org
  2014-01-23 11:45 ` mpolacek at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: nickc at gcc dot gnu.org @ 2014-01-16 12:19 UTC (permalink / raw)
  To: gcc-bugs

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

Bug 57180 depends on bug 28865, which changed state.

Bug 28865 Summary: Structures with a flexible arrray member have wrong .size
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28865

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c/57180] Structures with a flexible arrray member have wrong size
  2013-05-05 23:42 [Bug c/57180] New: Structures with a flexible arrray member have wrong size amodra at gmail dot com
                   ` (4 preceding siblings ...)
  2014-01-16 12:19 ` nickc at gcc dot gnu.org
@ 2014-01-23 11:45 ` mpolacek at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2014-01-23 11:45 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mpolacek at gcc dot gnu.org

--- Comment #4 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Seems to be fixed on trunk.


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2014-01-23 11:45 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-05-05 23:42 [Bug c/57180] New: Structures with a flexible arrray member have wrong size amodra at gmail dot com
2013-05-06  8:53 ` [Bug c/57180] " mikpe at it dot uu.se
2013-05-06  9:37 ` amodra at gmail dot com
2013-05-09 19:20 ` mikpe at it dot uu.se
2013-05-11  9:18 ` mikpe at it dot uu.se
2014-01-16 12:19 ` nickc at gcc dot gnu.org
2014-01-23 11:45 ` mpolacek at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).