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:
parent
50d6dc3d21
commit
540ed3d170
4 changed files with 43 additions and 78 deletions
|
|
@ -151,15 +151,13 @@ where
|
|||
data_init: &mut DataInit<'_, D>,
|
||||
) {
|
||||
let instance = data_init.init(resource, ());
|
||||
let capabilities = {
|
||||
let mut caps = state.toplevel_management_state().capabilities.clone();
|
||||
let ratio = std::mem::size_of::<ManagementCapabilities>() / std::mem::size_of::<u8>();
|
||||
let ptr = caps.as_mut_ptr() as *mut u8;
|
||||
let len = caps.len() * ratio;
|
||||
let cap = caps.capacity() * ratio;
|
||||
std::mem::forget(caps);
|
||||
unsafe { Vec::from_raw_parts(ptr, len, cap) }
|
||||
};
|
||||
let capabilities = state
|
||||
.toplevel_management_state()
|
||||
.capabilities
|
||||
.iter()
|
||||
.flat_map(|cap| (*cap as u32).to_ne_bytes())
|
||||
.collect::<Vec<u8>>();
|
||||
|
||||
instance.capabilities(capabilities);
|
||||
state.toplevel_management_state().instances.push(instance);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue