public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [RFC][PATCH v1 0/4] Allow flexible array members in unions and alone in structures [PR53548]
@ 2024-04-19 18:43 Qing Zhao
  2024-04-19 18:43 ` [RFC][PATCH v1 1/4] Documentation change Qing Zhao
                   ` (4 more replies)
  0 siblings, 5 replies; 16+ messages in thread
From: Qing Zhao @ 2024-04-19 18:43 UTC (permalink / raw)
  To: josmyers, richard.guenther, uecker
  Cc: siddhesh, keescook, gcc-patches, Qing Zhao

Hi,

The request for GCC to accept that the C99 flexible array member can be
in a union or alone in a struct has been made a long time ago around 2012 
for supporting several practical cases including glibc.

A GCC PR has been opened for such request at that time:

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

However, this PR was closed as WONTFIX around 2015 due to the following reason:

"there is an existing extension that makes the requested functionality possible"
i.e GCC fully supported that the zero-length array can be in union or alone
in structs for a long time. (though I didn't see any official documentation on
such extension)

It's reasonable to close PR53548 at that time since zero-length array extension
can be used for such purpose.

However, since GCC13, in order to improve the C/C++ security, we introduced
-fstrict-flex-arrays=n to gradually eliminate the "fake flexible array"
usages from C/C++ source code. As a result, zero-lenghth arrays eventually 
will be replaced by C99 flexiable array member completely.   

Therefore, GCC needs to explicitly allow such extensions directly for C99
flexible arrays, since flexable array member in unions or alone in structs
are common code patterns in active use by the Linux kernel (and other projects).

For example, these do not error by default with GCC:

union one {
	int a;
	int b[0];
};

union two {
	int a;
	struct {
		struct { } __empty;
		int b[];
	};
};

But these do:

union three {
	int a;
	int b[];
};

struct four {
  	int b[];
}

Clang has supported such extensions since March, 2024
https://github.com/llvm/llvm-project/pull/84428

GCC should also support such extensions. This will allow for
a seamless transition for code bases away from zero-length arrays without
losing existing code patterns. 

The patch set includes:

  1. Documentation change.
     Allow flexible array members in unions and alone in structures
     [PR53548]
  2. C and C++ FE changes to support flexible array members in unions and
    alone in structures.
  3. Add testing cases for flexible array members in unions and alone in
    structures.
  4. Adjust testcases for flexible array member in union and alone in
    structure extension.


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

* [RFC][PATCH v1 1/4] Documentation change
  2024-04-19 18:43 [RFC][PATCH v1 0/4] Allow flexible array members in unions and alone in structures [PR53548] Qing Zhao
@ 2024-04-19 18:43 ` Qing Zhao
  2024-04-19 20:54   ` Tom Tromey
  2024-04-23 18:04   ` Joseph Myers
  2024-04-19 18:43 ` [RFC][PATCH v1 2/4] C and C++ FE changes to support flexible array members in unions and alone in structures Qing Zhao
                   ` (3 subsequent siblings)
  4 siblings, 2 replies; 16+ messages in thread
From: Qing Zhao @ 2024-04-19 18:43 UTC (permalink / raw)
  To: josmyers, richard.guenther, uecker
  Cc: siddhesh, keescook, gcc-patches, Qing Zhao

for allow flexible array members in unions and alone in structures [PR53548]

The request for GCC to accept that the C99 flexible array member can be
in a union or alone in a structure has been made a long time ago around 2012
for supporting several practical cases including glibc.

A GCC PR has been opened for such request at that time:

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

However, this PR was closed as WONTFIX around 2015 due to the following reason:

"there is an existing extension that makes the requested functionality possible"
i.e GCC fully supported that the zero-length array can be in a union or alone
in a structure for a long time. (though I didn't see any official documentation
on such extension)

It's reasonable to close PR53548 at that time since zero-length array extension
can be used for such purpose.

However, since GCC13, in order to improve the C/C++ security, we introduced
-fstrict-flex-arrays=n to gradually eliminate the "fake flexible array"
usages from C/C++ source code. As a result, zero-length arrays eventually
will be replaced by C99 flexiable array member completely.

Therefore, GCC needs to explicitly allow such extensions directly for C99
flexible arrays, since flexable array member in unions or alone in structs
are common code patterns in active use by the Linux kernel (and other projects).

For example, these do not error by default with GCC:

union one {
  int a;
  int b[0];
};

union two {
  int a;
  struct {
    struct { } __empty;
    int b[];
  };
};

But these do:

union three {
  int a;
  int b[];
};

struct four {
  int b[];
}

Clang has supported such extensions since March, 2024
https://github.com/llvm/llvm-project/pull/84428

GCC should also support such extensions. This will allow for
a seamless transition for code bases away from zero-length arrays without
losing existing code patterns.

gcc/ChangeLog:

	* doc/extend.texi: Add documentation for Flexible Array Members in
	Unions and Flexible Array Members alone in Structures.
---
 gcc/doc/extend.texi | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)

diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index 7b54a241a7bf..b12ce5fb9b87 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -42,6 +42,8 @@ extensions, accepted by GCC in C90 mode and in C++.
 * Named Address Spaces::Named address spaces.
 * Zero Length::         Zero-length arrays.
 * Empty Structures::    Structures with no members.
+* Flexible Array Members in Unions::  Unions with Flexible Array Members.
+* Flexible Array Members alone in Structures::  Structures with only Flexible Array Members.
 * Variable Length::     Arrays whose length is computed at run time.
 * Variadic Macros::     Macros with a variable number of arguments.
 * Escaped Newlines::    Slightly looser rules for escaped newlines.
@@ -1873,6 +1875,41 @@ The structure has size zero.  In C++, empty structures are part
 of the language.  G++ treats empty structures as if they had a single
 member of type @code{char}.
 
+@node Flexible Array Members in Unions
+@section Unions with Flexible Array Members
+@cindex unions with flexible array members
+@cindex unions with FAMs
+
+GCC permits a C99 flexible array member (FAM) to be in a union:
+
+@smallexample
+union with_fam @{
+  int a;
+  int b[];
+@};
+@end smallexample
+
+The size of the union is as if the flexiable array member were omitted
+except that it may have more trailing padding than the omission would imply.
+
+If all the members of a union are flexiable array member, the size of 
+such union is zero.
+
+@node Flexible Array Members alone in Structures
+@section Structures with only Flexible Array Members
+@cindex structures with only flexible array members
+@cindex structures with only FAMs
+
+GCC permits a C99 flexible array member (FAM) to be alone in a structure:
+
+@smallexample
+struct only_fam @{
+  int b[];
+@};
+@end smallexample
+
+The size of such structure gives the size zero.
+
 @node Variable Length
 @section Arrays of Variable Length
 @cindex variable-length arrays
-- 
2.31.1


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

* [RFC][PATCH v1 2/4] C and C++ FE changes to support flexible array members in unions and alone in structures.
  2024-04-19 18:43 [RFC][PATCH v1 0/4] Allow flexible array members in unions and alone in structures [PR53548] Qing Zhao
  2024-04-19 18:43 ` [RFC][PATCH v1 1/4] Documentation change Qing Zhao
