From 565f181f656439847ef79650caad5c0b6c20171b Mon Sep 17 00:00:00 2001 From: Eric Hawicz Date: Fri, 8 Nov 2024 22:20:40 -0500 Subject: [PATCH] Fix issue #875: cast to unsigned char so bytes above 0x7f aren't interpreted as negative, which was causing the strict-mode control characters check to incorrectly trigger. --- json_tokener.c | 2 +- tests/test_parse.c | 17 ++++++++++++++--- tests/test_parse.expected | 13 ++++++++++--- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/json_tokener.c b/json_tokener.c index c831f8a..773229e 100644 --- a/json_tokener.c +++ b/json_tokener.c @@ -678,7 +678,7 @@ struct json_object *json_tokener_parse_ex(struct json_tokener *tok, const char * state = json_tokener_state_string_escape; break; } - else if ((tok->flags & JSON_TOKENER_STRICT) && c <= 0x1f) + else if ((tok->flags & JSON_TOKENER_STRICT) && (unsigned char)c <= 0x1f) { // Disallow control characters in strict mode tok->err = json_tokener_error_parse_string; diff --git a/tests/test_parse.c b/tests/test_parse.c index d664a31..525f68c 100644 --- a/tests/test_parse.c +++ b/tests/test_parse.c @@ -297,6 +297,7 @@ struct incremental_step {"d", -1, -1, json_tokener_continue, 0, 0}, {"1", -1, -1, json_tokener_continue, 0, 0}, {"e\"", -1, -1, json_tokener_success, 1, 0}, + /* parse two char at every time */ {"\"\\u", -1, -1, json_tokener_continue, 0, 0}, {"d8", -1, -1, json_tokener_continue, 0, 0}, @@ -322,6 +323,11 @@ struct incremental_step {"\"fff \\ud83d\\ude", -1, -1, json_tokener_continue, 0, 0}, {"00 bar\"", -1, -1, json_tokener_success, 1, 0}, + /* Check a utf-8 char (a+umlaut) that has bytes that look negative when + char are signed (see also control char check below) */ + {"\"\xc3\xa4\"", -1, -1, json_tokener_success, 1, 0}, + {"\"\xc3\xa4\"", -1, -1, json_tokener_success, 1, JSON_TOKENER_STRICT}, + /* Check that json_tokener_reset actually resets */ {"{ \"foo", -1, -1, json_tokener_continue, 1, 0}, {": \"bar\"}", -1, 0, json_tokener_error_parse_unexpected, 1, 0}, @@ -394,8 +400,8 @@ struct incremental_step {"Infinity", 9, 8, json_tokener_success, 1, 0}, {"infinity", 9, 8, json_tokener_success, 1, 0}, - {"-infinity", 10, 9, json_tokener_success, 1, 0}, {"infinity", 9, 0, json_tokener_error_parse_unexpected, 1, JSON_TOKENER_STRICT}, + {"-infinity", 10, 9, json_tokener_success, 1, 0}, {"-infinity", 10, 1, json_tokener_error_parse_unexpected, 1, JSON_TOKENER_STRICT}, {"inf", 3, 3, json_tokener_continue, 0, 0}, @@ -462,12 +468,15 @@ struct incremental_step {"[18446744073709551616]", 23, 21, json_tokener_error_parse_number, 1, JSON_TOKENER_STRICT}, /* XXX this seems like a bug, should fail with _error_parse_number instead */ + {"18446744073709551616", 21, 20, json_tokener_success, 1, 0}, {"18446744073709551616", 21, 20, json_tokener_error_parse_eof, 1, JSON_TOKENER_STRICT}, /* Exceeding integer limits as double parse OK */ {"[9223372036854775808.0]", 24, 23, json_tokener_success, 1, 0}, + {"[-9223372036854775809.0]", 25, 24, json_tokener_success, 1, 0}, {"[-9223372036854775809.0]", 25, 24, json_tokener_success, 1, JSON_TOKENER_STRICT}, {"[18446744073709551615.0]", 25, 24, json_tokener_success, 1, 0}, + {"[18446744073709551616.0]", 25, 24, json_tokener_success, 1, 0}, {"[18446744073709551616.0]", 25, 24, json_tokener_success, 1, JSON_TOKENER_STRICT}, /* offset=1 because "n" is the start of "null". hmm... */ @@ -524,6 +533,7 @@ struct incremental_step {"\"\\a\"", -1, 2, json_tokener_error_parse_string, 1, 0}, /* Check '\'' in strict model */ + {"\'foo\'", -1, 5, json_tokener_success, 1, 0}, {"\'foo\'", -1, 0, json_tokener_error_parse_unexpected, 1, JSON_TOKENER_STRICT}, /* Parse array/object */ @@ -544,9 +554,10 @@ struct incremental_step * in what we accept (up to a point). */ {"[1,2,3,]", -1, -1, json_tokener_success, 0, 0}, + {"[1,2,3,]", -1, 7, json_tokener_error_parse_unexpected, 1, JSON_TOKENER_STRICT}, {"[1,2,,3,]", -1, 5, json_tokener_error_parse_unexpected, 0, 0}, + {"[1,2,,3,]", -1, 5, json_tokener_error_parse_unexpected, 0, JSON_TOKENER_STRICT}, - {"[1,2,3,]", -1, 7, json_tokener_error_parse_unexpected, 1, JSON_TOKENER_STRICT}, {"{\"a\":1,}", -1, 7, json_tokener_error_parse_unexpected, 1, JSON_TOKENER_STRICT}, // utf-8 test @@ -656,7 +667,7 @@ static void test_incremental_parse(void) printf("json_tokener_parse(%s) ... ", string_to_parse); new_obj = json_tokener_parse(string_to_parse); if (new_obj == NULL) - puts("got error as expected"); + printf("%s", "got error as expected\n"); /* test incremental parsing in various forms */ tok = json_tokener_new(); diff --git a/tests/test_parse.expected b/tests/test_parse.expected index 82db5db..cc5dd10 100644 --- a/tests/test_parse.expected +++ b/tests/test_parse.expected @@ -134,6 +134,8 @@ json_tokener_parse_ex(tok, "fff \ud834\udd, 15) ... OK: got correct error: cont json_tokener_parse_ex(tok, 1e bar" , 7) ... OK: got object of type [string]: "fff 𝄞 bar" json_tokener_parse_ex(tok, "fff \ud83d\ude, 15) ... OK: got correct error: continue json_tokener_parse_ex(tok, 00 bar" , 7) ... OK: got object of type [string]: "fff 😀 bar" +json_tokener_parse_ex(tok, "ä" , 4) ... OK: got object of type [string]: "ä" +json_tokener_parse_ex(tok, "ä" , 4) ... OK: got object of type [string]: "ä" json_tokener_parse_ex(tok, { "foo , 6) ... OK: got correct error: continue json_tokener_parse_ex(tok, : "bar"} , 8) ... OK: got correct error: unexpected character json_tokener_parse_ex(tok, { "foo , 6) ... OK: got correct error: continue @@ -177,8 +179,8 @@ json_tokener_parse_ex(tok, null , 4) ... OK: got correct error: continu json_tokener_parse_ex(tok, null , 5) ... OK: got object of type [null]: null json_tokener_parse_ex(tok, Infinity , 9) ... OK: got object of type [double]: Infinity json_tokener_parse_ex(tok, infinity , 9) ... OK: got object of type [double]: Infinity -json_tokener_parse_ex(tok, -infinity , 10) ... OK: got object of type [double]: -Infinity json_tokener_parse_ex(tok, infinity , 9) ... OK: got correct error: unexpected character +json_tokener_parse_ex(tok, -infinity , 10) ... OK: got object of type [double]: -Infinity json_tokener_parse_ex(tok, -infinity , 10) ... OK: got correct error: unexpected character json_tokener_parse_ex(tok, inf , 3) ... OK: got correct error: continue json_tokener_parse_ex(tok, inity , 6) ... OK: got object of type [double]: Infinity @@ -218,11 +220,14 @@ json_tokener_parse_ex(tok, [-9223372036854775809], 23) ... OK: got correct erro json_tokener_parse_ex(tok, [18446744073709551615], 23) ... OK: got object of type [array]: [ 18446744073709551615 ] json_tokener_parse_ex(tok, [18446744073709551616], 23) ... OK: got object of type [array]: [ 18446744073709551615 ] json_tokener_parse_ex(tok, [18446744073709551616], 23) ... OK: got correct error: number expected +json_tokener_parse_ex(tok, 18446744073709551616, 21) ... OK: got object of type [int]: 18446744073709551615 json_tokener_parse_ex(tok, 18446744073709551616, 21) ... OK: got correct error: unexpected end of data json_tokener_parse_ex(tok, [9223372036854775808.0], 24) ... OK: got object of type [array]: [ 9223372036854775808.0 ] json_tokener_parse_ex(tok, [-9223372036854775809.0], 25) ... OK: got object of type [array]: [ -9223372036854775809.0 ] +json_tokener_parse_ex(tok, [-9223372036854775809.0], 25) ... OK: got object of type [array]: [ -9223372036854775809.0 ] json_tokener_parse_ex(tok, [18446744073709551615.0], 25) ... OK: got object of type [array]: [ 18446744073709551615.0 ] json_tokener_parse_ex(tok, [18446744073709551616.0], 25) ... OK: got object of type [array]: [ 18446744073709551616.0 ] +json_tokener_parse_ex(tok, [18446744073709551616.0], 25) ... OK: got object of type [array]: [ 18446744073709551616.0 ] json_tokener_parse_ex(tok, noodle , 7) ... OK: got correct error: null expected json_tokener_parse_ex(tok, naodle , 7) ... OK: got correct error: null expected json_tokener_parse_ex(tok, track , 6) ... OK: got correct error: boolean expected @@ -250,6 +255,7 @@ json_tokener_parse_ex(tok, "\t" , 4) ... OK: got object of type [string json_tokener_parse_ex(tok, "\/" , 4) ... OK: got object of type [string]: "\/" json_tokener_parse_ex(tok, "/" , 3) ... OK: got object of type [string]: "\/" json_tokener_parse_ex(tok, "\a" , 4) ... OK: got correct error: invalid string sequence +json_tokener_parse_ex(tok, 'foo' , 5) ... OK: got object of type [string]: "foo" json_tokener_parse_ex(tok, 'foo' , 5) ... OK: got correct error: unexpected character json_tokener_parse_ex(tok, [1,2,3] , 7) ... OK: got object of type [array]: [ 1, 2, 3 ] json_tokener_parse_ex(tok, [1,2,3} , 7) ... OK: got correct error: array value separator ',' expected @@ -263,8 +269,9 @@ json_tokener_parse_ex(tok, {"a":1 , 6) ... OK: got correct error: continu json_tokener_parse_ex(tok, [,] , 3) ... OK: got correct error: unexpected character json_tokener_parse_ex(tok, [,1] , 4) ... OK: got correct error: unexpected character json_tokener_parse_ex(tok, [1,2,3,] , 8) ... OK: got object of type [array]: [ 1, 2, 3 ] -json_tokener_parse_ex(tok, [1,2,,3,] , 9) ... OK: got correct error: unexpected character json_tokener_parse_ex(tok, [1,2,3,] , 8) ... OK: got correct error: unexpected character +json_tokener_parse_ex(tok, [1,2,,3,] , 9) ... OK: got correct error: unexpected character +json_tokener_parse_ex(tok, [1,2,,3,] , 9) ... OK: got correct error: unexpected character json_tokener_parse_ex(tok, {"a":1,} , 8) ... OK: got correct error: unexpected character json_tokener_parse_ex(tok, "123asc$%&" , 11) ... OK: got object of type [string]: "123asc$%&" json_tokener_parse_ex(tok, "123asc$%&" , 11) ... OK: got object of type [string]: "123asc$%&" @@ -322,5 +329,5 @@ json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence json_tokener_parse_ex(tok, "" , 3) ... OK: got correct error: invalid string sequence -End Incremental Tests OK=230 ERROR=0 +End Incremental Tests OK=237 ERROR=0 ==================================