Fix no_std panic in Byte::from_f64/from_f32 on non-finite input - #38
Open
dualfroz wants to merge 1 commit into
Open
Fix no_std panic in Byte::from_f64/from_f32 on non-finite input#38dualfroz wants to merge 1 commit into
no_std panic in Byte::from_f64/from_f32 on non-finite input#38dualfroz wants to merge 1 commit into
Conversation
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.
dualfroz
force-pushed
the
fix-nostd-ceil-nonfinite-panic
branch
from
September 5, 2026 23:13
e84be64 to
277dd54
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Byte::from_f64andByte::from_f32are documented to returnNonewhen theinput is "too large" or not
>= 0. Instdbuilds a non-finite input such asf64::INFINITYcorrectly returnsNone, but inno_stdbuilds it panics:So the same call behaves differently across the two build configurations and
breaks the documented contract in
no_std.Root cause
from_f64/from_f32guard the input withif size >= 0.0, which rejectsNaN(sinceNaN >= 0.0isfalse) but lets+infthrough. The value is thenpassed to
ceil_f64/ceil_f32insrc/common.rs.The
stdimplementation usesv.ceil(), which returnsinf; the subsequentinf as u128/as u64cast saturates to the maximum, and the range check thenreturns
None.The
no_stdimplementation instead does:Decimal::from_f64(andfrom_f32) returnsNonefor non-finite values, so.unwrap()panics. Theno_stdhelper therefore diverges from thestdone.Fix
Make the
no_stdceil_f64/ceil_f32fall back to the input value when it isnot representable as a
Decimal(i.e. non-finite), mirroringf64::ceil:With this,
from_f64(f64::INFINITY)flows through the same saturating cast andrange check as in
std, returningNone. Finite values are unaffected.Test
Added
from_non_finite_returns_nonetotests/byte.rs, asserting thatfrom_f64/from_f32returnNonefor both infinity and NaN. It passes under--all-featuresand under--no-default-features --features byte(the configthat previously panicked).