From afe1f38ebaf4d9cbf57a7806e676ea722ecbc246 Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Wed, 18 Nov 2026 18:08:32 +0000 Subject: [PATCH] Fix sized delete[] size for zero-length array new [GH #550] array_new_general increased a zero-byte request to one byte even when there is an array cookie, but the placement, nothrow, and aligned paths and the IA-64 delete[] and exception cleanup compute n * size + prefix, so a sized operator delete[] received a different size for n == 0. Increase the size only when there is no prefix (as __cxa_vec_new2 and g++ do); the non-IA-64 get_array_size no longer adjusts it either. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- ...550-zero-length-array-new-sized-delete.txt | 12 ++++ lib_src/vec_newdel.c | 18 +++-- .../default.1.1.txt | 1 + .../default.1.2.txt | 1 + ...array-new-zero-length-sized-delete.sft.cpp | 71 +++++++++++++++++++ 5 files changed, 96 insertions(+), 7 deletions(-) create mode 100644 lib_src/pending_changes/gh-550-zero-length-array-new-sized-delete.txt create mode 100644 tests/tests/regressions/.array-new-zero-length-sized-delete.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.array-new-zero-length-sized-delete.rto/default.1.2.txt create mode 100644 tests/tests/regressions/array-new-zero-length-sized-delete.sft.cpp diff --git a/lib_src/pending_changes/gh-550-zero-length-array-new-sized-delete.txt b/lib_src/pending_changes/gh-550-zero-length-array-new-sized-delete.txt new file mode 100644 index 0000000000..a167f7dc73 --- /dev/null +++ b/lib_src/pending_changes/gh-550-zero-length-array-new-sized-delete.txt @@ -0,0 +1,12 @@ +[GH #550] +Sized delete[] size for a zero-length array new + +For an array new of zero elements of a class with a destructor (so that there +is an array cookie), operator new[] was asked for one byte more than the size +later passed to a sized operator delete[], e.g. + + struct E { ~E(); }; + delete[] new E[n]; // n == 0 at run time: new[] 9 bytes, delete[] 8. + +A zero-byte request is now only increased to one byte when there is no array +cookie, as with __cxa_vec_new2 and g++. diff --git a/lib_src/vec_newdel.c b/lib_src/vec_newdel.c index 5f4a496ce1..00d2ebfbfc 100644 --- a/lib_src/vec_newdel.c +++ b/lib_src/vec_newdel.c @@ -357,9 +357,8 @@ The number of elements in the array is returned in *number_of_elements. size = app->size; *number_of_elements = ~(app->encoded_number_of_elements); size_to_check = element_size * *number_of_elements; - /* The array size is adjusted to always allocate at least a byte of - storage for the array. */ - if (size_to_check == 0) size_to_check = 1; + /* The array size is not adjusted when a prefix is used (see + array_new_general). */ if (size != size_to_check) _array_pointer_not_from_vec_new(); #else /* defined(__EDG_IA64_ABI) */ app = ((an_alloc_prefix_ptr)array_ptr) - 1; @@ -490,10 +489,15 @@ use. #endif /* defined(__EDG_IA64_ABI) */ #endif /* EXCEPTION_HANDLING && ABI_COMPATIBILITY_VERSION >= 406 */ array_size = number_of_elements * element_size; - /* Always allocate at least a byte of storage for the array to guarantee - that the pointer returned to the caller (which points after the prefix) - cannot be the same as a subsequently allocated object. */ - if (array_size == 0) array_size = 1; + /* Without a prefix, always allocate at least a byte of storage for the + array to guarantee that the pointer returned to the caller cannot be + the same as a subsequently allocated object. With a prefix, the size + is not adjusted: the deallocation size is recomputed from the element + count (as in the IA-64 ABI), and arrays allocated by the caller (e.g., + by placement new) are not adjusted either, so a two-operand delete + routine would otherwise be passed a size different from the one + allocated. */ + if (array_size == 0 && prefix_size == 0) array_size = 1; if (array_ptr == NULL) { /* Allocate the array if a pointer has not been supplied by the caller. */ diff --git a/tests/tests/regressions/.array-new-zero-length-sized-delete.rto/default.1.1.txt b/tests/tests/regressions/.array-new-zero-length-sized-delete.rto/default.1.1.txt new file mode 100644 index 0000000000..d72d18ed14 --- /dev/null +++ b/tests/tests/regressions/.array-new-zero-length-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-zero-length-sized-delete.rto/default.1.2.txt b/tests/tests/regressions/.array-new-zero-length-sized-delete.rto/default.1.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.array-new-zero-length-sized-delete.rto/default.1.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/array-new-zero-length-sized-delete.sft.cpp b/tests/tests/regressions/array-new-zero-length-sized-delete.sft.cpp new file mode 100644 index 0000000000..e6daeca6ab --- /dev/null +++ b/tests/tests/regressions/array-new-zero-length-sized-delete.sft.cpp @@ -0,0 +1,71 @@ +//type:rp +//options_all:--c++14 --exceptions + +typedef decltype(sizeof(0)) Size; +namespace std { + struct nothrow_t { explicit nothrow_t() = default; }; +} + +alignas(long double) unsigned char storage[64]; +Size allocated_size; +Size deallocated_size; +int deallocations; + +void *operator new[](Size size) { + allocated_size = size; + return storage; +} +void *operator new[](Size size, const std::nothrow_t &) noexcept { + allocated_size = size; + return storage; +} +void operator delete[](void *) noexcept {} +void operator delete[](void *, const std::nothrow_t &) noexcept {} +void operator delete[](void *p, Size size) noexcept { + if (p == storage) { + deallocated_size = size; + ++deallocations; + } +} + +struct Element { + int value; + Element() : value(1) {} + ~Element() {} +}; + +struct Placed { + int value; + Placed() : value(1) {} + ~Placed() {} + static void *operator new[](Size size, int) { + allocated_size = size; + return storage; + } + static void operator delete[](void *, int) {} + static void operator delete[](void *p, Size size) { + ::operator delete[](p, size); + } +}; + +int check() { + int result = 0; + if (deallocations != 1) result = 1; + else if (deallocated_size != allocated_size) result = 2; + deallocations = 0; + return result; +} + +int main(int argc, char **) { + int count = argc - 1; + int result; + delete[] new Element[count][3]; + if ((result = check()) != 0) return 10 + result; + delete[] new Element[count]; + if ((result = check()) != 0) return 20 + result; + delete[] new (std::nothrow_t{}) Element[count]; + if ((result = check()) != 0) return 30 + result; + delete[] new (1) Placed[count]; + if ((result = check()) != 0) return 40 + result; + return 0; +}