From d63e372eb1170a9c559054f9b156d534cdb3338b Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Wed, 18 Nov 2026 18:08:34 +0000 Subject: [PATCH] Fix the size passed to sized delete[] when an array new constructor throws [GH #551] The size argument of the deallocation call that frees the storage when an element constructor throws was a reusable copy of the (discarded) size argument of the allocation, which could refer to an uninitialized temporary. The size argument is invariant, so copy it directly (this is asserted). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/lower_init.c | 11 ++- .../gh-551-array-new-throw-sized-delete.txt | 7 ++ .../default.1.1.txt | 1 + .../default.1.2.txt | 1 + .../array-new-throw-sized-delete.sft.cpp | 78 +++++++++++++++++++ 5 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 src/pending_changes/gh-551-array-new-throw-sized-delete.txt create mode 100644 tests/tests/regressions/.array-new-throw-sized-delete.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.array-new-throw-sized-delete.rto/default.1.2.txt create mode 100644 tests/tests/regressions/array-new-throw-sized-delete.sft.cpp diff --git a/src/lower_init.c b/src/lower_init.c index 156d63c8cc..cc29d3fe47 100644 --- a/src/lower_init.c +++ b/src/lower_init.c @@ -11950,8 +11950,15 @@ arrays with class elements. entity_node = NULL; /* Allocate in __vec_new. */ #if ABI_CHANGES_FOR_PLACEMENT_DELETE if (sized_delete) { - /* Create a size argument for the deallocation routine. */ - delete_args = make_reusable_copy(args, /*vars_can_change=*/TRUE); + /* Create a size argument for the deallocation routine. args itself + is discarded (the number of elements is passed to the runtime + instead), so the copy must not depend on evaluating it. args is + computed only from constants and a lowered temporary that is set + before the allocation (see size_arg_for_new), so a plain copy is + safe. */ + check_assertion(is_invariant_expr(args, /*vars_can_change=*/FALSE, + /*treat_as_potential_prvalue=*/FALSE)); + delete_args = make_reusable_copy(args, /*vars_can_change=*/FALSE); prefix_size_node = get_prefix_size_node(elem_type, new_routine); if (prefix_size_node != NULL) { /* Add in the size of the prefix, if applicable. */ diff --git a/src/pending_changes/gh-551-array-new-throw-sized-delete.txt b/src/pending_changes/gh-551-array-new-throw-sized-delete.txt new file mode 100644 index 0000000000..d06f6953ac --- /dev/null +++ b/src/pending_changes/gh-551-array-new-throw-sized-delete.txt @@ -0,0 +1,7 @@ +[GH #551] +Sized delete[] size when an array new element constructor throws + +When an element constructor of an array new with an initializer (e.g., +"new E[n]{}") threw, the storage was freed by a sized operator delete[] whose +size argument was read from an uninitialized temporary. The size passed is +now the size that was allocated. diff --git a/tests/tests/regressions/.array-new-throw-sized-delete.rto/default.1.1.txt b/tests/tests/regressions/.array-new-throw-sized-delete.rto/default.1.1.txt new file mode 100644 index 0000000000..d72d18ed14 --- /dev/null +++ b/tests/tests/regressions/.array-new-throw-sized-delete.rto/default.1.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=1 --c++14 --exceptions Test_name.c diff --git a/tests/tests/regressions/.array-new-throw-sized-delete.rto/default.1.2.txt b/tests/tests/regressions/.array-new-throw-sized-delete.rto/default.1.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.array-new-throw-sized-delete.rto/default.1.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/array-new-throw-sized-delete.sft.cpp b/tests/tests/regressions/array-new-throw-sized-delete.sft.cpp new file mode 100644 index 0000000000..8876e71ef1 --- /dev/null +++ b/tests/tests/regressions/array-new-throw-sized-delete.sft.cpp @@ -0,0 +1,78 @@ +//type:rp +//options_all:--c++14 --exceptions + +typedef decltype(sizeof(0)) Size; + +alignas(long double) unsigned char storage[256]; +Size allocated_size; +Size deallocated_size; +int deallocations; + +void *operator new[](Size size) { + allocated_size = size; + return storage; +} +void operator delete[](void *) noexcept {} +void operator delete[](void *p, Size size) noexcept { + if (p == storage) { + deallocated_size = size; + ++deallocations; + } +} + +int constructions; +int throw_at; + +struct Element { + int value; + Element() : value(1) { + if (++constructions == throw_at) throw 1; + } + Element(int v) : value(v) { + if (++constructions == throw_at) throw 1; + } + ~Element() {} +}; + +int check(int (*make)(int), int count) { + int result = 0; + constructions = 0; + throw_at = 2; + deallocations = 0; + allocated_size = deallocated_size = 0; + try { + make(count); + result = 1; + } catch (int) { + } + if (result == 0 && deallocations != 1) result = 2; + if (result == 0 && deallocated_size != allocated_size) result = 3; + return result; +} + +int make_brace(int count) { + delete[] new Element[count]{}; + return 0; +} +int make_list(int count) { + delete[] new Element[count]{Element(7)}; + return 0; +} +int make_multi_brace(int count) { + delete[] new Element[count][3]{}; + return 0; +} +int make_multi_list(int count) { + delete[] new Element[count][3]{{Element(7)}}; + return 0; +} + +int main(int argc, char **) { + int count = argc + 2; + int result; + if ((result = check(make_brace, count)) != 0) return 10 + result; + if ((result = check(make_list, count)) != 0) return 20 + result; + if ((result = check(make_multi_brace, count)) != 0) return 30 + result; + if ((result = check(make_multi_list, count)) != 0) return 40 + result; + return 0; +}