diff --git a/src/lib.rs b/src/lib.rs index d6c5011..05c0de5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -209,12 +209,15 @@ macro_rules! impl_i16be { { let sl = unsafe { std::slice::from_raw_parts_mut($arr, $size) }; for xx in sl.iter_mut() { - if *xx < i16::MIN as $tt { return *xx } - if *xx > i16::MAX as $tt { return *xx } + // All source types represent the target bounds exactly in f64. + let value = *xx as f64; + if !value.is_finite() || value < i16::MIN as f64 || value > i16::MAX as f64 { + return *xx; + } let mut buf = [0; size_of::<$tt>()]; BigEndian::write_i16(&mut buf, *xx as i16); - *xx = <$tt>::from_le_bytes(buf); + *xx = <$tt>::from_ne_bytes(buf); } 0 as $tt } @@ -227,12 +230,14 @@ macro_rules! impl_i32be { { let sl = unsafe { std::slice::from_raw_parts_mut($arr, $size) }; for xx in sl.iter_mut() { - if *xx < i32::MIN as $tt { return *xx } - if *xx > i32::MAX as $tt { return *xx } + let value = *xx as f64; + if !value.is_finite() || value < i32::MIN as f64 || value > i32::MAX as f64 { + return *xx; + } let mut buf = [0; size_of::<$tt>()]; BigEndian::write_i32(&mut buf, *xx as i32); - *xx = <$tt>::from_le_bytes(buf); + *xx = <$tt>::from_ne_bytes(buf); } 0 as $tt } diff --git a/tests/numeric_packing.rs b/tests/numeric_packing.rs new file mode 100644 index 0000000..4001514 --- /dev/null +++ b/tests/numeric_packing.rs @@ -0,0 +1,103 @@ +use klamath_rs_ext::*; +use std::{mem::size_of, slice}; + +// Read the packed prefix of each source slot, as the Python wrapper does. +fn packed_bytes(values: &[T], width: usize) -> Vec { + let bytes = unsafe { slice::from_raw_parts(values.as_ptr().cast::(), size_of_val(values)) }; + bytes + .chunks_exact(size_of::()) + .flat_map(|slot| slot[..width].iter().copied()) + .collect() +} + +macro_rules! check_int2 { + ($ty:ty, $function:ident) => {{ + let mut values = [0 as $ty, 1 as $ty, 256 as $ty, 32767 as $ty]; + assert_eq!( + unsafe { $function(values.as_mut_ptr(), values.len()) }, + 0 as $ty + ); + assert_eq!(packed_bytes(&values, 2), [0, 0, 0, 1, 1, 0, 0x7f, 0xff]); + }}; +} + +macro_rules! check_int4 { + ($ty:ty, $function:ident) => {{ + let mut values = [0 as $ty, 1 as $ty, 65536 as $ty, 2147483520_i64 as $ty]; + assert_eq!( + unsafe { $function(values.as_mut_ptr(), values.len()) }, + 0 as $ty + ); + assert_eq!( + packed_bytes(&values, 4), + [0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0x7f, 0xff, 0xff, 0x80] + ); + }}; +} + +#[test] +fn all_source_types_pack_to_big_endian_integer_bytes() { + check_int2!(u16, u16_to_i16); + check_int2!(u32, u32_to_i16); + check_int2!(u64, u64_to_i16); + check_int2!(i16, i16_to_i16); + check_int2!(i32, i32_to_i16); + check_int2!(i64, i64_to_i16); + check_int2!(f32, f32_to_i16); + check_int2!(f64, f64_to_i16); + check_int4!(u32, u32_to_i32); + check_int4!(u64, u64_to_i32); + check_int4!(i32, i32_to_i32); + check_int4!(i64, i64_to_i32); + check_int4!(f32, f32_to_i32); + check_int4!(f64, f64_to_i32); +} + +#[test] +fn signed_limits_and_unsigned_overflows_are_checked() { + let mut signed = [i64::from(i16::MIN), -1]; + assert_eq!(unsafe { i64_to_i16(signed.as_mut_ptr(), signed.len()) }, 0); + assert_eq!(packed_bytes(&signed, 2), [0x80, 0, 0xff, 0xff]); + let mut signed = [i64::from(i32::MIN), i64::from(i32::MAX)]; + assert_eq!(unsafe { i64_to_i32(signed.as_mut_ptr(), signed.len()) }, 0); + assert_eq!( + packed_bytes(&signed, 4), + [0x80, 0, 0, 0, 0x7f, 0xff, 0xff, 0xff] + ); + + for value in [32768_u64, u64::MAX] { + let mut input = [value]; + assert_eq!(unsafe { u64_to_i16(input.as_mut_ptr(), 1) }, value); + assert_eq!(input, [value]); + } + for value in [2147483648_u64, u64::MAX] { + let mut input = [value]; + assert_eq!(unsafe { u64_to_i32(input.as_mut_ptr(), 1) }, value); + assert_eq!(input, [value]); + } +} + +#[test] +fn nonfinite_floats_and_rounded_up_f32_limit_are_rejected() { + for convert in [f32_to_i16, f32_to_i32] { + for value in [f32::NAN, f32::INFINITY, f32::NEG_INFINITY] { + let mut input = [value]; + let result = unsafe { convert(input.as_mut_ptr(), 1) }; + assert_ne!(result, 0.0); + assert_eq!(input[0].to_bits(), value.to_bits()); + } + } + for convert in [f64_to_i16, f64_to_i32] { + for value in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] { + let mut input = [value]; + assert_ne!(unsafe { convert(input.as_mut_ptr(), 1) }, 0.0); + assert_eq!(input[0].to_bits(), value.to_bits()); + } + } + let mut input = [2147483648_f32]; + assert_eq!(unsafe { f32_to_i32(input.as_mut_ptr(), 1) }, 2147483648_f32); + assert_eq!(input, [2147483648_f32]); + let mut input = [-2147483648_f32]; + assert_eq!(unsafe { f32_to_i32(input.as_mut_ptr(), 1) }, 0.0); + assert_eq!(packed_bytes(&input, 4), [0x80, 0, 0, 0]); +}