@ 2024-04-19 18:43 ` Qing Zhao
  2024-04-23 19:51   ` Joseph Myers
  2024-04-19 18:43 ` [RFC][PATCH v1 3/4] Add testing cases for " Qing Zhao
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 16+ messages in thread
From: Qing Zhao @ 2024-04-19 18:43 UTC (permalink / raw)
  To: josmyers, richard.guenther, uecker
  Cc: siddhesh, keescook, gcc-patches, Qing Zhao

gcc/c/ChangeLog:

	* c-decl.cc (finish_struct): Change errors to pedwarns for the cases
	flexible array members in union or alone in structures.

gcc/cp/ChangeLog:

	* class.cc (diagnose_flexarrays): Change error to pdewarn for the case
	flexible array members alone in structures.
	* decl.cc (grokdeclarator): Change error to pdewarn for the case
	flexible array members in unions.

gcc/ChangeLog:

	* stor-layout.cc (place_union_field): Use zero sizes for flexible array
	member fields.
---
 gcc/c/c-decl.cc    | 16 +++++-----------
 gcc/cp/class.cc    | 11 ++++++++---
 gcc/cp/decl.cc     |  7 +++----
 gcc/stor-layout.cc |  9 +++++++--
 4 files changed, 23 insertions(+), 20 deletions(-)

diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc
index 345090dae38b..947f3cd589eb 100644
--- a/gcc/c/c-decl.cc
+++ b/gcc/c/c-decl.cc
@@ -9471,11 +9471,8 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
       if (flexible_array_member_type_p (TREE_TYPE (x)))
 	{
 	  if (TREE_CODE (t) == UNION_TYPE)
-	    {
-	      error_at (DECL_SOURCE_LOCATION (x),
-			"flexible array member in union");
-	      TREE_TYPE (x) = error_mark_node;
-	    }
+	    pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
+		     "flexible array member in union is a GCC extension");
 	  else if (!is_last_field)
 	    {
 	      error_at (DECL_SOURCE_LOCATION (x),
@@ -9483,12 +9480,9 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
 	      TREE_TYPE (x) = error_mark_node;
 	    }
 	  else if (!saw_named_field)
-	    {
-	      error_at (DECL_SOURCE_LOCATION (x),
-			"flexible array member in a struct with no named "
-			"members");
-	      TREE_TYPE (x) = error_mark_node;
-	    }
+	    pedwarn (DECL_SOURCE_LOCATION (x), OPT_Wpedantic,
+		     "flexible array member in a struct with no named "
+		     "members is a GCC extension");
 	}
 
       if (pedantic && TREE_CODE (t) == RECORD_TYPE
diff --git a/gcc/cp/class.cc b/gcc/cp/class.cc
index 5f258729940b..0c8afb72550f 100644
--- a/gcc/cp/class.cc
+++ b/gcc/cp/class.cc
@@ -7624,6 +7624,7 @@ diagnose_flexarrays (tree t, const flexmems_t *fmem)
   bool diagd = false;
 
   const char *msg = 0;
+  const char *msg_fam = 0;
 
   if (TYPE_DOMAIN (TREE_TYPE (fmem->array)))
     {
@@ -7649,15 +7650,19 @@ diagnose_flexarrays (tree t, const flexmems_t *fmem)
       if (fmem->after[0])
 	msg = G_("flexible array member %qD not at end of %q#T");
       else if (!fmem->first)
-	msg = G_("flexible array member %qD in an otherwise empty %q#T");
+	msg_fam = G_("flexible array member %qD in an otherwise"
+		     " empty %q#T is a GCC extension");
 
-      if (msg)
+      if (msg || msg_fam)
 	{
 	  location_t loc = DECL_SOURCE_LOCATION (fmem->array);
 	  diagd = true;
 
 	  auto_diagnostic_group d;
-	  error_at (loc, msg, fmem->array, t);
+	  if (msg)
+	    error_at (loc, msg, fmem->array, t);
+	  else
+	    pedwarn (loc, OPT_Wpedantic, msg_fam, fmem->array, t);
 
 	  /* In the unlikely event that the member following the flexible
 	     array member is declared in a different class, or the member
diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 65ab64885ff8..9a91c6f80da1 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -14566,10 +14566,9 @@ grokdeclarator (const cp_declarator *declarator,
 	    if (ctype
 		&& (TREE_CODE (ctype) == UNION_TYPE
 		    || TREE_CODE (ctype) == QUAL_UNION_TYPE))
-	      {
-		error_at (id_loc, "flexible array member in union");
-		type = error_mark_node;
-	      }
+	      pedwarn (id_loc, OPT_Wpedantic,
+		       "flexible array member in union is a GCC extension");
+
 	    else
 	      {
 		/* Array is a flexible member.  */
diff --git a/gcc/stor-layout.cc b/gcc/stor-layout.cc
index e34be19689c0..10c0809914cd 100644
--- a/gcc/stor-layout.cc
+++ b/gcc/stor-layout.cc
@@ -1245,13 +1245,18 @@ place_union_field (record_layout_info rli, tree field)
       && TYPE_TYPELESS_STORAGE (TREE_TYPE (field)))
     TYPE_TYPELESS_STORAGE (rli->t) = 1;
 
+  /* We might see a flexible array member field (with no DECL_SIZE_UNIT), use
+     zero size for such field.  */
+  tree field_size_unit = DECL_SIZE_UNIT (field)
+			 ? DECL_SIZE_UNIT (field)
+			 : build_int_cst (sizetype, 0);
   /* We assume the union's size will be a multiple of a byte so we don't
      bother with BITPOS.  */
   if (TREE_CODE (rli->t) == UNION_TYPE)
-    rli->offset = size_binop (MAX_EXPR, rli->offset, DECL_SIZE_UNIT (field));
+    rli->offset = size_binop (MAX_EXPR, rli->offset, field_size_unit);
   else if (TREE_CODE (rli->t) == QUAL_UNION_TYPE)
     rli->offset = fold_build3 (COND_EXPR, sizetype, DECL_QUALIFIER (field),
-			       DECL_SIZE_UNIT (field), rli->offset);
+			       field_size_unit, rli->offset);
 }
 
 /* A bitfield of SIZE with a required access alignment of ALIGN is allocated
-- 
2.31.1


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

* [RFC][PATCH v1 3/4] Add testing cases for flexible array members in unions and alone in structures.
  2024-04-19 18:43 [RFC][PATCH v1 0/4] Allow flexible array members in unions and alone in structures [PR53548] Qing Zhao
  2024-04-19 18:43 ` [RFC][PATCH v1 1/4] Documentation change Qing Zhao
  2024-04-19 18:43 ` [RFC][PATCH v1 2/4] C and C++ FE changes to support flexible array members in unions and alone in structures Qing Zhao
@ 2024-04-19 18:43 ` Qing Zhao
  2024-04-23 18:53   ` Joseph Myers
  2024-04-19 18:43 ` [RFC][PATCH v1 4/4] Adjust testcases for flexible array member in union and alone in structure extension Qing Zhao
  2024-04-19 21:55 ` [RFC][PATCH v1 0/4] Allow flexible array members in unions and alone in structures [PR53548] Kees Cook
  4 siblings, 1 reply; 16+ messages in thread
From: Qing Zhao @ 2024-04-19 18:43 UTC (permalink / raw)
  To: josmyers, richard.guenther, uecker
  Cc: siddhesh, keescook, gcc-patches, Qing Zhao

gcc/testsuite/ChangeLog:

	* gcc.dg/flex-array-in-union-1.c: New test.
	* gcc.dg/flex-array-in-union-2.c: New test.
---
 gcc/testsuite/gcc.dg/flex-array-in-union-1.c | 37 +++++++++++++++++
 gcc/testsuite/gcc.dg/flex-array-in-union-2.c | 42 ++++++++++++++++++++
 2 files changed, 79 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/flex-array-in-union-1.c
 create mode 100644 gcc/testsuite/gcc.dg/flex-array-in-union-2.c

diff --git a/gcc/testsuite/gcc.dg/flex-array-in-union-1.c b/gcc/testsuite/gcc.dg/flex-array-in-union-1.c
new file mode 100644
index 000000000000..2a532d77c1dd
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/flex-array-in-union-1.c
@@ -0,0 +1,37 @@
+/* testing the correct usage of flexible array members in unions 
+   and alone in structure.  */
+/* { dg-do run} */
+/* { dg-options "-O2 -Wpedantic" } */
+
+union with_fam_1 {
+  int a;
+  int b[];  /* { dg-warning "flexible array member in union is a GCC extension" } */
+};
+
+union with_fam_2 {
+  char a;
+  int b[];  /* { dg-warning "flexible array member in union is a GCC extension" } */
+};
+
+union with_fam_3 {
+  char a[];  /* { dg-warning " flexible array member in union is a GCC extension" } */
+  int b[];  /* { dg-warning "flexible array member in union is a GCC extension" } */
+};
+
+struct only_fam {
+  int b[];  /* { dg-warning "flexible array member in a struct with no named members is a GCC extension" } */
+};
+
+int main ()
+{
+  if (sizeof (union with_fam_1) != sizeof (int))
+    __builtin_abort ();
+  if (sizeof (union with_fam_2) != __alignof__ (int))
+    __builtin_abort ();
+  if (sizeof (union with_fam_3) != 0)
+    __builtin_abort ();
+  if (sizeof (struct only_fam) != 0)
+    __builtin_abort ();
+  return 0;
+}
+
diff --git a/gcc/testsuite/gcc.dg/flex-array-in-union-2.c b/gcc/testsuite/gcc.dg/flex-array-in-union-2.c
new file mode 100644
index 000000000000..130124bbe653
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/flex-array-in-union-2.c
@@ -0,0 +1,42 @@
+/* testing the correct usage of flexible array members in unions 
+   and alone in structure: initialization  */
+/* { dg-do run} */
+/* { dg-options "-O2" } */
+
+union with_fam_1 {
+  int a;
+  int b[]; 
+} with_fam_1_v = {.b = {1, 2, 3, 4}};
+
+union with_fam_2 {
+  int a;
+  char b[];  
+} with_fam_2_v = {.a = 0x1f2f3f4f};
+
+union with_fam_3 {
+  char a[];  
+  int b[];  
+} with_fam_3_v = {.b = {0x1f2f3f4f, 0x5f6f7f7f}};
+
+struct only_fam {
+  int b[]; 
+} only_fam_v = {{7, 11}};
+
+int main ()
+{
+  if (with_fam_1_v.b[3] != 4
+      || with_fam_1_v.b[0] != 1)
+    __builtin_abort ();
+  if (with_fam_2_v.b[3] != 0x1f
+      || with_fam_2_v.b[0] != 0x4f)
+    __builtin_abort ();
+  if (with_fam_3_v.a[0] != 0x4f
+      || with_fam_3_v.a[7] != 0x5f)
+    __builtin_abort ();
+  if (only_fam_v.b[0] != 7
+      || only_fam_v.b[1] != 11)
+    __builtin_abort ();
+
+  return 0;
+}
+
-- 
2.31.1


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

* [RFC][PATCH v1 4/4] Adjust testcases for flexible array member in union and alone in structure extension.
  2024-04-19 18:43 [RFC][PATCH v1 0/4] Allow flexible array members in unions and alone in structures [PR53548] Qing Zhao
                   ` (2 preceding siblings ...)
  2024-04-19 18:43 ` [RFC][PATCH v1 3/4] Add testing cases for " Qing Zhao
@ 2024-04-19 18:43 ` Qing Zhao
  2024-04-19 21:55 ` [RFC][PATCH v1 0/4] Allow flexible array members in unions and alone in structures [PR53548] Kees Cook
  4 siblings, 0 replies; 16+ messages in thread
From: Qing Zhao @ 2024-04-19 18:43 UTC (permalink / raw)
  To: josmyers, richard.guenther, uecker
  Cc: siddhesh, keescook, gcc-patches, Qing Zhao

gcc/testsuite/ChangeLog:

	* c-c++-common/builtin-clear-padding-3.c: Adjust testcase.
	* g++.dg/ext/flexary12.C: Likewise.
	* g++.dg/ext/flexary19.C: Likewise.
	* g++.dg/ext/flexary2.C: Likewise.
	* g++.dg/ext/flexary3.C: Likewise.
	* g++.dg/ext/flexary36.C: Likewise.
	* g++.dg/ext/flexary4.C: Likewise.
	* g++.dg/ext/flexary5.C: Likewise.
	* g++.dg/ext/flexary8.C: Likewise.
	* g++.dg/torture/pr64280.C: Likewise.
	* gcc.dg/20050620-1.c: Likewise.
	* gcc.dg/940510-1.c: Likewise.
---
 .../c-c++-common/builtin-clear-padding-3.c    | 10 ++--
 gcc/testsuite/g++.dg/ext/flexary12.C          |  6 +--
 gcc/testsuite/g++.dg/ext/flexary19.C          | 42 +++++++--------
 gcc/testsuite/g++.dg/ext/flexary2.C           |  2 +-
 gcc/testsuite/g++.dg/ext/flexary3.C           |  2 +-
 gcc/testsuite/g++.dg/ext/flexary36.C          |  2 +-
 gcc/testsuite/g++.dg/ext/flexary4.C           | 54 +++++++++----------
 gcc/testsuite/g++.dg/ext/flexary5.C           |  4 +-
 gcc/testsuite/g++.dg/ext/flexary8.C           |  8 +--
 gcc/testsuite/g++.dg/torture/pr64280.C        |  2 +-
 gcc/testsuite/gcc.dg/20050620-1.c             |  2 +-
 gcc/testsuite/gcc.dg/940510-1.c               |  4 +-
 12 files changed, 68 insertions(+), 70 deletions(-)

diff --git a/gcc/testsuite/c-c++-common/builtin-clear-padding-3.c b/gcc/testsuite/c-c++-common/builtin-clear-padding-3.c
index d16cc6aad05f..a4f49f26db14 100644
--- a/gcc/testsuite/c-c++-common/builtin-clear-padding-3.c
+++ b/gcc/testsuite/c-c++-common/builtin-clear-padding-3.c
@@ -2,14 +2,12 @@
 /* { dg-do compile } */
 /* { dg-options "" } */
 
-union U { int a; char b[] __attribute__((aligned (2 * sizeof (int)))); };	/* { dg-error "flexible array member in union" } */
+union U { int a; char b[] __attribute__((aligned (2 * sizeof (int)))); };
 struct V { int a; union U b; };
-struct W { int a; union U b; int c; };
 
 void
-foo (union U *u, struct V *v, struct W *w)
+foo (union U *u, struct V *v)
 {
-  __builtin_clear_padding (u);
-  __builtin_clear_padding (v);
-  __builtin_clear_padding (w);
+  __builtin_clear_padding (u); /* { dg-error "flexible array member" "does not have well defined padding bits" } */
+  __builtin_clear_padding (v); /* { dg-error "flexible array member" "does not have well defined padding bits" } */
 }
diff --git a/gcc/testsuite/g++.dg/ext/flexary12.C b/gcc/testsuite/g++.dg/ext/flexary12.C
index b0964948731d..6ba4b6417135 100644
--- a/gcc/testsuite/g++.dg/ext/flexary12.C
+++ b/gcc/testsuite/g++.dg/ext/flexary12.C
@@ -6,7 +6,7 @@
 // { dg-options "-Wno-pedantic" }
 
 struct A {
-  int a [];  // { dg-error "flexible array member .A::a. in an otherwise empty .struct A." }
+  int a [];
 };
 
 void f1 ()
@@ -40,7 +40,7 @@ void f2 ()
 }
 
 struct D {
-  int a [];  // { dg-error "flexible array member .D::a. in an otherwise empty .struct D." }
+  int a [];
   D ();
 };
 
@@ -52,7 +52,7 @@ D::D ():    // { dg-error "initializer for flexible array member" }
 
 template <class T>
 struct C {
-  T a [];  // { dg-error "flexible array member" }
+  T a [];
 };
 
 void f3 ()
diff --git a/gcc/testsuite/g++.dg/ext/flexary19.C b/gcc/testsuite/g++.dg/ext/flexary19.C
index abfbc43028af..9a06f9ca758f 100644
--- a/gcc/testsuite/g++.dg/ext/flexary19.C
+++ b/gcc/testsuite/g++.dg/ext/flexary19.C
@@ -12,7 +12,7 @@ struct S1
   // The following declares a named data member of an unnamed struct
   // (i.e., it is not an anonymous struct).
   struct {
-    int a[];        // { dg-error "in an otherwise empty" }
+    int a[];        // { dg-warning "in an otherwise empty" }
   } s;
 };
 
@@ -21,7 +21,7 @@ struct S2
   int i;
 
   struct {
-    int a[];        // { dg-error "in an otherwise empty" }
+    int a[];        // { dg-warning "in an otherwise empty" }
   } s[1];
 };
 
@@ -30,7 +30,7 @@ struct S3
   int i;
 
   struct {
-    int a[];        // { dg-error "in an otherwise empty" }
+    int a[];        // { dg-warning "in an otherwise empty" }
   } s[];
 };
 
@@ -39,7 +39,7 @@ struct S4
   int i;
 
   struct {
-    int a[];        // { dg-error "in an otherwise empty" }
+    int a[];        // { dg-warning "in an otherwise empty" }
   } s[2];
 };
 
@@ -48,7 +48,7 @@ struct S5
   int i;
 
   struct {
-    int a[];        // { dg-error "in an otherwise empty" }
+    int a[];        // { dg-warning "in an otherwise empty" }
   } s[1][2];
 };
 
@@ -57,7 +57,7 @@ struct S6
   int i;
 
   struct {
-    int a[];        // { dg-error "in an otherwise empty" }
+    int a[];        // { dg-warning "in an otherwise empty" }
   } s[][2];
 };
 
@@ -66,7 +66,7 @@ struct S7
   int i;
 
   struct {
-    int a[];        // { dg-error "in an otherwise empty" }
+    int a[];        // { dg-warning "in an otherwise empty" }
   } *s;
 };
 
@@ -75,7 +75,7 @@ struct S8
   int i;
 
   struct {
-    int a[];        // { dg-error "in an otherwise empty" }
+    int a[];        // { dg-warning "in an otherwise empty" }
   } **s;
 };
 
@@ -84,7 +84,7 @@ struct S9
   int i;
 
   struct {
-    int a[];        // { dg-error "in an otherwise empty" }
+    int a[];        // { dg-warning "in an otherwise empty" }
   } *s[1];
 };
 
@@ -93,7 +93,7 @@ struct S10
   int i;
 
   struct {
-    int a[];        // { dg-error "in an otherwise empty" }
+    int a[];        // { dg-warning "in an otherwise empty" }
   } *s[];
 };
 
@@ -102,7 +102,7 @@ struct S11
   int i;
 
   struct {
-    int a[];        // { dg-error "in an otherwise empty" }
+    int a[];        // { dg-warning "in an otherwise empty" }
   } **s[1];
 };
 
@@ -111,7 +111,7 @@ struct S12
   int i;
 
   struct {
-    int a[];        // { dg-error "in an otherwise empty" }
+    int a[];        // { dg-warning "in an otherwise empty" }
   } **s[];
 };
 
@@ -120,7 +120,7 @@ struct S13
   int i;
 
   struct {
-    int a[];        // { dg-error "in an otherwise empty" }
+    int a[];        // { dg-warning "in an otherwise empty" }
   } **s[2];
 };
 
@@ -129,7 +129,7 @@ struct S14
   int i;
 
   struct {
-    int a[];        // { dg-error "in an otherwise empty" }
+    int a[];        // { dg-warning "in an otherwise empty" }
   } &s;
 };
 
@@ -138,7 +138,7 @@ struct S15
   int i;
 
   typedef struct {
-    int a[];        // { dg-error "in an otherwise empty" }
+    int a[];        // { dg-warning "in an otherwise empty" }
   } T15;
 };
 
@@ -159,8 +159,8 @@ struct S17
 {
   int i;
 
-  union {           // anonymous union
-    int a[];        // { dg-error "flexible array member in union" }
+  union {           // { dg-warning "invalid use" } 
+    int a[];        // { dg-warning "flexible array member in union" }
   };
 };
 
@@ -209,7 +209,7 @@ struct S22
   struct S22S {
     static int i;
 
-    int a[];        // { dg-error "in an otherwise empty" }
+    int a[];        // { dg-warning "in an otherwise empty" }
   } s;
 };
 
@@ -218,7 +218,7 @@ struct S23
   struct {          // { dg-warning "10:ISO C\\+\\+ prohibits anonymous struct" }
     static int i;   // { dg-error "static data member" }
 
-    int a[];        // { dg-error "in an otherwise empty" }
+    int a[];        // { dg-warning "in an otherwise empty" }
   };
 };
 
@@ -227,7 +227,7 @@ struct S24
   static int i;
 
   struct {
-    int a[];        // { dg-error "in an otherwise empty" }
+    int a[];        // { dg-warning "in an otherwise empty" }
   } s;
 };
 
@@ -252,7 +252,7 @@ struct S26
   };
 
   struct {
-    int a[];        // { dg-error "in an otherwise empty" }
+    int a[];        // { dg-warning "in an otherwise empty" }
   } s;
 };
 
diff --git a/gcc/testsuite/g++.dg/ext/flexary2.C b/gcc/testsuite/g++.dg/ext/flexary2.C
index c0253777a1e9..7095a3b0fb2d 100644
--- a/gcc/testsuite/g++.dg/ext/flexary2.C
+++ b/gcc/testsuite/g++.dg/ext/flexary2.C
@@ -13,7 +13,7 @@ struct A {
 
 struct B {
   B() {}
-  A a[];   // { dg-error "extension|flexible array .* in an otherwise empty" }
+  A a[];
 };
 
 struct C {
diff --git a/gcc/testsuite/g++.dg/ext/flexary3.C b/gcc/testsuite/g++.dg/ext/flexary3.C
index 8344b42dd163..7785c9b190a4 100644
--- a/gcc/testsuite/g++.dg/ext/flexary3.C
+++ b/gcc/testsuite/g++.dg/ext/flexary3.C
@@ -11,7 +11,7 @@
 // { dg-options "" }
 
 struct s {
-    char c[];   // { dg-error "flexible array member .* in an otherwise empty" }
+    char c[];
 };
 
 int main()
diff --git a/gcc/testsuite/g++.dg/ext/flexary36.C b/gcc/testsuite/g++.dg/ext/flexary36.C
index 5bb827cfd0e0..87d6fb092100 100644
--- a/gcc/testsuite/g++.dg/ext/flexary36.C
+++ b/gcc/testsuite/g++.dg/ext/flexary36.C
@@ -25,7 +25,7 @@ struct {
 
 
 union {
-  int a[];          // { dg-error "flexible array member in union" }
+  int a[];
   int b;
 } du = { 1 };
 
diff --git a/gcc/testsuite/g++.dg/ext/flexary4.C b/gcc/testsuite/g++.dg/ext/flexary4.C
index bd28cf55de2b..a87b7e71edb6 100644
--- a/gcc/testsuite/g++.dg/ext/flexary4.C
+++ b/gcc/testsuite/g++.dg/ext/flexary4.C
@@ -11,79 +11,79 @@
 #include "flexary.h"
 
 struct Sx {
-  int a[];                  // { dg-error "in an otherwise empty" }
+  int a[];
 };
 
 // Verify that non-data members or static data members either before
 // or after a flexible array member in an otherwise empty struct don't
 // suppress the diagnostic.
 struct Sx2 {
-  int a[];                  // { dg-error "in an otherwise empty" }
+  int a[];
   typedef int I;
 };
 
 struct Sx3 {
   typedef int I;
-  int a[];                  // { dg-error "in an otherwise empty" }
+  int a[];
 };
 
 struct Sx4 {
-  int a[];                  // { dg-error "in an otherwise empty" }
+  int a[];
   enum E { e };
 };
 
 struct Sx5 {
   enum E { e };
-  int a[];                  // { dg-error "in an otherwise empty" }
+  int a[];
 };
 
 struct Sx6 {
-  int a[];                  // { dg-error "in an otherwise empty" }
+  int a[];
   static int i;
 };
 
 struct Sx7 {
   static int i;
-  int a[];                  // { dg-error "in an otherwise empty" }
+  int a[];
 };
 
 struct Sx8 {
-  int a[];                  // { dg-error "in an otherwise empty" }
+  int a[];
   Sx8 () { }
 };
 
 struct Sx9 {
   Sx9 () { }
-  int a[];                  // { dg-error "in an otherwise empty" }
+  int a[];
 };
 
 struct Sx10 {
-  int a[];                  // { dg-error "in an otherwise empty" }
+  int a[];
   virtual ~Sx10 () { }
 };
 
 struct Sx11 {
   virtual ~Sx11 () { }
-  int a[];                  // { dg-error "in an otherwise empty" }
+  int a[];
 };
 
 struct Sx12 {
-  int a[];                  // { dg-error "in an otherwise empty" }
+  int a[];
   virtual void foo () = 0;
 };
 
 struct Sx13 {
   virtual void foo () = 0;
-  int a[];                  // { dg-error "in an otherwise empty" }
+  int a[];
 };
 
 struct Sx14 {
-  int a[][1];               // { dg-error "in an otherwise empty" }
+  int a[][1];
 };
 
 struct Sx15 {
   typedef int A[];
-  A a;                      // { dg-error "in an otherwise empty" }
+  A a;
 };
 
 // Verify also that a zero-size array doesn't suppress the diagnostic.
@@ -91,7 +91,7 @@ struct Sx16 {
   // a_0 below is diagnosed with -Wpedantic only and emits
   // warning: ISO C++ forbids zero-size arrays
   int a_0 [0];
-  int a_x [];               // { dg-error "in an otherwise empty" }
+  int a_x [];
 };
 
 struct Sx17 {
@@ -123,7 +123,7 @@ struct Sx19 {
 // so doesn't contribute its member to that of the enclosing struct.
 struct Sx20 {
   struct S { int i; };
-  int a_x [];               // { dg-error "in an otherwise empty" }
+  int a_x [];
 };
 
 struct Sx21 {
@@ -148,12 +148,12 @@ struct Sx24 {
 
 struct Sx25 {
   struct S { };
-  S a_x [];                 // { dg-error "flexible array member" }
+  S a_x [];
 };
 
 struct Sx26 {
   struct { }
-    a_x [];                   // { dg-error "flexible array member" }
+    a_x [];
 };
 
 struct Sx27 {
@@ -193,13 +193,13 @@ struct Sx32 {
 ASSERT_AT_END (Sx32, a);
 
 struct Sx33 {
-  int a [];                 // { dg-error "otherwise empty" }
+  int a [];
   friend int foo ();
 };
 
 struct Sx34 {
   friend int foo ();
-  int a [];                 // { dg-error "otherwise empty" }
+  int a [];
 };
 
 // Verify that intervening non-field declarations of members other
@@ -277,7 +277,7 @@ ASSERT_AT_END (Sx44, a);
 struct S_S_S_x {
   struct A {
     struct B {
-      int a[];              // { dg-error "flexible array member" }
+      int a[];
     } b;
   } a;
 };
@@ -300,7 +300,7 @@ struct NotAnon1 {
   // The following is not an anonymous struct -- the type is unnamed
   // but the object has a name.
   struct {
-    int bad[];              // { dg-error "otherwise empty" }
+    int bad[];
   } name;
 };
 
@@ -328,7 +328,7 @@ ASSERT_AT_END (Anon3, good);
 
 struct Anon4 {
   struct {
-    int in_empty_struct[];  // { dg-error "in an otherwise empty" }
+    int in_empty_struct[];
   };
 };
 
@@ -366,7 +366,7 @@ struct Six {
 ASSERT_AT_END (Six, a);
 
 class Cx {
-  int a[];                  // { dg-error "flexible array member" }
+  int a[];
 };
 
 class Cix {
@@ -390,7 +390,7 @@ struct S0i {
 
 struct S_a0_ax {
   int a0[0];
-  int ax[];                 // { dg-error "flexible array member" }
+  int ax[];
 };
 
 struct S_a0_i_ax {
@@ -417,7 +417,7 @@ struct Si_ax_a0 {
 
 struct S_u0_ax {
   union { } u[0];
-  int ax[];                 // { dg-error "flexible array member" }
+  int ax[];
 };
 
 struct S_a1_s2 {
diff --git a/gcc/testsuite/g++.dg/ext/flexary5.C b/gcc/testsuite/g++.dg/ext/flexary5.C
index d5ec13204966..3fb88c0e8a2c 100644
--- a/gcc/testsuite/g++.dg/ext/flexary5.C
+++ b/gcc/testsuite/g++.dg/ext/flexary5.C
@@ -13,7 +13,7 @@ struct S_no_diag: T {
 
 template <class T>
 struct STx_1: T {
-  char a[];   // { dg-error "flexible array member" }
+  char a[];
 };
 
 template <class T, int I>
@@ -37,7 +37,7 @@ struct E1: E<0>, E<1> { };
 struct E2: E<2>, E<3> { };
 struct D1: E1, E2
 {
-    char a[];   // { dg-error "flexible array member" }
+    char a[];
 };
 
 struct NE { size_t i; };
diff --git a/gcc/testsuite/g++.dg/ext/flexary8.C b/gcc/testsuite/g++.dg/ext/flexary8.C
index 7a1811deaff2..a0477f0fa410 100644
--- a/gcc/testsuite/g++.dg/ext/flexary8.C
+++ b/gcc/testsuite/g++.dg/ext/flexary8.C
@@ -4,26 +4,26 @@
 
 union U_i_ax {
     int i;
-    int a[];                  // { dg-error "flexible array member in union" }
+    int a[];
 };
 
 struct SU1 {
   union {
-    int a[];                  // { dg-error "flexible array member in union" }
+    int a[];
   };
 };
 
 struct SU2 {
   int n;
   union {
-    int a[];                  // { dg-error "flexible array member in union" }
+    int a[];
   };
 };
 
 struct SU3 {
   union {
     int n;
-    int a[];                  // { dg-error "flexible array member in union" }
+    int a[];
   };
 };
 
diff --git a/gcc/testsuite/g++.dg/torture/pr64280.C b/gcc/testsuite/g++.dg/torture/pr64280.C
index 5c569e864b4c..1ea70c4e766e 100644
--- a/gcc/testsuite/g++.dg/torture/pr64280.C
+++ b/gcc/testsuite/g++.dg/torture/pr64280.C
@@ -15,7 +15,7 @@ public:
 typedef int jmp_buf[];
 struct C
 {
-  jmp_buf cond_;   // { dg-error "flexible array member" }
+  jmp_buf cond_;
 };
 class F
 {
diff --git a/gcc/testsuite/gcc.dg/20050620-1.c b/gcc/testsuite/gcc.dg/20050620-1.c
index befdd9636500..0a9e1d478454 100644
--- a/gcc/testsuite/gcc.dg/20050620-1.c
+++ b/gcc/testsuite/gcc.dg/20050620-1.c
@@ -5,7 +5,7 @@
 void
 foo (void)
 {
-  struct { int i[]; } u;	/* { dg-error "flexible array member" } */
+  struct { int i[]; } u;
 }
 
 void
diff --git a/gcc/testsuite/gcc.dg/940510-1.c b/gcc/testsuite/gcc.dg/940510-1.c
index 46183831d096..9bcd7881f715 100644
--- a/gcc/testsuite/gcc.dg/940510-1.c
+++ b/gcc/testsuite/gcc.dg/940510-1.c
@@ -1,5 +1,5 @@
 /* { dg-do compile } */
 /* { dg-options "-std=c89 -pedantic" } */
 struct { int a[]; } x = { 0 };	/* { dg-warning "ISO C90 does not support flexible array members" } */
-/* { dg-error "flexible array member in a struct with no named members"  "" { target *-*-* }  .-1 } */
-
+/* { dg-warning "flexible array member in a struct with no named members is a GCC extension"  "" { target *-*-* }  .-1 } */
+/* { dg-warning "initialization of a flexible array member"  "" { target *-*-* }  .-2 } */
-- 
2.31.1


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

* Re: [RFC][PATCH v1 1/4] Documentation change
  2024-04-19 18:43 ` [RFC][PATCH v1 1/4] Documentation change Qing Zhao
