Skip to content

Commit

Permalink
Don't narrow provenance through references (#16)
Browse files Browse the repository at this point in the history
slice::get_unchecked returns a reference, and since references narrow
provenance, when the reference is coerced to a pointer it is UB to use
the pointer outside of its current pointee.
This is the same fix as rust-lang/rust #78602
  • Loading branch information
saethlin authored Jan 17, 2022
1 parent bbfee07 commit 4ca22cf
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/sip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ macro_rules! load_int_le {
debug_assert!($i + mem::size_of::<$int_ty>() <= $buf.len());
let mut data = 0 as $int_ty;
ptr::copy_nonoverlapping(
$buf.get_unchecked($i),
$buf.as_ptr().add($i),
&mut data as *mut _ as *mut u8,
mem::size_of::<$int_ty>(),
);
Expand Down
6 changes: 3 additions & 3 deletions src/sip128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ macro_rules! load_int_le {
debug_assert!($i + mem::size_of::<$int_ty>() <= $buf.len());
let mut data = 0 as $int_ty;
ptr::copy_nonoverlapping(
$buf.get_unchecked($i),
$buf.as_ptr().add($i),
&mut data as *mut _ as *mut u8,
mem::size_of::<$int_ty>(),
);
Expand Down Expand Up @@ -647,8 +647,8 @@ impl Hash128 {
let h1 = self.h1.to_le();
let h2 = self.h2.to_le();
unsafe {
ptr::copy_nonoverlapping(&h1 as *const _ as *const u8, bytes.get_unchecked_mut(0), 8);
ptr::copy_nonoverlapping(&h2 as *const _ as *const u8, bytes.get_unchecked_mut(8), 8);
ptr::copy_nonoverlapping(&h1 as *const _ as *const u8, bytes.as_mut_ptr(), 8);
ptr::copy_nonoverlapping(&h2 as *const _ as *const u8, bytes.as_mut_ptr().add(8), 8);
}
bytes
}
Expand Down

0 comments on commit 4ca22cf

Please sign in to comment.