Skip to content

Commit ec1388f

Browse files
committed
[bevy_core/bytes] Fix UB with accessing memory with incorrect alignment
1 parent 20673db commit ec1388f

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

crates/bevy_core/src/bytes.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,8 @@ where
5050
{
5151
fn from_bytes(bytes: &[u8]) -> Self {
5252
unsafe {
53-
let byte_ptr = bytes.as_ptr();
54-
let ptr = byte_ptr as *const Self;
55-
(*ptr).clone()
53+
let (_, body, _) = bytes.align_to::<Self>();
54+
body[0].clone()
5655
}
5756
}
5857
}
@@ -170,10 +169,8 @@ where
170169
{
171170
fn from_bytes(bytes: &[u8]) -> Self {
172171
unsafe {
173-
let byte_ptr = bytes.as_ptr() as *const T;
174-
let len = bytes.len() / std::mem::size_of::<T>();
175-
let slice = core::slice::from_raw_parts::<T>(byte_ptr, len);
176-
slice.to_vec()
172+
let (_, body, _) = bytes.align_to::<T>();
173+
body.to_vec()
177174
}
178175
}
179176
}

0 commit comments

Comments
 (0)