fix(serde): error instead of returning null for numbers out of range - #86
Merged
Conversation
#85) A number too large for an `f64` (ex. `1e400`) deserialized to `serde_json::Value::Null` with no error, because `str::parse::<f64>` resolves to infinity instead of erroring and `serde_json` turns non-finite floats into `null`. Now these error with "Number is out of range", which matches what `serde_json::from_str` does for the same input. The hexadecimal branch had the same class of bug, where an out of range number was visited as a string. It now errors as well and additionally handles values that fit in a `u64` (ex. `0x8000000000000000`) or that are exactly `i64::MIN` (`-0x8000000000000000`), which previously deserialized as strings.
Only falls back to an i128 for the values an i64 can't hold, which are the ones that fit in a u64 and `i64::MIN`.
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.
Closes #85
Problem
A JSON number too large for an
f64silently deserialized tonull:str::parse::<f64>resolves tof64::INFINITYinstead of erroring, andserde_json'sValuevisitor routes non-finite floats throughNumber::from_f64, which returnsNoneand producesValue::Null. Deserializing to a plainf64yieldedinf.Fix
visit_numbernow returns aParseErrorwhen the number can't be represented, so{ "amount": 1e400 }errors withNumber is out of range on line 1 column 13. This matchesserde_json::from_str, which errors withnumber out of rangefor the same input.While in there, the hexadecimal branch had the same class of bug — it fell back to visiting the raw literal as a string when it didn't fit in an
i64. It now:0xFFFFFFFFFFFFFFFFFF), instead of deserializing as"0xFFFFFFFFFFFFFFFFFF"i128so values that fit in au64(ex.0x8000000000000000) and-0x8000000000000000(exactlyi64::MIN) deserialize as numbers, instead of as stringsThe trailing
visitor.visit_str(raw)fallback for unparseable decimals is gone since it was unreachable — every number token the scanner produces is valid Rust float syntax, soparse::<f64>only ever fails to give a usable value by overflowing.Behaviour change
Input that previously deserialized (as
null,inf, or a string) now errors, including when the value's field is ignored by the target type —serde_jsonerrors there too.Note this leaves the non-serde conversions diverging:
impl From<ast::Value> for serde_json::ValueandCstNumberLit::to_serde_valuestill turn1e400intoValue::String("1e400"), and their signatures (From/-> Option<_>) can't surface a parse error. Happy to follow up if you'd like those to returnNoneor be documented.Tests
Added
it_should_error_when_number_is_out_of_f64_rangeandit_should_error_when_hexadecimal_number_is_out_of_range, covering positive/negative overflow, error positions, the largest finitef64, underflow to±0.0, out-of-range values in ignored fields, and the hex cases above.