@ 2024-04-19 20:54   ` Tom Tromey
  2024-04-22 13:28     ` Qing Zhao
  2024-04-23 18:04   ` Joseph Myers
  1 sibling, 1 reply; 16+ messages in thread
From: Tom Tromey @ 2024-04-19 20:54 UTC (permalink / raw)
  To: Qing Zhao
  Cc: josmyers, richard.guenther, uecker, siddhesh, keescook, gcc-patches

>>>>> Qing Zhao <qing.zhao@oracle.com> writes:

> +The size of the union is as if the flexiable array member were omitted
> +except that it may have more trailing padding than the omission would imply.
> +
> +If all the members of a union are flexiable array member, the size of 

There's a couple of spots that say "flexiable" which should say "flexible".

thanks,
Tom

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

* Re: [RFC][PATCH v1 0/4] Allow flexible array members in unions and alone in structures [PR53548]
  2024-04-19 18:43 [RFC][PATCH v1 0/4] Allow flexible array members in unions and alone in structures [PR53548] Qing Zhao
                   ` (3 preceding siblings ...)
  2024-04-19 18:43 ` [RFC][PATCH v1 4/4] Adjust testcases for flexible array member in union and alone in structure extension Qing Zhao
@ 2024-04-19 21:55 ` Kees Cook
  4 siblings, 0 replies; 16+ messages in thread
