Use to_ne_bytes() instead of unsound Vec::from_raw_parts

`from_raw_parts()` is only sound if the alignment is the same, so it
can't be used to covert to a `Vec<u8>` from a type with a greater
alignment.

This isn't hard to avoid, luckily.
This commit is contained in:
Ian Douglas Scott 2024-10-11 12:00:47 -07:00 committed by Victoria Brekenfeld
parent 50d6dc3d21
commit 540ed3d170
4 changed files with 43 additions and 78 deletions

View file

@ -509,14 +509,10 @@ where
}
handle_state.states = states.clone();
let states: Vec<u8> = {
let ratio = std::mem::size_of::<States>() / std::mem::size_of::<u8>();
let ptr = states.as_mut_ptr() as *mut u8;
let len = states.len() * ratio;
let cap = states.capacity() * ratio;
std::mem::forget(states);
unsafe { Vec::from_raw_parts(ptr, len, cap) }
};
let states = states
.iter()
.flat_map(|state| (*state as u32).to_ne_bytes())
.collect::<Vec<u8>>();
instance.state(states);
changed = true;
}