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
Original file line number Diff line number Diff line change
@@ -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++.
18 changes: 11 additions & 7 deletions lib_src/vec_newdel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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. */
Expand Down
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
71 changes: 71 additions & 0 deletions tests/tests/regressions/array-new-zero-length-sized-delete.sft.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
Loading