From: Kees Cook @ 2024-04-19 21:55 UTC (permalink / raw)
  To: Qing Zhao; +Cc: josmyers, richard.guenther, uecker, siddhesh, gcc-patches

On Fri, Apr 19, 2024 at 06:43:13PM +0000, Qing Zhao wrote:
> Therefore, GCC needs to explicitly allow such extensions directly for C99
> flexible arrays, since flexable array member in unions or alone in structs
> are common code patterns in active use by the Linux kernel (and other projects).

Thank you for fixing this! :) This will make conversions much much
easier for the Linux kernel (and future userspace programs).

I've tested these patches and everything behaves like I'd expect.

-Kees

-- 
Kees Cook

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

* Re: [RFC][PATCH v1 1/4] Documentation change
  2024-04-19 20:54   ` Tom Tromey
@ 2024-04-22 13:28     ` Qing Zhao
  0 siblings, 0 replies; 16+ messages in thread
From: Qing Zhao @ 2024-04-22 13:28 UTC (permalink / raw)
  To: Tom Tromey
  Cc: Joseph Myers, Richard Biener, uecker, Siddhesh Poyarekar,
	Kees Cook, gcc-patches



> On Apr 19, 2024, at 16:54, Tom Tromey <tom@tromey.com> wrote:
> 
>>>>>> Qing Zhao <qing.zhao@oracle.com> writes:
> 
>> +The size of the union is as if the flexiable array member were omitted
>> +except that it may have more trailing padding than the omission would imply.
>> +
>> +If all the members of a union are flexiable array member, the size of
> 
> There's a couple of spots that say "flexiable" which should say "flexible".

