Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix UB dropping BoxBytes with a zero-sized layout #258

Merged
merged 1 commit into from
Jul 30, 2024

Conversation

zachs18
Copy link
Contributor

@zachs18 zachs18 commented Jul 29, 2024

Box does not actually perform an allocation for zero-sized values, so BoxBytes::drop should not call dealloc if self.layout.size() == 0.

@Lokathor Lokathor merged commit 1c75146 into Lokathor:main Jul 30, 2024
14 checks passed
@zachs18 zachs18 mentioned this pull request Jul 31, 2024
// `layout.size()` initialized bytes.
// SAFETY: `ptr` is owned, points to `layout.size()` initialized bytes, and
// was allocated with `layout` (unless `layout.size() == 0` in which case it
// is dangling).
Copy link

Choose a reason for hiding this comment

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

Note: this terminology is incorrect according to Rust's definition. A pointer to zero-sized data is never dangling, since all zero-sized reads and writes are now considered valid, even with a null pointer.

The primary issue is that dealloc requires that the layout not be zero-sized; whether the pointer dangles is irrelevant.

Copy link
Owner

Choose a reason for hiding this comment

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

That is definitely true in a strict sense, and a follow up PR for fixing that note would be appropriate if you wanted to do that, because safety notes should not be loose with wording.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver-patch semver patch change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants