Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/lower_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
7 changes: 7 additions & 0 deletions src/pending_changes/gh-551-array-new-throw-sized-delete.txt
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
eccp -DTEST_NUMBER=1 --c++14 --exceptions Test_name.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./a.out
78 changes: 78 additions & 0 deletions tests/tests/regressions/array-new-throw-sized-delete.sft.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
Loading