Skip to content

Commit

Permalink
[bevy_core/bytes] Fix UB with accessing memory with incorrect alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanSWard committed Apr 19, 2021
1 parent 20673db commit d7425d3
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions crates/bevy_core/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@ where
T: Byteable + Clone,
{
fn from_bytes(bytes: &[u8]) -> Self {
unsafe {
let byte_ptr = bytes.as_ptr();
let ptr = byte_ptr as *const Self;
(*ptr).clone()
}
unsafe { bytes.as_ptr().cast::<Self>().read_unaligned() }
}
}

Expand Down Expand Up @@ -170,10 +166,8 @@ where
{
fn from_bytes(bytes: &[u8]) -> Self {
unsafe {
let byte_ptr = bytes.as_ptr() as *const T;
let len = bytes.len() / std::mem::size_of::<T>();
let slice = core::slice::from_raw_parts::<T>(byte_ptr, len);
slice.to_vec()
let (_, body, _) = bytes.align_to::<T>();
body.to_vec()
}
}
}
Expand Down

0 comments on commit d7425d3

Please sign in to comment.