summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Frank <flori@ping.de>2017-03-23 14:26:12 +0100
committerFlorian Frank <flori@ping.de>2017-04-10 09:35:59 +0200
commit1cf49cae8dafafe7da52d4cb38bdf3e3b3c247bb (patch)
treef7fe8621bc9180385f8adb85661f8dc4ac17ba53
parente3faab528d402dda126df5cb9468fea15ba58ce0 (diff)
downloadjson-1cf49cae8dafafe7da52d4cb38bdf3e3b3c247bb.tar.gz
Raise exception
for incomplete unicode surrogates/character escape sequences
-rw-r--r--ext/json/ext/parser/parser.c46
-rw-r--r--ext/json/ext/parser/parser.rl12
-rw-r--r--json.gemspecbin5473 -> 5474 bytes
-rw-r--r--json_pure.gemspec4
-rw-r--r--tests/json_encoding_test.rb2
5 files changed, 41 insertions, 23 deletions
diff --git a/ext/json/ext/parser/parser.c b/ext/json/ext/parser/parser.c
index 302ba76..c0a240a 100644
--- a/ext/json/ext/parser/parser.c
+++ b/ext/json/ext/parser/parser.c
@@ -1435,13 +1435,21 @@ static VALUE json_string_unescape(VALUE result, char *string, char *stringEnd)
break;
case 'u':
if (pe > stringEnd - 4) {
- return Qnil;
+ rb_enc_raise(
+ EXC_ENCODING eParserError,
+ "%u: incomplete unicode character escape sequence at '%s'", __LINE__, p
+ );
} else {
UTF32 ch = unescape_unicode((unsigned char *) ++pe);
pe += 3;
if (UNI_SUR_HIGH_START == (ch & 0xFC00)) {
pe++;
- if (pe > stringEnd - 6) return Qnil;
+ if (pe > stringEnd - 6) {
+ rb_enc_raise(
+ EXC_ENCODING eParserError,
+ "%u: incomplete surrogate pair at '%s'", __LINE__, p
+ );
+ }
if (pe[0] == '\\' && pe[1] == 'u') {
UTF32 sur = unescape_unicode((unsigned char *) pe + 2);
ch = (((ch & 0x3F) << 10) | ((((ch >> 6) & 0xF) + 1) << 16)
@@ -1471,7 +1479,7 @@ static VALUE json_string_unescape(VALUE result, char *string, char *stringEnd)
}
-#line 1475 "parser.c"
+#line 1483 "parser.c"
enum {JSON_string_start = 1};
enum {JSON_string_first_final = 8};
enum {JSON_string_error = 0};
@@ -1479,7 +1487,7 @@ enum {JSON_string_error = 0};
enum {JSON_string_en_main = 1};
-#line 504 "parser.rl"
+#line 512 "parser.rl"
static int
@@ -1501,15 +1509,15 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu
*result = rb_str_buf_new(0);
-#line 1505 "parser.c"
+#line 1513 "parser.c"
{
cs = JSON_string_start;
}
-#line 525 "parser.rl"
+#line 533 "parser.rl"
json->memo = p;
-#line 1513 "parser.c"
+#line 1521 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1534,7 +1542,7 @@ case 2:
goto st0;
goto st2;
tr2:
-#line 490 "parser.rl"
+#line 498 "parser.rl"
{
*result = json_string_unescape(*result, json->memo + 1, p);
if (NIL_P(*result)) {
@@ -1545,14 +1553,14 @@ tr2:
{p = (( p + 1))-1;}
}
}
-#line 501 "parser.rl"
+#line 509 "parser.rl"
{ p--; {p++; cs = 8; goto _out;} }
goto st8;
st8:
if ( ++p == pe )
goto _test_eof8;
case 8:
-#line 1556 "parser.c"
+#line 1564 "parser.c"
goto st0;
st3:
if ( ++p == pe )
@@ -1628,7 +1636,7 @@ case 7:
_out: {}
}
-#line 527 "parser.rl"
+#line 535 "parser.rl"
if (json->create_additions && RTEST(match_string = json->match_string)) {
VALUE klass;
@@ -1808,7 +1816,7 @@ static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self)
}
-#line 1812 "parser.c"
+#line 1820 "parser.c"
enum {JSON_start = 1};
enum {JSON_first_final = 10};
enum {JSON_error = 0};
@@ -1816,7 +1824,7 @@ enum {JSON_error = 0};
enum {JSON_en_main = 1};
-#line 720 "parser.rl"
+#line 728 "parser.rl"
/*
@@ -1833,16 +1841,16 @@ static VALUE cParser_parse(VALUE self)
GET_PARSER;
-#line 1837 "parser.c"
+#line 1845 "parser.c"
{
cs = JSON_start;
}
-#line 736 "parser.rl"
+#line 744 "parser.rl"
p = json->source;
pe = p + json->len;
-#line 1846 "parser.c"
+#line 1854 "parser.c"
{
if ( p == pe )
goto _test_eof;
@@ -1876,7 +1884,7 @@ st0:
cs = 0;
goto _out;
tr2:
-#line 712 "parser.rl"
+#line 720 "parser.rl"
{
char *np = JSON_parse_value(json, p, pe, &result, 0);
if (np == NULL) { p--; {p++; cs = 10; goto _out;} } else {p = (( np))-1;}
@@ -1886,7 +1894,7 @@ st10:
if ( ++p == pe )
goto _test_eof10;
case 10:
-#line 1890 "parser.c"
+#line 1898 "parser.c"
switch( (*p) ) {
case 13: goto st10;
case 32: goto st10;
@@ -1975,7 +1983,7 @@ case 9:
_out: {}
}
-#line 739 "parser.rl"
+#line 747 "parser.rl"
if (cs >= JSON_first_final && p == pe) {
return result;
diff --git a/ext/json/ext/parser/parser.rl b/ext/json/ext/parser/parser.rl
index 5964907..9e1341e 100644
--- a/ext/json/ext/parser/parser.rl
+++ b/ext/json/ext/parser/parser.rl
@@ -446,13 +446,21 @@ static VALUE json_string_unescape(VALUE result, char *string, char *stringEnd)
break;
case 'u':
if (pe > stringEnd - 4) {
- return Qnil;
+ rb_enc_raise(
+ EXC_ENCODING eParserError,
+ "%u: incomplete unicode character escape sequence at '%s'", __LINE__, p
+ );
} else {
UTF32 ch = unescape_unicode((unsigned char *) ++pe);
pe += 3;
if (UNI_SUR_HIGH_START == (ch & 0xFC00)) {
pe++;
- if (pe > stringEnd - 6) return Qnil;
+ if (pe > stringEnd - 6) {
+ rb_enc_raise(
+ EXC_ENCODING eParserError,
+ "%u: incomplete surrogate pair at '%s'", __LINE__, p
+ );
+ }
if (pe[0] == '\\' && pe[1] == 'u') {
UTF32 sur = unescape_unicode((unsigned char *) pe + 2);
ch = (((ch & 0x3F) << 10) | ((((ch >> 6) & 0xF) + 1) << 16)
diff --git a/json.gemspec b/json.gemspec
index 8df035f..52c08c8 100644
--- a/json.gemspec
+++ b/json.gemspec
Binary files differ
diff --git a/json_pure.gemspec b/json_pure.gemspec
index a762e0b..1ae9a65 100644
--- a/json_pure.gemspec
+++ b/json_pure.gemspec
@@ -8,7 +8,7 @@ Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
s.require_paths = ["lib".freeze]
s.authors = ["Florian Frank".freeze]
- s.date = "2017-01-12"
+ s.date = "2017-04-10"
s.description = "This is a JSON implementation in pure Ruby.".freeze
s.email = "flori@ping.de".freeze
s.extra_rdoc_files = ["README.md".freeze]
@@ -17,7 +17,7 @@ Gem::Specification.new do |s|
s.licenses = ["Ruby".freeze]
s.rdoc_options = ["--title".freeze, "JSON implemention for ruby".freeze, "--main".freeze, "README.md".freeze]
s.required_ruby_version = Gem::Requirement.new(">= 1.9".freeze)
- s.rubygems_version = "2.6.8".freeze
+ s.rubygems_version = "2.6.11".freeze
s.summary = "JSON Implementation for Ruby".freeze
s.test_files = ["./tests/test_helper.rb".freeze]
diff --git a/tests/json_encoding_test.rb b/tests/json_encoding_test.rb
index 29ae02e..4e086ef 100644
--- a/tests/json_encoding_test.rb
+++ b/tests/json_encoding_test.rb
@@ -79,6 +79,8 @@ class JSONEncodingTest < Test::Unit::TestCase
json = '["\ud840\udc01"]'
assert_equal json, generate(utf8, :ascii_only => true)
assert_equal utf8, parse(json)
+ assert_raises(JSON::ParserError) { parse('"\u"') }
+ assert_raises(JSON::ParserError) { parse('"\ud800"') }
end
def test_chars