Fix uninitialized memory references#300
Open
matteoldani wants to merge 1 commit into
Open
Conversation
- Fix uninitialized memory reference UB in from_mutated_str. Creating a &mut [u8; 64] reference pointing to uninitialized bytes from MaybeUninit violates Rust's validity invariants. Replaced the slice cast with safe raw pointer arithmetic (ptr::copy_nonoverlapping) and slice creation over only the initialized portion of memory. - Refactor magic number 64 into a named const MAX_STACK_SIZE. - Add UB test case.
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.
Fixes #260
Fixes #279
This PR resolves a critical memory safety issue in
Atom::from_mutated_str.Previously, the code did
&mut *buffer.as_mut_ptr()on aMaybeUninit<[u8; 64]>. Creating a mutable reference to uninitialized memory is instant Undefined Behavior in Rust, and it violated Stacked Borrows (caught by Miri).This is fixed by:
1. Using raw pointers (
buffer.as_mut_ptr()) andstd::ptr::copy_nonoverlappingto write to the buffer.2. Slicing only the initialized portion (
slice::from_raw_parts_mut) before creating the&mut strreference, ensuring we never create a reference to uninitialized bytes.