Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove leading 0s before parsing into serde_json::Number #6766

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changesets/fix_sushi_caboose_police_agency.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### Remove leading 0s before parsing into `serde_json::Number` ([PR #6766](https://github.com/apollographql/router/pull/6766))

We previously decided to allow leading 0s, per [this discussion](https://github.com/apollographql/router/pull/5762#discussion_r1711807550).

However, since we use the parsing logic of `serde_json::Number` to produce the internal representation, and that logic fails given leading zeros, we need to normalize the input `&str` a bit before calling `number.parse()`. We already perform some similar normalizations, e.g. ensuring the fractional part is at least a 0 (not empty after the `.`), so there's precedent.

Thanks to @nicholascioli for finding this problem using fuzz testing!

By [@benjamn](https://github.com/benjamn) in https://github.com/apollographql/router/pull/6766
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,14 @@ impl LitExpr {
);

let mut s = String::new();
s.push_str(int.fragment());
let int_str = int.fragment();
// Remove leading zeros to avoid failing the stricter
// number.parse() below, but allow a single zero.
if let Some(first_non_zero) = int_str.chars().position(|c| c != '0') {
benjamn marked this conversation as resolved.
Show resolved Hide resolved
s.push_str(&int_str[first_non_zero..]);
} else {
s.push('0');
}

let full_range = if let Some((_, dot, _, frac)) = frac {
let frac_range = merge_ranges(
Expand Down Expand Up @@ -277,6 +284,7 @@ mod tests {
use crate::sources::connect::json_selection::location::new_span;
use crate::sources::connect::json_selection::PathList;

#[track_caller]
fn check_parse(input: &str, expected: LitExpr) {
match LitExpr::parse(new_span(input)) {
Ok((remainder, parsed)) => {
Expand Down Expand Up @@ -317,6 +325,24 @@ mod tests {
"-123.",
LitExpr::Number(serde_json::Number::from_f64(-123.0).unwrap()),
);
check_parse("00", LitExpr::Number(serde_json::Number::from(0)));
check_parse(
"-00",
LitExpr::Number(serde_json::Number::from_f64(-0.0).unwrap()),
);
Comment on lines +332 to +335
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess negative zero has to be a float, since an integer wouldn't be able to represent the negativity?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds right!

check_parse("0", LitExpr::Number(serde_json::Number::from(0)));
check_parse(
"-0",
LitExpr::Number(serde_json::Number::from_f64(-0.0).unwrap()),
);
check_parse(" 00 ", LitExpr::Number(serde_json::Number::from(0)));
check_parse(" 0 ", LitExpr::Number(serde_json::Number::from(0)));
check_parse(
" - 0 ",
LitExpr::Number(serde_json::Number::from_f64(-0.0).unwrap()),
);
check_parse("001", LitExpr::Number(serde_json::Number::from(1)));
check_parse("0010", LitExpr::Number(serde_json::Number::from(10)));
benjamn marked this conversation as resolved.
Show resolved Hide resolved

check_parse("true", LitExpr::Bool(true));
check_parse(" true ", LitExpr::Bool(true));
Expand Down