Skip to content

Sized operator delete[] gets a different size than operator new[] for a zero-length array new #600

Backlog

Description

@nv-cmeerw

Discussed in #550

Originally posted by lexx42xxel November 18, 2026

Issue Summary

Version: EDG C/C++ front end 7.0, main at commit  6a6c7f2d6bd8027f1b0c90877b4ba222fbff6642 (2026-11-15).

For new E[n] with n == 0 at run time and a class with a destructor (so there is an array cookie), operator new[] is asked for cookie + 1 bytes, but the sized operator delete[] is passed cookie bytes. [new.delete.array] requires the same size.

Reproducing Source Code

extern "C" int printf(const char *, ...);
typedef decltype(sizeof(0)) Size;
alignas(16) unsigned char storage[64];
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; }
struct E { E() {} ~E() {} };
int bad;
static void report(const char *what) {
  printf("%-12s new[] size %lu, sized delete[] size %lu (expected equal)\n",
         what, (unsigned long)alloc_size, (unsigned long)dealloc_size);
  bad |= alloc_size != dealloc_size;
}
int main(int argc, char **) {
  int n = argc - 1;                               /* 0 at run time */
  delete[] new E[n];                              report("new E[0]");
  delete[] new E[n][3];                           report("new E[0][3]");
  return bad;
}

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

Actual output:

new E[0]     new[] size 9, sized delete[] size 8
new E[0][3]  new[] size 9, sized delete[] size 8

Expected: Equal sizes (g++: 8 / 8).

array_new_general (lib_src) bumps a zero-byte request to one byte even when there is a prefix,
while the placement, nothrow and aligned paths and the IA-64 delete[]/exception cleanup compute
n * size + prefix. Bumping only when there is no prefix matches __cxa_vec_new2 and g++.

The program exits nonzero when the bug is present. A fix with a regression test is ready (branch
fix/zero-length-array-new-sized-delete); a PR will reference this issue.

AddressSanitizer

The same bug is detected by AddressSanitizer with this variant, which uses the real allocator /
heap-owning or written members instead of counters:

struct E { E() {} ~E() {} };
int main(int argc, char **) {
  int n = argc - 1;                               /* 0 at run time */
  delete[] new E[n];
  delete[] new E[n][3];
  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:   9 bytes;
  size of the deallocated type: 8 bytes.
    #0 0x... in operator delete[](void*, unsigned long) (/lib64/libasan.so.8+0xe8afb)
    #1 0x... in _INTERNAL_12_vec_newdel_c_8a08d4a5::free_array(void*, unsigned long, unsigned long,
void (*)(void*), int) lib_src/vec_newdel.c:251
    #2 0x... in _INTERNAL_12_vec_newdel_c_8a08d4a5::array_delete_general(void*, long, unsigned
long, unsigned long, void (*)(void*), int, void (*)(void*), int, int) lib_src/vec_newdel.c:1009
    #3 0x... in __cxa_vec_delete3 lib_src/vec_newdel.c:1116
    #4 0x... in main zero-length-array-new-sized-delete.cpp:4
SUMMARY: AddressSanitizer: new-delete-type-mismatch lib_src/vec_newdel.c:251 in
_INTERNAL_12_vec_newdel_c_8a08d4a5::free_array(void*, unsigned long, unsigned long, void
(*)(void*), int)

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.txt file in my .zip or .tar.gz upload. If I included a LICENSE.txt file, any tests created using the reproducing source code will be governed by the terms of the license I provided instead.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions