fix(packing): validate unsigned and floating point inputs
Compare inputs against integer bounds in f64 so unsigned minima and rounded f32 maxima cannot bypass validation. Reject nonfinite floats and preserve packed bytes in native source slots.
This commit is contained in:
parent
9643089f2d
commit
aa2132dc6e
1 changed files with 114 additions and 6 deletions
17
src/lib.rs
17
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
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue