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 ec1388f
Showing 1 changed file with 4 additions and 7 deletions.
11 changes: 4 additions & 7 deletions crates/bevy_core/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ where
{
fn from_bytes(bytes: &[u8]) -> Self {
unsafe {
let byte_ptr = bytes.as_ptr();
let ptr = byte_ptr as *const Self;
(*ptr).clone()
let (_, body, _) = bytes.align_to::<Self>();
body[0].clone()
}
}
}
Expand Down Expand Up @@ -170,10 +169,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 ec1388f

Please sign in to comment.