[C++ PATCH] Diagnose invalid variable-sized compound literals (PR c++/36023)

Jakub Jelinek jakub@redhat.com
Mon Apr 28 21:00:00 GMT 2008


Hi!

reshape_init doesn't expect arrays to be VLAs.  check_initializer
already ensures that, but finish_compound_literal, the only other
caller, does not.  Fixed thusly, regtested on x86_64-linux,
ok for trunk/4.3?

2008-04-28  Jakub Jelinek  <jakub@redhat.com>

	PR c++/36023
	* semantics.c (finish_compound_literal): Make sure ARRAY_TYPEs
	have complete element types and that they aren't variable sized.

	* g++.dg/ext/complit10.C: New test.

--- gcc/cp/semantics.c.jj	2008-04-23 21:52:57.000000000 +0200
+++ gcc/cp/semantics.c	2008-04-28 19:00:54.000000000 +0200
@@ -2123,6 +2123,26 @@ finish_compound_literal (tree type, VEC(
     }
 
   type = complete_type (type);
+  if (TREE_CODE (type) == ARRAY_TYPE)
+    {
+      tree element_type = TREE_TYPE (type);
+
+      /* The array type itself need not be complete, because the
+	 initializer may tell us how many elements are in the array.
+	 But, the elements of the array must be complete.  */
+      if (!COMPLETE_TYPE_P (complete_type (element_type)))
+	{
+	  error ("elements of array %qT have incomplete type", type);
+	  return error_mark_node;
+	}
+      /* It is not valid to initialize a VLA.  */
+      if ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
+	   || !TREE_CONSTANT (TYPE_SIZE (element_type)))
+	{
+	  error ("variable-sized compound literal");
+	  return error_mark_node;
+	}
+    }
   compound_literal = reshape_init (type, compound_literal);
   if (TREE_CODE (type) == ARRAY_TYPE)
     cp_complete_array_type (&type, compound_literal, false);
--- gcc/testsuite/g++.dg/ext/complit10.C.jj	2008-04-28 19:07:31.000000000 +0200
+++ gcc/testsuite/g++.dg/ext/complit10.C	2008-04-28 19:06:56.000000000 +0200
@@ -0,0 +1,20 @@
+// PR c++/36023
+// { dg-do compile }
+// { dg-options "" }
+
+struct A;
+
+void
+f1 (int i)
+{
+  (int[i]) { 1 };	// { dg-error "variable-sized compound literal" }
+  (A[5]) { 1 };		// { dg-error "have incomplete type" }
+  (A[i]) { 1 };		// { dg-error "have incomplete type" }
+}
+
+void
+f2 ()
+{
+  (int[]) { 1 };
+  (int[1]) { 1 };
+}

	Jakub



More information about the Gcc-patches mailing list