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

Do not permit leading zeros in JSON numbers #3167

Merged
merged 1 commit into from
Jun 4, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions packages/json/_test.pony
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ class iso _TestParseNumber is UnitTest
doc.parse("0")?
h.assert_eq[I64](0, doc.data as I64)

doc.parse("-0")?
h.assert_eq[I64](0, doc.data as I64)

doc.parse("13")?
h.assert_eq[I64](13, doc.data as I64)

Expand Down Expand Up @@ -110,6 +113,11 @@ class iso _TestParseNumber is UnitTest
h.assert_error({() ? => JsonDoc.parse("1.-3")? })
h.assert_error({() ? => JsonDoc.parse("1e")? })

// RFC 8259 does not permit leading zeros
h.assert_error({() ? => JsonDoc.parse("01")? })
h.assert_error({() ? => JsonDoc.parse("-01")? })
h.assert_error({() ? => JsonDoc.parse("-012")? })

class iso _TestParseString is UnitTest
"""
Test Json parsing of strings.
Expand Down
9 changes: 8 additions & 1 deletion packages/json/json_doc.pony
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,20 @@ class JsonDoc
_get_char()? // Consume -
end

let leading_zero = _peek_char()? == '0'

var frac: I64 = 0
var frac_digits: U8 = 0
var exp: I64 = 0
var exp_digits: U8 = 0

// Start with integer part
(let int, _) = _parse_decimal()?
(let int, let int_digits) = _parse_decimal()?

if (int_digits > 1) and (leading_zero == true) then
_error("Leading 0 not permitted")
error
end

if _peek_char()? == '.' then
// We have a . so expect a fractional part
Expand Down