Skip to content

Fix the size passed to sized delete when an array new constructor throws [GH #551] - #564

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

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

Conversation

@lexx42xxel

@lexx42xxel lexx42xxel commented Nov 20, 2026 •

Copy link
Copy Markdown

Fixes the problem reported in triage discussion #551.

Problem and fix

The size argument of the deallocation call that frees the storage when an
element constructor throws was a reusable copy of the (discarded) size
argument of the allocation, which could refer to an uninitialized
temporary. The size argument is invariant, so copy it directly (this is
asserted).

Alternatives considered

Re-evaluate the size expression at cleanup time. Rejected: it may involve temporaries and side effects; the size is invariant once allocated, so copying it (asserted) is simpler and safe.

Tests

New: tests/tests/regressions/array-new-throw-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 garbage size when an array new element constructor throws #551, per CONTRIBUTING.
    Commits are tagged [GH #551] with the discussion number; happy to retag once an issue is opened.
  • Commits are tagged [GH #551].
  • Design and alternatives are described above.
  • No measurable compile-time/memory impact expected (localized lowering change); see the bench job.
  • Changes are localized (src/lower_init.c).
  • EDG style; comments updated.
  • Changes entry: src/pending_changes/gh-551-array-new-throw-sized-delete.txt.
  • Tests fail before and pass after: array-new-throw-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 array deallocation when an element constructor throws during initialization. The deallocation now receives the correct allocated size, including for single-dimensional and multidimensional arrays initialized with braces or initializer lists. This prevents the size from being taken from an uninitialized temporary.

…hrows [GH edgcpp#551]

The size argument of the deallocation call that frees the storage when an
element constructor throws was a reusable copy of the (discarded) size
argument of the allocation, which could refer to an uninitialized
temporary.  The size argument is invariant, so copy it directly (this is
asserted).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lexx42xxel
lexx42xxel requested a review from a team November 20, 2026 07:47
@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The sized-delete lowering now preserves the allocation-size expression as invariant. A new regression test checks the deallocation size when element construction throws during four array-initialization forms.

Changes: Sized array deallocation

Layer / File(s) Summary
Preserve the allocation size
src/lower_init.c
The sized-delete path asserts that the allocation-size expression is invariant and copies it with vars_can_change=FALSE.
Test throwing array construction
tests/tests/regressions/array-new-throw-sized-delete.sft.cpp, tests/tests/regressions/.array-new-throw-sized-delete.rto/*, src/pending_changes/gh-551-array-new-throw-sized-delete.txt
The regression test checks that sized deallocation receives the allocation size when construction throws in single- and multidimensional arrays, using empty-brace and initializer-list initialization.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested labels: bug

Suggested reviewers: daveedvdv-nvidia, darkarc

Merge Risk: Minimal · up to d63e3

This fixes the size passed to sized array delete when an element constructor throws. It is a narrow change with a regression test covering four initialization forms, and no concrete merge-blocking risk is identified.

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 states the primary change: the size passed to sized delete is corrected when an array new constructor throws. The raw summary confirms the lower_init.c change copies the size argument with v…
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 states the primary change: the size passed to sized delete is corrected when an array new constructor throws. The raw summary confirms the lower_init.c change copies the size argument with vars_can_change=FALSE, and the regression test and pending-changes note cover GH #551. The title is specific, concise, and matches the 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 d63e372.

Files selected for processing: 5

  • src/lower_init.c
  • src/pending_changes/gh-551-array-new-throw-sized-delete.txt
  • tests/tests/regressions/.array-new-throw-sized-delete.rto/default.1.1.txt
  • tests/tests/regressions/.array-new-throw-sized-delete.rto/default.1.2.txt
  • tests/tests/regressions/array-new-throw-sized-delete.sft.cpp

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

@edgcpp-bot

Copy link
Copy Markdown
Collaborator

AcknowlEDG review results updated for d63e372eb117:

@edgcpp-bot

Copy link
Copy Markdown
Collaborator

AcknowlEDG review results updated for d63e372eb117:

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