Skip to content

Commit

Permalink
Merge pull request #507 from jhawthorn/infinity_underflow
Browse files Browse the repository at this point in the history
Fix "unexpected token" offset for Infinity
  • Loading branch information
hsbt committed Dec 1, 2023
2 parents 3bfa53f + 42ac170 commit fa4725f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ext/json/ext/parser/parser.c
Expand Up @@ -587,7 +587,7 @@ cs = 0;
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);
}
}
goto st29;
Expand Down
2 changes: 1 addition & 1 deletion ext/json/ext/parser/parser.rl
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
19 changes: 19 additions & 0 deletions tests/json_ext_parser_test.rb
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,22 @@ 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

unless RUBY_PLATFORM =~ /java/
ex = assert_raise(ParserError) { parse('-Infinity') }
assert_equal "unexpected token at '-Infinity'", ex.message
end

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 fa4725f

Please sign in to comment.