Thanks for catching those typo, I will fix them.

Qing
> 
> thanks,
> Tom


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

* Re: [RFC][PATCH v1 1/4] Documentation change
  2024-04-19 18:43 ` [RFC][PATCH v1 1/4] Documentation change Qing Zhao
  2024-04-19 20:54   ` Tom Tromey
@ 2024-04-23 18:04   ` Joseph Myers
  2024-04-23 18:21     ` Qing Zhao
  1 sibling, 1 reply; 16+ messages in thread
From: Joseph Myers @ 2024-04-23 18:04 UTC (permalink / raw)
  To: Qing Zhao; +Cc: richard.guenther, uecker, siddhesh, keescook, gcc-patches

On Fri, 19 Apr 2024, Qing Zhao wrote:

> +The size of the union is as if the flexiable array member were omitted
> +except that it may have more trailing padding than the omission would imply.

"trailing padding" is more a concept for structures than for unions (where 
padding depends on which union member is active).  But I suppose it's 
still true that the union can be larger than without the flexible member, 
because of alignment considerations.

union u { char c; int a[]; };

needs to be sufficiently aligned for int, which means the size is a 
multiple of the size of int, whereas if the flexible array member weren't 
present, the size could be 1 byte.

-- 
Joseph S. Myers
josmyers@redhat.com


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

