This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[C++] Reject variably modified types in operator new
- From: Florian Weimer <fweimer at redhat dot com>
- To: GCC Patches <gcc-patches at gcc dot gnu dot org>
- Date: Tue, 29 May 2012 18:00:46 +0200
- Subject: [C++] Reject variably modified types in operator new
This patch flags operator new on variably modified types as an error.
If this is acceptable, this will simplify the implementation of the
C++11 requirement to throw std::bad_array_new_length instead of
allocating a memory region which is too short.
Okay for trunk? Or should I guard this with -fpermissive?
2012-05-29 Florian Weimer <fweimer@redhat.com>
* init.c (build_new): Reject variably modified types.
2012-05-29 Florian Weimer <fweimer@redhat.com>
* g++.dg/init/new33.C: New.
--
Florian Weimer / Red Hat Product Security Team
Index: gcc/cp/init.c
===================================================================
--- gcc/cp/init.c (revision 187951)
+++ gcc/cp/init.c (working copy)
@@ -2844,6 +2844,13 @@
if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
return error_mark_node;
+ if (variably_modified_type_p (type, NULL_TREE))
+ {
+ if (complain & tf_error)
+ error ("new cannot be applied to a variably modified type");
+ return error_mark_node;
+ }
+
rval = build_new_1 (placement, type, nelts, init, use_global_new, complain);
if (rval == error_mark_node)
return error_mark_node;
Index: gcc/testsuite/g++.dg/init/new33.C
===================================================================
--- gcc/testsuite/g++.dg/init/new33.C (revision 0)
+++ gcc/testsuite/g++.dg/init/new33.C (revision 0)
@@ -0,0 +1,10 @@
+// { dg-do compile }
+
+int
+main (int argc, char **argv)
+{
+ typedef char A[argc];
+ new A; // { dg-error "variably modified" }
+ new A[0]; // { dg-error "variably modified" }
+ new A[5]; // { dg-error "variably modified" }
+}