Skip to content

fix(number): keep the sign of -0 and do not cap the exponent below the mantissa length - #248

Open
yuxi-liu-wired wants to merge 2 commits into
cloudwego:mainfrom
yuxi-liu-wired:pr/numbers
Open

yuxi-liu-wired wants to merge 2 commits into
cloudwego:mainfrom
yuxi-liu-wired:pr/numbers

Conversation

@yuxi-liu-wired

Copy link
Copy Markdown
Contributor

Related fixes in one area, 2 commits (each with its own test).

fix: keep the sign of negative zero when parsing numbers

-0.0, -0e5 and -0 were parsed as positive zero: every zero
fast path in parse_number and parse_number_unchecked returned
ParserNumber::Float(0.0) without looking at negative, although the
literal is IEEE 754 negative zero (as str::parse::<f64> reads it, and
as parse_float32 in the same crate already returns).

sonic_rs::from_str::<f64>("-0.0")    0.0     -> -0.0
sonic_rs::from_str::<Value>("-0")    0.0     -> -0.0
to_string(&from_str::<Value>("-0.0")) "0.0"  -> "-0.0"

The sign survives Value round trips, so serializing a parsed -0.0
now gives back -0.0, as serde_json does.

Tests: test_parse_number_negative_zero (sonic-number, checked and
unchecked paths) and test_negative_zero_keeps_sign (public API: f64,
Value, round trip, inside an array). Both fail without the change.

fix: don't cap the exponent below the mantissa's digit count

parse_exponent stops accumulating at 1000, so e-30800000 and
e-4294967296 both become e-1000. The exponent is then combined with
the number of digits of the mantissa, and for a mantissa longer than
about 1000 digits the capped value is no longer far enough out of
range:

"1{4000 ones}e-4294967296"   1.1111111111111111e-295   (is 0)
"1{4000 ones}e-30800000"     error: not finite         (is 0)
"0.{4000 zeros}1e4294967296" 1e293                     (out of range)
"0.{4000 zeros}1e30800000"   0.0                       (out of range)

Rust's str::parse::<f64> and serde_json give the right results. The
cap is raised to 0x10000, the same bound core::num::dec2flt uses: the
exponent still cannot overflow an i32, and no JSON text reaching the
parser has enough digits to bring it back into range.

Test: test_parse_number_long_mantissa_huge_exponent (fails before
this change). A sweep of 3840 combinations (mantissa length x exponent
x sign, f64/f32/Value) now matches str::parse exactly; 8 differed
before.

Testing

Each commit adds a regression test that fails before the change. The full test suite passes and
cargo fmt --check is clean on the changed files.

Found with a differential fuzzer comparing serde_json, simd-json, sonic-rs and jiter.

@CLAassistant

CLAassistant commented Oct 1, 2026 •

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

yuxi-liu-wired and others added 2 commits November 13, 2026 12:55
`-0.0`, `-0e5` and `-0` were parsed as positive zero: every zero
fast path in `parse_number` and `parse_number_unchecked` returned
`ParserNumber::Float(0.0)` without looking at `negative`, although the
literal is IEEE 754 negative zero (as `str::parse::<f64>` reads it, and
as `parse_float32` in the same crate already returns).

    sonic_rs::from_str::<f64>("-0.0")    0.0     -> -0.0
    sonic_rs::from_str::<Value>("-0")    0.0     -> -0.0
    to_string(&from_str::<Value>("-0.0")) "0.0"  -> "-0.0"

The sign survives `Value` round trips, so serializing a parsed `-0.0`
now gives back `-0.0`, as serde_json does.

Tests: `test_parse_number_negative_zero` (sonic-number, checked and
unchecked paths) and `test_negative_zero_keeps_sign` (public API: f64,
Value, round trip, inside an array). Both fail without the change.

Found by a three-way differential fuzzer (serde_json, simd-json,
sonic-rs).

Co-authored-by: TRAE CLI <[email protected]>
`parse_exponent` stops accumulating at 1000, so `e-30800000` and
`e-4294967296` both become `e-1000`. The exponent is then combined with
the number of digits of the mantissa, and for a mantissa longer than
about 1000 digits the capped value is no longer far enough out of
range:

    "1{4000 ones}e-4294967296"   1.1111111111111111e-295   (is 0)
    "1{4000 ones}e-30800000"     error: not finite         (is 0)
    "0.{4000 zeros}1e4294967296" 1e293                     (out of range)
    "0.{4000 zeros}1e30800000"   0.0                       (out of range)

Rust's `str::parse::<f64>` and serde_json give the right results. The
cap is raised to 0x10000, the same bound `core::num::dec2flt` uses: the
exponent still cannot overflow an i32, and no JSON text reaching the
parser has enough digits to bring it back into range.

Test: `test_parse_number_long_mantissa_huge_exponent` (fails before
this change). A sweep of 3840 combinations (mantissa length x exponent
x sign, f64/f32/Value) now matches `str::parse` exactly; 8 differed
before.

Found by a three-way differential fuzzer (serde_json, simd-json,
sonic-rs, jiter).

Co-authored-by: TRAE CLI <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants