Fix allocator capacity bounds checking and tracking in Buf32#754
Merged
Conversation
Contributor
Author
|
I've realized that the |
simonwuelker
reviewed
Jul 7, 2026
| self.cap = new_cap; | ||
|
|
||
| let actual_vec_cap = vec.capacity(); | ||
| let actual_cap_bytes = (actual_vec_cap - 1) * mem::size_of::<H>(); |
Member
There was a problem hiding this comment.
We have bytes_to_vec_capacity, can we add a vec_capacity_to_bytes to avoid duplicating this code?
Contributor
Author
There was a problem hiding this comment.
Done, thanks for the suggestions
| let actual_vec_cap = vec.capacity(); | ||
| let actual_cap_bytes = (actual_vec_cap - 1) * mem::size_of::<H>(); | ||
| if actual_cap_bytes > std::u32::MAX as usize { | ||
| panic!("tendril: capacity overflow"); |
Member
There was a problem hiding this comment.
nit: Use the OFLOW constant.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes a memory safety vulnerability in
Buf32that causes Undefined Behavior when interacting with global allocators that over-provision memory (such asjemalloc).When
Buf32initially allocates memory viaVec::with_capacity(), the global allocator often returns a buffer slightly larger than requested to align with memory chunk boundaries. Previously,Buf32mistakenly stored the requested capacity rather than the actual capacity returned by the allocator.When the buffer was later deallocated or resized using
Vec::from_raw_parts, it passed this smaller, incorrect capacity back to the global allocator. Providing a mismatched layout size back to the allocator violates Vec 's API invariants and isUndefined Behavior, which can result in immediate segmentation faults or silent heap corruption.
Changes:
•
Buf32::with_capacitynow queriesvec.capacity()to determine the true allocated layout size.• The capacity calculation translates this back into byte capacity by explicitly subtracting the first H header block.
• Adds a bounds check against
u32::MAXto explicitly panic and prevent 32-bit truncation if an allocation happens to unexpectedly exceedu32::MAXbytes.Resolves #751