]> git.ipfire.org Git - thirdparty/gcc.git/commit
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)
commit472d23c941e9abb3465ca808dc44eaf8c88703eb
treec0b3a6b5a7d8046a581133bb0b49d67b1f122e86
parenta7ebcb2ed60833fa3f5d390f5a3a412ca113a1b1
c++: Only destruct elts of array for new expression if exception is thrown during the initialization [PR117827]

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]