Skip to content

Fix uninitialized memory references#300

Open
matteoldani wants to merge 1 commit into
servo:mainfrom
matteoldani:fix-uninit-memory
Open

Fix uninitialized memory references#300
matteoldani wants to merge 1 commit into
servo:mainfrom
matteoldani:fix-uninit-memory

Conversation

@matteoldani

Copy link
Copy Markdown

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 a MaybeUninit<[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()) and std::ptr::copy_nonoverlapping to write to the buffer.
2. Slicing only the initialized portion (slice::from_raw_parts_mut) before creating the &mut str reference, ensuring we never create a reference to uninitialized bytes.

- 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.
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.

Unsoundness in dynamic_set.rs and from_mutated_str Status of provenance/aliasing issues detected by Miri

1 participant