Skip to content

Commit

Permalink
Merge pull request #735 from tompng/fix_invalid_number
Browse files Browse the repository at this point in the history
Reject invalid number: `-` `-.1` `-e0`
  • Loading branch information
byroot authored Jan 20, 2025
2 parents 96079a1 + b9bfeec commit 8f1bba3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ext/json/ext/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,8 @@ static VALUE json_parse_any(JSON_ParserState *state, JSON_ParserConfig *config)
raise_parse_error("invalid number: %s", start);
} else if (RB_UNLIKELY(integer_length > 2 && start[0] == '-' && start[1] == '0')) {
raise_parse_error("invalid number: %s", start);
} else if (RB_UNLIKELY(integer_length == 1 && start[0] == '-')) {
raise_parse_error("invalid number: %s", start);
}

if ((state->cursor < state->end) && (*state->cursor == '.')) {
Expand Down
3 changes: 3 additions & 0 deletions test/json/json_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ def test_parse_numbers
assert_raise(JSON::ParserError) { parse('-023.12') }
assert_raise(JSON::ParserError) { parse('023e12') }
assert_raise(JSON::ParserError) { parse('-023e12') }
assert_raise(JSON::ParserError) { parse('-') }
assert_raise(JSON::ParserError) { parse('-.1') }
assert_raise(JSON::ParserError) { parse('-e0') }
assert_equal(23, parse('23'))
assert_equal(-23, parse('-23'))
assert_equal_float(3.141, parse('3.141'))
Expand Down

0 comments on commit 8f1bba3

Please sign in to comment.