From 86c0d4eb7ec2f949df0160780230a7d6c4486ceb Mon Sep 17 00:00:00 2001 From: tompng Date: Mon, 20 Jan 2025 20:42:20 +0900 Subject: [PATCH] Fix parsing incomplete unicode escape "\uaaa" --- ext/json/ext/parser/parser.c | 2 +- test/json/json_parser_test.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/ext/json/ext/parser/parser.c b/ext/json/ext/parser/parser.c index 907bd047..de72edf4 100644 --- a/ext/json/ext/parser/parser.c +++ b/ext/json/ext/parser/parser.c @@ -630,7 +630,7 @@ static VALUE json_string_unescape(JSON_ParserState *state, const char *string, c unescape = (char *) "\f"; break; case 'u': - if (pe > stringEnd - 4) { + if (pe > stringEnd - 5) { raise_parse_error("incomplete unicode character escape sequence at '%s'", p); } else { uint32_t ch = unescape_unicode((unsigned char *) ++pe); diff --git a/test/json/json_parser_test.rb b/test/json/json_parser_test.rb index c5ce0232..bca8ff28 100644 --- a/test/json/json_parser_test.rb +++ b/test/json/json_parser_test.rb @@ -302,6 +302,14 @@ def test_parse_broken_string end end + def test_invalid_unicode_escape + assert_raise(JSON::ParserError) { parse('"\u"') } + assert_raise(JSON::ParserError) { parse('"\ua"') } + assert_raise(JSON::ParserError) { parse('"\uaa"') } + assert_raise(JSON::ParserError) { parse('"\uaaa"') } + assert_equal "\uaaaa", parse('"\uaaaa"') + end + def test_parse_big_integers json1 = JSON(orig = (1 << 31) - 1) assert_equal orig, parse(json1)