Skip to content

Commit b20a9fb

Browse files
authored
Merge commit from fork
Fix potential out of bound read in `json_string_unescape`.
2 parents 57911f1 + cf242d8 commit b20a9fb

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Changes
22

3+
* Fix a potential crash in the C extension parser.
34
* Raise a ParserError on all incomplete unicode escape sequence. This was the behavior until `2.10.0` unadvertently changed it.
45
* Ensure document snippets that are included in parser errors don't include truncated multibyte characters.
56

ext/json/ext/parser/parser.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ static VALUE json_string_unescape(JSON_ParserState *state, const char *string, c
608608
buffer = RSTRING_PTR(result);
609609
bufferStart = buffer;
610610

611-
while ((pe = memchr(pe, '\\', stringEnd - pe))) {
611+
while (pe < stringEnd && (pe = memchr(pe, '\\', stringEnd - pe))) {
612612
unescape = (char *) "?";
613613
unescape_len = 1;
614614
if (pe > p) {

test/json/json_parser_test.rb

+7
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,13 @@ def test_parse_leading_slash
673673
end
674674
end
675675

676+
def test_parse_malformated_unicode_escapes
677+
assert_equal "�|", JSON.parse('"\\u1gef|"')
678+
assert_equal "�|", JSON.parse('"\\u12ge|"')
679+
assert_equal "�|", JSON.parse('"\\u123g|"')
680+
assert_equal '�\\\\"', JSON.parse('"\\u1\\\\\\\\\\\\\\\\"')
681+
end
682+
676683
private
677684

678685
def string_deduplication_available?

0 commit comments

Comments
 (0)