* Re: [RFC][PATCH v1 1/4] Documentation change
  2024-04-23 18:04   ` Joseph Myers
@ 2024-04-23 18:21     ` Qing Zhao
  2024-04-23 19:03       ` Joseph Myers
  0 siblings, 1 reply; 16+ messages in thread
From: Qing Zhao @ 2024-04-23 18:21 UTC (permalink / raw)
  To: Joseph Myers
  Cc: Richard Biener, uecker, Siddhesh Poyarekar, Kees Cook, gcc-patches



> On Apr 23, 2024, at 14:04, Joseph Myers <josmyers@redhat.com> wrote:
> 
> On Fri, 19 Apr 2024, Qing Zhao wrote:
> 
>> +The size of the union is as if the flexiable array member were omitted
>> +except that it may have more trailing padding than the omission would imply.
> 
> "trailing padding" is more a concept for structures than for unions (where 
> padding depends on which union member is active).  But I suppose it's 
> still true that the union can be larger than without the flexible member, 
> because of alignment considerations.
> 
> union u { char c; int a[]; };
> 
> needs to be sufficiently aligned for int, which means the size is a 
> multiple of the size of int, whereas if the flexible array member weren't 
> present, the size could be 1 byte.

Yes, that’s exact what I tried to include in the documentation part -:)
And I have a testing case for this in the patch. 

However, I am not very confident on the wording of the doc, is the current wording good enough for this?
Or do you have any suggestion on how to make it better?

Thanks a lot!

Qing
> 
> -- 
> Joseph S. Myers
> josmyers@redhat.com
> 


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

* Re: [RFC][PATCH v1 3/4] Add testing cases for flexible array members in unions and alone in structures.
  2024-04-19 18:43 ` [RFC][PATCH v1 3/4] Add testing cases for " Qing Zhao
