You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
let bytes = &[1u8,2u8,3u8,4u8]as&[u8];let alt_slice:&[u32] = unsafe{ core::mem::transmute(bytes)};// expected to be expandedassert_eq!(alt_slice[0],1);assert_eq!(alt_slice[1],2);assert_eq!(alt_slice[2],3);assert_eq!(alt_slice[3],4);
This is not true. alt_slice[0] have (mostly) 0x01020304 or 0x04030201 depending on byte-order, and left is looks like garbage and user thinks it is not what (s)he wanted.
Also, this contains UB. Miri reports call to transmute is UB.
I believe: mem::transmute::<&[T], &[U]> is UB if and only if mem::size_of<T> < mem::size_of<U>.
Alternatives:
This may not be MachineApplicable, because:
If user is expected to keep length and transmute each element:
What it does
This prevents a kind of UB.
Examples:
This is not true.
alt_slice[0]
have (mostly)0x01020304
or0x04030201
depending on byte-order, and left is looks like garbage and user thinks it is not what (s)he wanted.Also, this contains UB. Miri reports call to
transmute
is UB.I believe:
mem::transmute::<&[T], &[U]>
is UB if and only ifmem::size_of<T> < mem::size_of<U>
.Alternatives:
This may not be
MachineApplicable
, because:If user is expected to keep length and transmute each element:
If user is expected to transmute entire bit pattern of pointing bit-pattern:
Lint Name
transmute_to_larger_element_slice
Category
correctness
Advantage
Drawbacks
No response
Example
Could be written as:
or
The text was updated successfully, but these errors were encountered: