This is the mail archive of the gcc-patches@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]

Re: C++ PATCH to reject initializating flexible array members in constructors (PR c++/72775)


On Thu, Dec 08, 2016 at 02:56:56PM -0500, Nathan Sidwell wrote:
> On 12/08/2016 01:05 PM, Jason Merrill wrote:
> > If the problem is the member initializer, we can diagnose that
> > directly rather than wait until we're in a constructor.
> 
> What about:
> struct Foo {
> int a;
> char ary[];
> Foo () : ary ("bob"){}
> };
> 
> ?  That ICEs in the same way.

My patch would make GCC to reject this too, but the new 'else' needs to
be removed from the patch, i.e.

2016-12-08  Marek Polacek  <polacek@redhat.com>

	PR c++/72775
	* init.c (perform_member_init): Diagnose initialization of a flexible
	array member in a constructor.

	* g++.dg/ext/flexary20.C: New.

diff --git gcc/cp/init.c gcc/cp/init.c
index b4b6cdb..01009c9 100644
--- gcc/cp/init.c
+++ gcc/cp/init.c
@@ -800,6 +800,10 @@ perform_member_init (tree member, tree init)
 	   in that case.  */
 	init = build_x_compound_expr_from_list (init, ELK_MEM_INIT,
 						tf_warning_or_error);
+      if (TREE_CODE (type) == ARRAY_TYPE
+	       && TYPE_DOMAIN (type) == NULL_TREE)
+	error_at (DECL_SOURCE_LOCATION (member),
+		  "initialization of a flexible array member in a constructor");
 
       if (init)
 	finish_expr_stmt (cp_build_modify_expr (input_location, decl,
diff --git gcc/testsuite/g++.dg/ext/flexary20.C gcc/testsuite/g++.dg/ext/flexary20.C
index e69de29..ff97b06 100644
--- gcc/testsuite/g++.dg/ext/flexary20.C
+++ gcc/testsuite/g++.dg/ext/flexary20.C
@@ -0,0 +1,49 @@
+// PR c++/72775
+// { dg-do compile { target c++11 } }
+// { dg-options -Wno-pedantic }
+
+struct S {
+  int i;
+  char a[] = "foo"; // { dg-error "initialization of a flexible array member in a constructor" }
+  S () {}
+};
+
+struct T {
+  int i;
+  char a[] = "foo"; // { dg-error "initialization of a flexible array member in a constructor" }
+};
+
+struct U {
+  int i;
+  char a[] = "foo"; // { dg-error "initialization of a flexible array member in a constructor" }
+  U ();
+};
+
+U::U() {}
+
+int
+main ()
+{
+  struct T t;
+}
+
+struct V {
+  int i;
+  struct W {
+    int j;
+    char a[] = "foo"; // { dg-error "initialization of a flexible array member in a constructor" }
+  } w;
+  V () {}
+};
+
+template <class T>
+struct X {
+  int i;
+  T a[] = "foo"; // { dg-error "initialization of a flexible array member in a constructor" }
+};
+
+void
+fn ()
+{
+  struct X<char> x;
+}

	Marek


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