Skip to content

Commit

Permalink
safety: conform to stacked borrows
Browse files Browse the repository at this point in the history
When using 'get_unchecked' twice where one is a mutable borrow, it ends
up creating UB under the "stacked borrows" model. Which isn't adopted
yet. Still, it seems likely that it will? So we fix it by deriving both
pointers to 'ptr::copy' from the same 'get_unchecked_mut' call.

Closes #121
  • Loading branch information
saethlin authored and BurntSushi committed Sep 2, 2022
1 parent bc10172 commit 9a37af8
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/ext_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3063,11 +3063,8 @@ pub trait ByteSlice: Sealed {
// Finally, we are only dealing with u8 data, which is Copy, which
// means we can copy without worrying about ownership/destructors.
unsafe {
ptr::copy(
self.as_bytes().get_unchecked(src_start),
self.as_bytes_mut().get_unchecked_mut(dest),
count,
);
let ptr = self.as_bytes_mut().as_mut_ptr();
ptr::copy(ptr.add(src_start), ptr.add(dest), count);
}
}
}
Expand Down

0 comments on commit 9a37af8

Please sign in to comment.