aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohn Hawthorn <john@hawthorn.email>2022-09-19 17:56:19 -0700
committerHiroshi SHIBATA <hsbt@ruby-lang.org>2023-12-01 16:47:06 +0900
commit4b770527c2d4287456d03d645b595662f2501d67 (patch)
treec09461227824efc7a485f78fdd567c6b58e035f5
parent854e6559b6dcda9dcbd6687aef1a3ac0c0c90cac (diff)
downloadruby-4b770527c2d4287456d03d645b595662f2501d67.tar.gz
[flori/json] Fix "unexpected token" offset for Infinity
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. https://github.com/flori/json/commit/42ac170712
-rw-r--r--ext/json/parser/parser.c2
-rw-r--r--ext/json/parser/parser.rl2
-rw-r--r--test/json/json_ext_parser_test.rb19
3 files changed, 21 insertions, 2 deletions
diff --git a/ext/json/parser/parser.c b/ext/json/parser/parser.c
index 876bec5769..095dd99e50 100644
--- a/ext/json/parser/parser.c
+++ b/ext/json/parser/parser.c
@@ -587,7 +587,7 @@ tr25:
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;
diff --git a/ext/json/parser/parser.rl b/ext/json/parser/parser.rl
index 1ecf3317c7..2602d91f55 100644
--- a/ext/json/parser/parser.rl
+++ b/ext/json/parser/parser.rl
@@ -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 {
diff --git a/test/json/json_ext_parser_test.rb b/test/json/json_ext_parser_test.rb
index b5b18fb20b..f49f88b596 100644
--- a/test/json/json_ext_parser_test.rb
+++ b/test/json/json_ext_parser_test.rb
@@ -3,6 +3,8 @@ require_relative 'test_helper'
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
@@ -11,5 +13,22 @@ class JSONExtParserTest < Test::Unit::TestCase
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