From 69238c9de68b4939b88d3cfec74257da85dd0db5 Mon Sep 17 00:00:00 2001 From: Lukasz Anforowicz Date: Tue, 29 Aug 2023 19:30:07 +0000 Subject: [PATCH] Verify alignment requirements of `f32` vs `u32` (and `f64` vs `u64`). Fixes https://github.com/BurntSushi/byteorder/issues/194 --- src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index cc37cca..317933d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -71,7 +71,7 @@ cases. #![cfg_attr(not(feature = "std"), no_std)] use core::{ - convert::TryInto, fmt::Debug, hash::Hash, ptr::copy_nonoverlapping, slice, + convert::TryInto, fmt::Debug, hash::Hash, mem::align_of, ptr::copy_nonoverlapping, slice, }; #[cfg(feature = "std")] @@ -1202,6 +1202,7 @@ pub trait ByteOrder: #[inline] fn read_f32_into(src: &[u8], dst: &mut [f32]) { let dst = unsafe { + const _: () = assert!(align_of::() <= align_of::()); slice::from_raw_parts_mut(dst.as_mut_ptr() as *mut u32, dst.len()) }; Self::read_u32_into(src, dst); @@ -1263,6 +1264,7 @@ pub trait ByteOrder: #[inline] fn read_f64_into(src: &[u8], dst: &mut [f64]) { let dst = unsafe { + const _: () = assert!(align_of::() <= align_of::()); slice::from_raw_parts_mut(dst.as_mut_ptr() as *mut u64, dst.len()) }; Self::read_u64_into(src, dst);