Skip to content

write_float writes null for NaN and infinity - #70

Open
yuxi-liu-wired wants to merge 1 commit into
simd-lite:mainfrom
yuxi-liu-wired:pr/write-float
Open

yuxi-liu-wired wants to merge 1 commit into
simd-lite:mainfrom
yuxi-liu-wired:pr/write-float

Conversation

@yuxi-liu-wired

Copy link
Copy Markdown

fix: write_float writes null for NaN and infinity

BaseGenerator::write_float passed every f64 to ryu::Buffer:: format_finite, whose output for non-finite input is unspecified. NaN
came out as 2.696539702293474e308 and infinity as
1.797693134862316e308, so simd-json serialized

simd_json::to_string(&f64::NAN)        "2.696539702293474e308"
simd_json::to_string(&f64::INFINITY)   "1.797693134862316e308"

(and the same from Value::encode). Both are valid JSON numbers but not
the value: the first reads back as infinity. JSON has no NaN or
infinity; serde_json writes null. write_float now does the same.

Test: test_write_float_non_finite (fails before this change).

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.

`BaseGenerator::write_float` passed every f64 to `ryu::Buffer::
format_finite`, whose output for non-finite input is unspecified. NaN
came out as `2.696539702293474e308` and infinity as
`1.797693134862316e308`, so simd-json serialized

    simd_json::to_string(&f64::NAN)        "2.696539702293474e308"
    simd_json::to_string(&f64::INFINITY)   "1.797693134862316e308"

(and the same from `Value::encode`). Both are valid JSON numbers but not
the value: the first reads back as infinity. JSON has no NaN or
infinity; serde_json writes `null`. `write_float` now does the same.

Test: `test_write_float_non_finite` (fails before this change).

Found by a differential fuzzer (serde_json, simd-json, sonic-rs, jiter)
extended to typed serialization.
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.

2 participants