fix: avoid no_std panic in ceil helpers on non-finite input

Byte::from_f64/from_f32 let +inf past the size >= 0.0 guard and then called
the no_std ceil helpers, which used Decimal::from_f64(v).unwrap(). Decimal
conversion returns None for non-finite values, so this panicked in no_std
builds while std returned None. Fall back to the input value for non-finite
input so no_std matches std and the documented None contract is upheld.
This commit is contained in:
dualfroz committed 2026-09-04 22:00:21 +02:00
1 parent 542a618c5d
commit 2219c475f1
2 files changed
+24 -2

No files matched your search

+14 -2
View File
@@ -36,7 +36,13 @@ pub(crate) fn ceil_f64(v: f64) -> f64 {
pub(crate) fn ceil_f64(v: f64) -> f64 {
debug_assert!(v >= 0.0);
Decimal::from_f64(v).unwrap().ceil().to_f64().unwrap()
// `Decimal::from_f64` returns `None` for non-finite values (e.g. infinity),
// so fall back to the input to mirror `f64::ceil` in the `std` path and
// avoid panicking.
match Decimal::from_f64(v) {
Some(d) => d.ceil().to_f64().unwrap_or(v),
None => v,
}
}
#[cfg(any(feature = "byte", feature = "bit"))]
@@ -54,7 +60,13 @@ pub(crate) fn ceil_f32(v: f32) -> f32 {
pub(crate) fn ceil_f32(v: f32) -> f32 {
debug_assert!(v >= 0.0);
Decimal::from_f32(v).unwrap().ceil().to_f32().unwrap()
// `Decimal::from_f32` returns `None` for non-finite values (e.g. infinity),
// so fall back to the input to mirror `f32::ceil` in the `std` path and
// avoid panicking.
match Decimal::from_f32(v) {
Some(d) => d.ceil().to_f32().unwrap_or(v),
None => v,
}
}
#[cfg(any(feature = "byte", feature = "bit"))]