Skip to content

Commit

Permalink
safety: verify alignment requirements of floats vs ints
Browse files Browse the repository at this point in the history
Fixes #194, Closes #195
  • Loading branch information
anforowicz authored and BurntSushi committed Oct 6, 2023
1 parent c0b6678 commit 52cc70c
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ cases.
#![cfg_attr(miri, allow(dead_code, unused_macros))]

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")]
Expand Down Expand Up @@ -1205,6 +1206,7 @@ pub trait ByteOrder:
#[inline]
fn read_f32_into(src: &[u8], dst: &mut [f32]) {
let dst = unsafe {
const _: () = assert!(align_of::<u32>() <= align_of::<f32>());
slice::from_raw_parts_mut(dst.as_mut_ptr() as *mut u32, dst.len())
};
Self::read_u32_into(src, dst);
Expand Down Expand Up @@ -1266,6 +1268,7 @@ pub trait ByteOrder:
#[inline]
fn read_f64_into(src: &[u8], dst: &mut [f64]) {
let dst = unsafe {
const _: () = assert!(align_of::<u64>() <= align_of::<f64>());
slice::from_raw_parts_mut(dst.as_mut_ptr() as *mut u64, dst.len())
};
Self::read_u64_into(src, dst);
Expand Down

0 comments on commit 52cc70c

Please sign in to comment.