Repository navigation
Description
Discussed in #551
Originally posted by lexx42xxel November 18, 2026
Issue Summary
When an element constructor of new E[n]{} throws, the storage is freed with a sized operator delete[] whose size argument is read from an uninitialized temporary.
Reproducing Source Code
extern "C" int printf(const char *, ...);
typedef decltype(sizeof(0)) Size;
alignas(16) unsigned char storage[256];
Size alloc_size, dealloc_size;
void *operator new[](Size s) { alloc_size = s; return storage; }
void operator delete[](void *) noexcept {}
void operator delete[](void *, Size s) noexcept { dealloc_size = s; }
int ctors;
struct E { int v; E() : v(1) { if (++ctors == 2) throw 1; } ~E() {} };
int main(int argc, char **) {
int n = argc + 2;
try { delete[] new E[n]{}; } catch (int) {}
printf("new[] size %lu, sized delete[] size %lu (expected equal)\n",
(unsigned long)alloc_size, (unsigned long)dealloc_size);
return alloc_size != dealloc_size;
}Command-line Options
eccp --c++17 --g++ --exceptions t.cpp -o t && ./t
Type of Issue
[Back End] There is a problem with the generated C code.
Additional Details
Version: EDG C/C++ front end 7.0, main at commit
6a6c7f2d6bd8027f1b0c90877b4ba222fbff6642 (2026-11-15).
Actual output:
new[] size 20, sized delete[] size 140378921400704 (expected equal)
Expected: Equal sizes.
The size argument of the cleanup deallocation call is a reusable copy of the (discarded) size
argument of the allocation, which can refer to a temporary that was never initialized on this path.
The size is invariant and can be copied directly.
The program exits nonzero when the bug is present. A fix with a regression
test is ready (branch fix/array-new-throw-sized-delete-size); a PR will reference this issue.
AddressSanitizer
AddressSanitizer detects the same bug in this variant, which has no counters or output of its own:
int ctors;
struct E { int v; E() : v(1) { if (++ctors == 2) throw 1; } ~E() {} };
int main(int argc, char **) {
int n = argc + 2;
try { delete[] new E[n]{}; } catch (int) {}
return 0;
}eccp --c++17 --g++ --exceptions -g --c_to_obj_option -fsanitize=address t.cpp -o t && ./t
Report on main (6a6c7f2), addresses elided:
ERROR: AddressSanitizer: new-delete-type-mismatch on 0x... in thread T0:
object passed to delete has wrong type:
size of the allocated type: 20 bytes;
size of the deallocated type: -4702111234474983738 bytes.
#0 0x... in operator delete[](void*, unsigned long) (/lib64/libasan.so.8+0xe8afb)
#1 0x... in main array-new-throw-sized-delete.cpp:5
SUMMARY: AddressSanitizer: new-delete-type-mismatch array-new-throw-sized-delete.cpp:5 in main
No report with the fix, nor with g++ (-fsanitize=address).
Reproducing (Standard) Configuration(s)
linux-gcc-debug
Reproducing (Non-standard) Configuration
No response
I acknowledge that:
- I have checked open issues and existing discussions before filing this report.
- The reproducing source code that I have provided does not depend on headers that I have not included with my submission.
- I have reduced the reproducing source code to the best of my ability.
- I have selected at least one standard configuration or uploaded a non-standard configuration dump.
- I understand code shared via this form is NOT confidential.
- I have permission to share the reproducing source code and grant permission for the project to use the source code (or derivatives of the source code) to create tests.
- I agree that any tests created using the reproducing source code (or deriatives of it) will be licensed under project license unless I included a
LICENSE.txtfile in my.zipor.tar.gzupload. If I included aLICENSE.txtfile, any tests created using the reproducing source code will be governed by the terms of the license I provided instead.
Activity