]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Only destruct elts of array for new expression if exception is thrown during...
authorJakub Jelinek <jakub@redhat.com>
Sat, 25 Jan 2025 09:15:24 +0000 (10:15 +0100)
committerJakub Jelinek <jakub@gcc.gnu.org>
Fri, 13 Jun 2025 09:50:27 +0000 (11:50 +0200)
The following testcase r12-6328, because the elements of the array
are destructed twice, once when the callee encounters delete[] p;
and then second time when the exception is thrown.
The array elts should be only destructed if exception is thrown from
one of the constructors during the build_vec_init emitted code in case of
new expressions, but when the new expression completes, it is IMO
responsibility of user code to delete[] it when it is no longer needed.

So, the following patch uses the cleanup_flags argument to build_vec_init
to get notified of the flags that need to be changed when the expression
is complete and build_disable_temp_cleanup to do the changes.

2025-01-25  Jakub Jelinek  <jakub@redhat.com>

PR c++/117827
* init.cc (build_new_1): Pass address of a make_tree_vector ()
initialized gc tree vector to build_vec_init and append
build_disable_temp_cleanup to init_expr from it.

* g++.dg/init/array66.C: New test.

(cherry picked from commit ce268ca2a923f8f35cc9dd5a7d0468a3980f129f)

gcc/cp/init.cc
gcc/testsuite/g++.dg/init/array66.C [new file with mode: 0644]

index a0a28f2a00231a6cd026859bd766182215e1bb5c..1dac067d5cfffecf60637114f2d9f33afd916c84 100644 (file)
@@ -3664,6 +3664,11 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
                 error ("parenthesized initializer in array new");
              return error_mark_node;
             }
+
+         /* Collect flags for disabling subobject cleanups once the complete
+            object is fully constructed.  */
+         vec<tree, va_gc> *flags = make_tree_vector ();
+
          init_expr
            = build_vec_init (data_addr,
                              cp_build_binary_op (input_location,
@@ -3673,7 +3678,28 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
                              vecinit,
                              explicit_value_init_p,
                              /*from_array=*/0,
-                              complain);
+                             complain,
+                             &flags);
+
+         for (tree f : flags)
+           {
+             /* See maybe_push_temp_cleanup.  */
+             tree d = f;
+             tree i = boolean_false_node;
+             if (TREE_CODE (f) == TREE_LIST)
+               {
+                 /* To disable a build_vec_init cleanup, set
+                    iterator = maxindex.  */
+                 d = TREE_PURPOSE (f);
+                 i = TREE_VALUE (f);
+                 ggc_free (f);
+               }
+             tree cl = build2 (MODIFY_EXPR, TREE_TYPE (d), d, i);
+             cl = convert_to_void (cl, ICV_STATEMENT, complain);
+             init_expr = build2 (COMPOUND_EXPR, void_type_node,
+                                 init_expr, cl);
+           }
+         release_tree_vector (flags);
        }
       else
        {
diff --git a/gcc/testsuite/g++.dg/init/array66.C b/gcc/testsuite/g++.dg/init/array66.C
new file mode 100644 (file)
index 0000000..ca38df8
--- /dev/null
@@ -0,0 +1,33 @@
+// PR c++/117827
+// { dg-do run { target c++11 } }
+
+struct C {
+  int c;
+  static int d, e;
+  C () : c (0) { ++d; }
+  C (const C &) = delete;
+  C &operator= (const C &) = delete;
+  ~C () { ++e; }
+};
+int C::d, C::e;
+
+C *
+foo (C *p)
+{
+  delete[] p;
+  throw 1;
+}
+
+int
+main ()
+{
+  try
+    {
+      foo (new C[1] {});
+    }
+  catch (...)
+    {
+    }
+  if (C::d != C::e)
+    __builtin_abort ();
+}