Bugfix in parsing escaped characters (+ additional tests to prevent this bug in future).

Thanks to Joseph Werle for bringing attention to it.
This commit is contained in:
Krzysztof Gabis 2014-06-10 20:59:35 +02:00
parent c707051778
commit 6e9e934b5a
4 changed files with 17 additions and 9 deletions

View File

@ -437,12 +437,14 @@ static const char * get_processed_string(const char **string) {
if (*unprocessed_ptr == '\\') {
unprocessed_ptr++;
switch (*unprocessed_ptr) {
case '\"': case '\\': case '/': break;
case 'b': *processed_ptr = '\b'; break;
case 'f': *processed_ptr = '\f'; break;
case 'n': *processed_ptr = '\n'; break;
case 'r': *processed_ptr = '\r'; break;
case 't': *processed_ptr = '\t'; break;
case '\"': *processed_ptr = '\"'; break;
case '\\': *processed_ptr = '\\'; break;
case '/': *processed_ptr = '/'; break;
case 'b': *processed_ptr = '\b'; break;
case 'f': *processed_ptr = '\f'; break;
case 'n': *processed_ptr = '\n'; break;
case 'r': *processed_ptr = '\r'; break;
case 't': *processed_ptr = '\t'; break;
case 'u':
if (parse_utf_16(&processed_ptr, &unprocessed_ptr) == ERROR) {
parson_free(output);
@ -460,7 +462,8 @@ static const char * get_processed_string(const char **string) {
} else {
*processed_ptr = *unprocessed_ptr;
}
processed_ptr++, unprocessed_ptr++;
processed_ptr++;
unprocessed_ptr++;
}
*processed_ptr = '\0';
if (try_realloc((void**)&output, strlen(output) + 1) == ERROR)

View File

@ -135,6 +135,8 @@ void test_suite_2(JSON_Value *root_value) {
TEST(STREQ(json_object_get_string(root_object, "/**/"), "comment"));
TEST(STREQ(json_object_get_string(root_object, "//"), "comment"));
TEST(STREQ(json_object_get_string(root_object, "url"), "https://www.example.com/search?q=12345"));
TEST(STREQ(json_object_get_string(root_object, "escaped chars"), "\" \\ /"));
}
void test_suite_2_no_comments(void) {

View File

@ -23,5 +23,6 @@
"*/" : null,
"/**/" : "comment",
"//" : "comment",
"#" : "comment"
"url" : "https:\/\/www.example.com\/search?q=12345",
"escaped chars" : "\" \\ \/"
}

View File

@ -29,7 +29,9 @@
"nested array" : ["lorem", "ipsum"] },
"*/" : null,
"/**/" : "comment",
"//" : "comment"
"//" : "comment",
"url" : "https:\/\/www.example.com\/search?q=12345",
"escaped chars" : "\" \\ \/"
}
/**/
//