From 6e9e934b5a536efb8df34d6336536b16970b310a Mon Sep 17 00:00:00 2001 From: Krzysztof Gabis Date: Tue, 10 Jun 2014 20:59:35 +0200 Subject: [PATCH] Bugfix in parsing escaped characters (+ additional tests to prevent this bug in future). Thanks to Joseph Werle for bringing attention to it. --- parson.c | 17 ++++++++++------- tests.c | 2 ++ tests/test_2.txt | 3 ++- tests/test_2_comments.txt | 4 +++- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/parson.c b/parson.c index 64124af..319af19 100644 --- a/parson.c +++ b/parson.c @@ -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) diff --git a/tests.c b/tests.c index ba30089..ec56759 100644 --- a/tests.c +++ b/tests.c @@ -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) { diff --git a/tests/test_2.txt b/tests/test_2.txt index ff4e443..8311b30 100644 --- a/tests/test_2.txt +++ b/tests/test_2.txt @@ -23,5 +23,6 @@ "*/" : null, "/**/" : "comment", "//" : "comment", - "#" : "comment" + "url" : "https:\/\/www.example.com\/search?q=12345", + "escaped chars" : "\" \\ \/" } diff --git a/tests/test_2_comments.txt b/tests/test_2_comments.txt index ece878c..8ec2a07 100644 --- a/tests/test_2_comments.txt +++ b/tests/test_2_comments.txt @@ -29,7 +29,9 @@ "nested array" : ["lorem", "ipsum"] }, "*/" : null, "/**/" : "comment", - "//" : "comment" + "//" : "comment", + "url" : "https:\/\/www.example.com\/search?q=12345", + "escaped chars" : "\" \\ \/" } /**/ // \ No newline at end of file