@ 2024-04-23 18:53   ` Joseph Myers
  2024-04-23 19:30     ` Qing Zhao
  0 siblings, 1 reply; 16+ messages in thread
From: Joseph Myers @ 2024-04-23 18:53 UTC (permalink / raw)
  To: Qing Zhao; +Cc: richard.guenther, uecker, siddhesh, keescook, gcc-patches

On Fri, 19 Apr 2024, Qing Zhao wrote:

> gcc/testsuite/ChangeLog:
> 
> 	* gcc.dg/flex-array-in-union-1.c: New test.
> 	* gcc.dg/flex-array-in-union-2.c: New test.

There should also be a -pedantic-errors test that these constructs get 
errors with -pedantic-errors.

The tests mix two cases: flexible arrays in unions, and flexible arrays on 
their own in structures.  That means the test names are misleading; either 
they should be renamed, or the struct tests should be split out.

Note that "no named members" also includes the case where there are 
unnamed bit-fields together with a flexible array member, so that should 
be tested as well.

Since this patch series involves changes for both C and C++, it would be 
best for the tests to be c-c++-common tests.  But if that's problematic 
for some reason - if there's still too much difference in behavior between 
C and C++ - then there should at least be tests for C++ that are as 
similar as possible to the tests for C.

-- 
Joseph S. Myers
josmyers@redhat.com


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

* Re: [RFC][PATCH v1 1/4] Documentation change
  2024-04-23 18:21     ` Qing Zhao
@ 2024-04-23 19:03       ` Joseph Myers
  2024-04-23 19:21         ` Qing Zhao
  0 siblings, 1 reply; 16+ messages in thread
From: Joseph Myers @ 2024-04-23 19:03 UTC (permalink / raw)
  To: Qing Zhao
  Cc: Richard Biener, uecker, Siddhesh Poyarekar, Kees Cook, gcc-patches

On Tue, 23 Apr 2024, Qing Zhao wrote:

> However, I am not very confident on the wording of the doc, is the 
> current wording good enough for this? Or do you have any suggestion on 
> how to make it better?

I'm not convinced the statement about size (in relation to a structure 
with the member omitted) is useful for unions the way it is for 
structures.  The structure with the member omitted is a relevant concept 
for thinking about a structure with a flexible array member (the flexible 
array member essentially goes after that structure); it's much less 
relevant for thinking about a union with a flexible array member.

(The statement that the size is zero when all members are flexible array 
members still seems a useful one to make.)

-- 
Joseph S. Myers
josmyers@redhat.com


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

* Re: [RFC][PATCH v1 1/4] Documentation change
  2024-04-23 19:03       ` Joseph Myers
