This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c/77650] New: struct with a nested flexible array followed by another member accepted


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

            Bug ID: 77650
           Summary: struct with a nested flexible array followed by
                    another member accepted
           Product: gcc
           Version: 7.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: ---

While testing my patch for C++ bug 71912 and gathering material for WG14 paper
N2083 I noticed that GCC isn't completely consistent in diagnosing invalid uses
of flexible array members.  In the following example, it rejects the definition
of struct Y with a hard error because the flexible array member a is followed
by another member, j.  But GCC issues only a pedantic warning for the similar
definition of struct T where the flexible array member defined in a nested
struct is also followed by another member.  I think it would be better (more
consistent and safer(*)) if both structs were rejected with an error.

$ cat v.c && /build/gcc-trunk-git/gcc/xgcc -B /build/gcc-trunk-git/gcc -O2 -S
-Wall -Wextra -Wpedantic v.c
struct X { int i, a[]; };
struct Y { int i, a[], j; };

struct S { struct X x; };
struct T { struct X x; int j; };
v.c:2:19: error: flexible array member not at end of struct
 struct Y { int i, a[], j; };
                   ^
v.c:4:21: warning: invalid use of structure with flexible array member
[-Wpedantic]
 struct S { struct X x; };
                     ^
v.c:5:21: warning: invalid use of structure with flexible array member
[-Wpedantic]
 struct T { struct X x; int j; };
                     ^

Rejecting the flexible array would be safer because with the definition of
struct Y commented out, a complete program with the definition of main below
aborts, indicating that GCC assumes that members of struct T do not alias. 
That assumption is violated by allowing the flexible array member to be
followed by another member in an enclosing struct.

void f (struct T *t, struct X *x)
{
  t->j = 0x12345678;
  x->a [0] = 0xdeadbeef;
  if (0 < t->j)
    __builtin_abort ();
}

int main (void)
{
  struct T *t = __builtin_malloc (sizeof (struct T));
  f (t, &t->x);
  return 0;
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]