Skip to content

Handle unwinding from Box::new - #837

Merged
Darksonn merged 2 commits into
masterfrom
box-new-unwind
Jul 7, 2026
Merged

Handle unwinding from Box::new#837
Darksonn merged 2 commits into
masterfrom
box-new-unwind

Conversation

@Darksonn

@Darksonn Darksonn commented Jul 5, 2026

Copy link
Copy Markdown
Member

This fixes a few cases where a panic from Box::new has incorrect behavior because it runs the destructor of the argument passed to new. They are fixed by ensuring that the allocation is performed before creating the value to be stored in the box.

Note that the default configuration is to abort on allocation failure, but it's possible to configure the allocator so that OOM results in panic instead of abort.

Fixes: #836

@Darksonn
Darksonn requested a review from seanmonstar July 7, 2026 11:01
Comment thread src/bytes.rs

impl From<Vec<u8>> for Bytes {
fn from(vec: Vec<u8>) -> Bytes {
let shared = Box::new(MaybeUninit::<Shared>::uninit());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to go against the line

// Avoid an extra allocation if possible

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I wanted to move it before the ManuallyDrop line. Fixed.

@Darksonn
Darksonn merged commit 924c82b into master Jul 7, 2026
18 checks passed
@Darksonn
Darksonn deleted the box-new-unwind branch July 7, 2026 13:33
@Darksonn Darksonn mentioned this pull request Jul 7, 2026
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.

Exception safety violations in BytesMut, leading to Double-Free

3 participants