Skip to content

Commit 2372e95

Browse files
authored
Merge pull request json-c#893 from sffc/supplemental-code-point-bug
Fix bug involving supplemental code points that look like high surrogates
2 parents bf92456 + 7974657 commit 2372e95

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

json_tokener.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ enum json_tokener_error json_tokener_get_error(struct json_tokener *tok)
145145
}
146146

147147
/* Stuff for decoding unicode sequences */
148-
#define IS_HIGH_SURROGATE(uc) (((uc)&0xFC00) == 0xD800)
149-
#define IS_LOW_SURROGATE(uc) (((uc)&0xFC00) == 0xDC00)
148+
#define IS_HIGH_SURROGATE(uc) (((uc)&0xFFFFFC00) == 0xD800)
149+
#define IS_LOW_SURROGATE(uc) (((uc)&0xFFFFFC00) == 0xDC00)
150150
#define DECODE_SURROGATE_PAIR(hi, lo) ((((hi)&0x3FF) << 10) + ((lo)&0x3FF) + 0x10000)
151151
static unsigned char utf8_replacement_char[3] = {0xEF, 0xBF, 0xBD};
152152

tests/test_parse.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ static void test_basic_parse(void)
113113
single_basic_parse("\"\\udd27\"", 0);
114114
// Test with a "short" high surrogate
115115
single_basic_parse("[9,'\\uDAD", 0);
116+
single_basic_parse("\"[9,'\\uDAD\"", 0);
117+
// Test with a supplemental character that looks like a high surrogate
118+
single_basic_parse("\"\\uD836\\uDE87\"", 0);
116119
single_basic_parse("null", 0);
117120
single_basic_parse("NaN", 0);
118121
single_basic_parse("-NaN", 0); /* non-sensical, returns null */
@@ -332,6 +335,11 @@ struct incremental_step
332335
{"{ \"foo", -1, -1, json_tokener_continue, 1, 0},
333336
{": \"bar\"}", -1, 0, json_tokener_error_parse_unexpected, 1, 0},
334337

338+
/* Check a supplemental code point that looks like a high surrogate */
339+
{"\"\\uD836", -1, -1, json_tokener_continue, 0, 0},
340+
{"\\uDE87", -1, -1, json_tokener_continue, 0, 0},
341+
{"\"", -1, -1, json_tokener_success, 1, 0},
342+
335343
/* Check incremental parsing with trailing characters */
336344
{"{ \"foo", -1, -1, json_tokener_continue, 0, 0},
337345
{"\": {\"bar", -1, -1, json_tokener_continue, 0, 0},

tests/test_parse.expected

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ new_obj.to_string("\ud840\u4e16")="�世"
1313
new_obj.to_string("\ud840")="�"
1414
new_obj.to_string("\udd27")="�"
1515
new_obj.to_string([9,'\uDAD)=null
16+
new_obj.to_string("[9,'\uDAD")=null
17+
new_obj.to_string("\uD836\uDE87")="𝪇"
1618
new_obj.to_string(null)=null
1719
new_obj.to_string(NaN)=NaN
1820
new_obj.to_string(-NaN)=null
@@ -138,6 +140,9 @@ json_tokener_parse_ex(tok, "ä" , 4) ... OK: got object of type [string
138140
json_tokener_parse_ex(tok, "ä" , 4) ... OK: got object of type [string]: "ä"
139141
json_tokener_parse_ex(tok, { "foo , 6) ... OK: got correct error: continue
140142
json_tokener_parse_ex(tok, : "bar"} , 8) ... OK: got correct error: unexpected character
143+
json_tokener_parse_ex(tok, "\uD836 , 7) ... OK: got correct error: continue
144+
json_tokener_parse_ex(tok, \uDE87 , 6) ... OK: got correct error: continue
145+
json_tokener_parse_ex(tok, " , 1) ... OK: got object of type [string]: "𝪇"
141146
json_tokener_parse_ex(tok, { "foo , 6) ... OK: got correct error: continue
142147
json_tokener_parse_ex(tok, ": {"bar , 8) ... OK: got correct error: continue
143148
json_tokener_parse_ex(tok, ":13}}XXXX , 10) ... OK: got object of type [object]: { "foo": { "bar": 13 } }
@@ -363,5 +368,5 @@ json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid
363368
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
364369
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
365370
json_tokener_parse_ex(tok, {"":1} , 7) ... OK: got correct error: invalid string sequence
366-
End Incremental Tests OK=269 ERROR=0
371+
End Incremental Tests OK=272 ERROR=0
367372
==================================

0 commit comments

Comments
 (0)