mirror of
https://github.com/Lokathor/bytemuck.git
synced 2024-11-21 14:22:26 +00:00
fix try_pod_read_unaligned
... never having worked correctly (#138)
It seems like a copy-paste error has happened between `try_from_bytes`/`try_from_bytes_mut` and `try_pod_read_unaligned`, causing `internal::try_pod_read_unaligned` to try to read a &T::Bits instead of a T::Bits, usually failing with a `SizeMismatch` error. In the worst case, this allows UB in safe code by having a type allowing any bit pattern and being pointer-sized.
This commit is contained in:
parent
09dd2ffd68
commit
f1571512d2
@ -186,6 +186,21 @@ fn anybitpattern_implies_zeroable() {
|
||||
assert_eq!(test, AnyBitPatternTest { a: 0, b: 0 });
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn checkedbitpattern_try_pod_read_unaligned() {
|
||||
let pod = [0u8];
|
||||
let res = bytemuck::checked::try_pod_read_unaligned::<
|
||||
CheckedBitPatternEnumWithValues,
|
||||
>(&pod);
|
||||
assert!(res.is_ok());
|
||||
|
||||
let pod = [5u8];
|
||||
let res = bytemuck::checked::try_pod_read_unaligned::<
|
||||
CheckedBitPatternEnumWithValues,
|
||||
>(&pod);
|
||||
assert!(res.is_err());
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
|
||||
#[repr(C, align(16))]
|
||||
struct Issue127 {}
|
||||
|
@ -249,7 +249,7 @@ pub fn try_pod_read_unaligned<T: CheckedBitPattern>(
|
||||
) -> Result<T, CheckedCastError> {
|
||||
let pod = unsafe { internal::try_pod_read_unaligned(bytes) }?;
|
||||
|
||||
if <T as CheckedBitPattern>::is_valid_bit_pattern(pod) {
|
||||
if <T as CheckedBitPattern>::is_valid_bit_pattern(&pod) {
|
||||
Ok(unsafe { transmute!(pod) })
|
||||
} else {
|
||||
Err(CheckedCastError::InvalidBitPattern)
|
||||
|
Loading…
Reference in New Issue
Block a user