Skip to content

Commit

Permalink
Fix "unexpected token" offset for Infinity
Browse files Browse the repository at this point in the history
Previously in the JSON::Ext parser, when we encountered an "Infinity"
token (and weren't allowing NaN/Infinity) we would try to display the
"unexpected token" at the character before.
  • Loading branch information
jhawthorn committed Jan 12, 2023
1 parent 63bc6ae commit 40120bc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ext/json/ext/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@ static char *JSON_parse_value(JSON_Parser *json, char *p, char *pe, VALUE *resul
if (json->allow_nan) {
*result = CInfinity;
} else {
rb_enc_raise(EXC_ENCODING eParserError, "unexpected token at '%s'", p - 8);
rb_enc_raise(EXC_ENCODING eParserError, "unexpected token at '%s'", p - 7);
}
}

Expand Down
2 changes: 1 addition & 1 deletion ext/json/ext/parser/parser.rl
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ static char *JSON_parse_object(JSON_Parser *json, char *p, char *pe, VALUE *resu
if (json->allow_nan) {
*result = CInfinity;
} else {
rb_enc_raise(EXC_ENCODING eParserError, "unexpected token at '%s'", p - 8);
rb_enc_raise(EXC_ENCODING eParserError, "unexpected token at '%s'", p - 7);
}
}
action parse_string {
Expand Down
17 changes: 17 additions & 0 deletions tests/json_ext_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

class JSONExtParserTest < Test::Unit::TestCase
if defined?(JSON::Ext::Parser)
include JSON

def test_allocate
parser = JSON::Ext::Parser.new("{}")
assert_raise(TypeError, '[ruby-core:35079]') do
Expand All @@ -11,5 +13,20 @@ def test_allocate
parser = JSON::Ext::Parser.allocate
assert_raise(TypeError, '[ruby-core:35079]') { parser.source }
end

def test_error_messages
ex = assert_raise(ParserError) { parse('Infinity') }
assert_equal "unexpected token at 'Infinity'", ex.message

ex = assert_raise(ParserError) { parse('-Infinity') }
assert_equal "unexpected token at 'Infinity'", ex.message

ex = assert_raise(ParserError) { parse('NaN') }
assert_equal "unexpected token at 'NaN'", ex.message
end

def parse(json)
JSON::Ext::Parser.new(json).parse
end
end
end

0 comments on commit 40120bc

Please sign in to comment.