mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-12-04 06:11:47 +00:00
fix(core): Fix UB in usize_from_f64 on non-finite and out-of-range floats
This commit is contained in:
1 file changed
+47
-3
+47
-3
@@ -62,14 +62,28 @@ macro_rules! validated {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
#[allow(clippy::as_conversions)]
|
#[allow(clippy::as_conversions, clippy::cast_possible_truncation, clippy::cast_sign_loss)]
|
||||||
pub fn usize_from_f64(val: f64) -> Result<usize, Error> {
|
pub fn usize_from_f64(val: f64) -> Result<usize, Error> {
|
||||||
|
// Exclusive upper bound for values whose truncation fits in `usize`.
|
||||||
|
// Note that `usize::MAX as f64` rounds up to 2^64 on 64-bit targets, so a
|
||||||
|
// strict `<` check against 2^(usize::BITS) rejects all out-of-range values
|
||||||
|
// on both 32-bit and 64-bit targets.
|
||||||
|
#[allow(clippy::cast_precision_loss)]
|
||||||
|
const MAX_USIZE_EXCLUSIVE: f64 = (1_u128 << usize::BITS) as f64;
|
||||||
|
|
||||||
|
if !val.is_finite() {
|
||||||
|
return Err!(Arithmetic("Converting non-finite float to unsigned integer"));
|
||||||
|
}
|
||||||
|
|
||||||
if val < 0.0 {
|
if val < 0.0 {
|
||||||
return Err!(Arithmetic("Converting negative float to unsigned integer"));
|
return Err!(Arithmetic("Converting negative float to unsigned integer"));
|
||||||
}
|
}
|
||||||
|
|
||||||
//SAFETY: <https://doc.rust-lang.org/std/primitive.f64.html#method.to_int_unchecked>
|
if val >= MAX_USIZE_EXCLUSIVE {
|
||||||
Ok(unsafe { val.to_int_unchecked::<usize>() })
|
return Err!(Arithmetic("Converting out-of-range float to unsigned integer"));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(val as usize)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@@ -111,3 +125,33 @@ fn try_into_err<Dst: TryFrom<Src>, Src>(e: <Dst as TryFrom<Src>>::Error) -> Erro
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn clamp<T: Ord>(val: T, min: T, max: T) -> T { cmp::min(cmp::max(val, min), max) }
|
pub fn clamp<T: Ord>(val: T, min: T, max: T) -> T { cmp::min(cmp::max(val, min), max) }
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn usize_from_f64_valid() {
|
||||||
|
assert_eq!(usize_from_f64(0.0).unwrap(), 0);
|
||||||
|
assert_eq!(usize_from_f64(-0.0).unwrap(), 0);
|
||||||
|
assert_eq!(usize_from_f64(42.9).unwrap(), 42);
|
||||||
|
assert_eq!(usize_from_f64(1_048_576.0).unwrap(), 1_048_576);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn usize_from_f64_rejects_invalid() {
|
||||||
|
usize_from_f64(-1.0).unwrap_err();
|
||||||
|
usize_from_f64(-0.5).unwrap_err();
|
||||||
|
usize_from_f64(f64::NAN).unwrap_err();
|
||||||
|
usize_from_f64(f64::INFINITY).unwrap_err();
|
||||||
|
usize_from_f64(f64::NEG_INFINITY).unwrap_err();
|
||||||
|
usize_from_f64(1e30).unwrap_err();
|
||||||
|
#[cfg(target_pointer_width = "64")]
|
||||||
|
{
|
||||||
|
// `usize::MAX as f64` rounds up to 2^64, which is out of range.
|
||||||
|
#[allow(clippy::as_conversions, clippy::cast_precision_loss)]
|
||||||
|
let rounded_max = usize::MAX as f64;
|
||||||
|
usize_from_f64(rounded_max).unwrap_err();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user