Skip to content

Fix parse_f64_approx returning NaN for 0e400 - #991

Open
Sebastian Turner (auraseb) wants to merge 1 commit into
microsoft:mainfrom
auraseb:fix/float-zero-exponent-nan
Open

Sebastian Turner (auraseb) wants to merge 1 commit into
microsoft:mainfrom
auraseb:fix/float-zero-exponent-nan

Conversation

@auraseb

Copy link
Copy Markdown

parse_f64_approx scaled a zero mantissa by 10^exponent. For exponents above about 308 that is 0 * inf, so 0e400 parsed as NaN instead of 0.0, and the JSON parser then rejected it as invalid. A zero mantissa now skips the scaling, which also keeps the sign of -0e400.

The exponent saturates at i32::MAX, but the table index then used exponent + 3, which panicked with an overflow in debug builds for inputs like 1e2147483645. The index now uses wrapping_add, so such inputs fall through to powi and return infinity, as in release builds.

Tests: added zero_with_large_exponent and huge_exponent to float.rs and a 0e400 case to the JSON number test. All three fail on main and pass with this change. cargo test --workspace, cargo fmt --check and cargo clippy --all-targets -- --deny warnings pass.

Fixes #990

Signed-off-by: Sebastian Turner <sebastiant3484@gmail.com>
@auraseb

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JSON parser rejects 0e400 because parse_f64_approx returns NaN for a zero mantissa with a large exponent

1 participant