Skip to content

Commit

Permalink
Fix undefined behavior in swap with the miri stacked borrows model.
Browse files Browse the repository at this point in the history
The `swap` function passed with `MIRIFLAGS=-zmiri-tree-borrows`
But failed when ran through the stacked borrows model, because
`get_unchecked_mut` returns `&mut` references which we need two of.

by using `as_mut_ptr()` we can avoid creating multiple mutable references
from a single one satisfying uniqueness.
  • Loading branch information
ratmice committed Mar 10, 2024
1 parent 1e48278 commit f0f3ed1
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/core/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,9 @@ impl<T> Core<T> for OptionCore<T> {
// We can't just have two mutable references, so we use `ptr::swap`
// instead of `mem::swap`. We do not use the slice's `swap` method as
// that performs bound checks.
let pa: *mut _ = self.data.get_unchecked_mut(a);
let pb: *mut _ = self.data.get_unchecked_mut(b);
let p = self.data.as_mut_ptr();
let pa: *mut _ = p.offset(a as isize);
let pb: *mut _ = p.offset(b as isize);
ptr::swap(pa, pb);
}
}
Expand Down

0 comments on commit f0f3ed1

Please sign in to comment.