@ 2024-04-23 19:21         ` Qing Zhao
  0 siblings, 0 replies; 16+ messages in thread
From: Qing Zhao @ 2024-04-23 19:21 UTC (permalink / raw)
  To: Joseph Myers
  Cc: Richard Biener, uecker, Siddhesh Poyarekar, Kees Cook, gcc-patches



> On Apr 23, 2024, at 15:03, Joseph Myers <josmyers@redhat.com> wrote:
> 
> On Tue, 23 Apr 2024, Qing Zhao wrote:
> 
>> However, I am not very confident on the wording of the doc, is the 
>> current wording good enough for this? Or do you have any suggestion on 
>> how to make it better?
> 
> I'm not convinced the statement about size (in relation to a structure 
> with the member omitted) is useful for unions the way it is for 
> structures.  The structure with the member omitted is a relevant concept 
> for thinking about a structure with a flexible array member (the flexible 
> array member essentially goes after that structure); it's much less 
> relevant for thinking about a union with a flexible array member.

Okay, then I will delete that statement about size.
> 
> (The statement that the size is zero when all members are flexible array 
> members still seems a useful one to make.)
And only keep the size is zero when all members are flexible array members.

Thanks.

Qing
> 
> -- 
> Joseph S. Myers
> josmyers@redhat.com
> 


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

* Re: [RFC][PATCH v1 3/4] Add testing cases for flexible array members in unions and alone in structures.
  2024-04-23 18:53   ` Joseph Myers
@ 2024-04-23 19:30     ` Qing Zhao
  0 siblings, 0 replies; 16+ messages in thread
From: Qing Zhao @ 2024-04-23 19:30 UTC (permalink / raw)
  To: Joseph Myers
  Cc: Richard Biener, uecker, Siddhesh Poyarekar, Kees Cook, gcc-patches



> On Apr 23, 2024, at 14:53, Joseph Myers <josmyers@redhat.com> wrote:
> 
> On Fri, 19 Apr 2024, Qing Zhao wrote:
> 
>> gcc/testsuite/ChangeLog:
>> 
>> 	* gcc.dg/flex-array-in-union-1.c: New test.
>> 	* gcc.dg/flex-array-in-union-2.c: New test.
> 
> There should also be a -pedantic-errors test that these constructs get 
> errors with -pedantic-errors.

Okay, will add. 
> 
> The tests mix two cases: flexible arrays in unions, and flexible arrays on 
> their own in structures.  That means the test names are misleading; either 
> they should be renamed, or the struct tests should be split out.
Okay, will update this.
> 
> Note that "no named members" also includes the case where there are 
> unnamed bit-fields together with a flexible array member, so that should 
> be tested as well.
Will add such testing cases.
> 
> Since this patch series involves changes for both C and C++, it would be 
> best for the tests to be c-c++-common tests.  But if that's problematic 
> for some reason - if there's still too much difference in behavior between 
> C and C++ - then there should at least be tests for C++ that are as 
> similar as possible to the tests for C.

I tried to put these two testing cases to c-c++-common, there were some inconsistent behavior 
I cannot resolve at that time, I will try to fix those issue or add C++ testing cases. 

Thanks for the review.

Qing
> 
> -- 
> Joseph S. Myers
> josmyers@redhat.com
> 


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

* Re: [RFC][PATCH v1 2/4] C and C++ FE changes to support flexible array members in unions and alone in structures.
  2024-04-19 18:43 ` [RFC][PATCH v1 2/4] C and C++ FE changes to support flexible array members in unions and alone in structures Qing Zhao
@ 2024-04-23 19:51   ` Joseph Myers
  2024-04-23 19:58     ` Qing Zhao
  0 siblings, 1 reply; 16+ messages in thread
From: Joseph Myers @ 2024-04-23 19:51 UTC (permalink / raw)
  To: Qing Zhao; +Cc: richard.guenther, uecker, siddhesh, keescook, gcc-patches

On Fri, 19 Apr 2024, Qing Zhao wrote:

> gcc/c/ChangeLog:
> 
> 	* c-decl.cc (finish_struct): Change errors to pedwarns for the cases
> 	flexible array members in union or alone in structures.

The C front-end changes are OK for GCC 15 once everything else in the 
series is ready for inclusion (in particular, the testsuite changes).

-- 
Joseph S. Myers
josmyers@redhat.com


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

* Re: [RFC][PATCH v1 2/4] C and C++ FE changes to support flexible array members in unions and alone in structures.
  2024-04-23 19:51   ` Joseph Myers
@ 2024-04-23 19:58     ` Qing Zhao
  0 siblings, 0 replies; 16+ messages in thread
From: Qing Zhao @ 2024-04-23 19:58 UTC (permalink / raw)
  To: Joseph Myers
  Cc: Richard Biener, uecker, Siddhesh Poyarekar, Kees Cook, gcc-patches



> On Apr 23, 2024, at 15:51, Joseph Myers <josmyers@redhat.com> wrote:
> 
> On Fri, 19 Apr 2024, Qing Zhao wrote:
> 
>> gcc/c/ChangeLog:
>> 
>> 	* c-decl.cc (finish_struct): Change errors to pedwarns for the cases
>> 	flexible array members in union or alone in structures.
> 
> The C front-end changes are OK for GCC 15 once everything else in the 
> series is ready for inclusion (in particular, the testsuite changes).

Thanks, will update the C FE changes based on your comments.

Qing
> 
> -- 
> Joseph S. Myers
> josmyers@redhat.com
> 


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

end of thread, other threads:[~2024-04-23 19:58 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-19 18:43 [RFC][PATCH v1 0/4] Allow flexible array members in unions and alone in structures [PR53548] Qing Zhao
2024-04-19 18:43 ` [RFC][PATCH v1 1/4] Documentation change Qing Zhao
2024-04-19 20:54   ` Tom Tromey
2024-04-22 13:28     ` Qing Zhao
2024-04-23 18:04   ` Joseph Myers
2024-04-23 18:21     ` Qing Zhao
2024-04-23 19:03       ` Joseph Myers
2024-04-23 19:21         ` Qing Zhao
2024-04-19 18:43 ` [RFC][PATCH v1 2/4] C and C++ FE changes to support flexible array members in unions and alone in structures Qing Zhao
2024-04-23 19:51   ` Joseph Myers
2024-04-23 19:58     ` Qing Zhao
2024-04-19 18:43 ` [RFC][PATCH v1 3/4] Add testing cases for " Qing Zhao
2024-04-23 18:53   ` Joseph Myers
2024-04-23 19:30     ` Qing Zhao
2024-04-19 18:43 ` [RFC][PATCH v1 4/4] Adjust testcases for flexible array member in union and alone in structure extension Qing Zhao
2024-04-19 21:55 ` [RFC][PATCH v1 0/4] Allow flexible array members in unions and alone in structures [PR53548] Kees Cook

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).