Skip to content

Fix sized delete size for zero-length array new [GH #550] - #563

Open
lexx42xxel wants to merge 1 commit into
edgcpp:mainfrom
lexx42xxel:fix/zero-length-array-new-sized-delete
Open

lexx42xxel wants to merge 1 commit into
edgcpp:mainfrom
lexx42xxel:fix/zero-length-array-new-sized-delete

Conversation

@lexx42xxel

@lexx42xxel lexx42xxel commented Nov 20, 2026 •

Copy link
Copy Markdown

Fixes the problem reported in triage discussion #550.

Problem and fix

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.

Alternatives considered

Also bump the size in the delete[] and cleanup paths. Rejected: the allocated size would then differ from what __cxa_vec_new2 and g++ request, and every path that computes n * size + prefix would need the same special case.

Tests

New: tests/tests/regressions/array-new-zero-length-sized-delete.sft.cpp.
Verified failing on main and passing with the fix (IA-64 and non-IA-64 builds); reproducer in the discussion.
AddressSanitizer: the discussion's reproducer (ASan variant) reports new-delete-type-mismatch on main; no report with the fix or with g++.

Checklist

  • Open issue: not yet; reported in triage discussion Sized operator delete[] gets a different size than operator new[] for a zero-length array new #550, per CONTRIBUTING.
    Commits are tagged [GH #550] with the discussion number; happy to retag once an issue is opened.
  • Commits are tagged [GH #550].
  • Design and alternatives are described above.
  • No measurable compile-time/memory impact expected (localized lowering change); see the bench job.
  • Changes are localized (lib_src/vec_newdel.c).
  • EDG style; comments updated.
  • Changes entry: lib_src/pending_changes/gh-550-zero-length-array-new-sized-delete.txt.
  • Tests fail before and pass after: array-new-zero-length-sized-delete.
  • edg-docker-test --posture=extensive (edg_x86_64, edg_x86_64_cp, edg_x86_32, edg_x86_32_cp): no regressions; recordings updated with -W.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed sized deallocation for zero-length arrays so the reported size matches the allocation size, including arrays with destructors and placement allocations.
  • Tests

    • Added regression coverage for zero-length array allocation and deallocation across several allocation methods.

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>
@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The array allocation path now preserves zero-byte sizes when an array prefix is present. A regression test checks that sized deallocation receives the recorded allocation size for several array allocation forms.

Changes: Array allocation and deallocation

Layer / File(s) Summary
Prefixed array size handling
lib_src/vec_newdel.c, lib_src/pending_changes/*
Prefixed arrays no longer receive a one-byte minimum allocation size when the computed size is zero. The pending-change note describes the size-matching behavior.
Sized-delete regression test
tests/tests/regressions/array-new-zero-length-sized-delete.sft.cpp, tests/tests/regressions/.array-new-zero-length-sized-delete.rto/*
The regression test records allocation and sized-deallocation sizes and checks multidimensional, one-dimensional, nothrow, and placement array allocations. The test fixture enables C++14 and exceptions and expects ./a.out.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Suggested labels: bug

Suggested reviewers: wchilders-nvidia, daveedvdv-nvidia

Merge Risk: Minimal · up to afe1f

Zero-length prefixed array allocations now pass sized delete the same size that was requested, which removes the mismatch reported in the issue. A regression test covers this. No actionable merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: the size passed to sized array delete for zero-length array new is fixed, and it references GH #550. The raw summary shows the core edit in lib_src/vec_newdel.c, which…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: the size passed to sized array delete for zero-length array new is fixed, and it references GH #550. The raw summary shows the core edit in lib_src/vec_newdel.c, which stops treating a zero element count as a one-byte allocation when a prefix is used, and the regression test and pending-change note match this topic. The title is specific and matches the PR objectives.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 123522f and afe1f38.

Files selected for processing: 5

  • lib_src/pending_changes/gh-550-zero-length-array-new-sized-delete.txt
  • lib_src/vec_newdel.c
  • tests/tests/regressions/.array-new-zero-length-sized-delete.rto/default.1.1.txt
  • tests/tests/regressions/.array-new-zero-length-sized-delete.rto/default.1.2.txt
  • tests/tests/regressions/array-new-zero-length-sized-delete.sft.cpp

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

@edgcpp-bot

Copy link
Copy Markdown
Collaborator

AcknowlEDG review results updated for afe1f38ebaf4:

@edgcpp-bot

Copy link
Copy Markdown
Collaborator

AcknowlEDG review results updated for afe1f38ebaf4:

CI success.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants