From 92f31bd99a3c5f7d7ea7c2fd1ab56d83caf786b0 Mon Sep 17 00:00:00 2001 From: Eric Haszlakiewicz Date: Sun, 29 Jul 2012 12:31:07 -0500 Subject: [PATCH] Handle the \f escape sequence (the two characters: backslash followed by an f, not a literal formfeed) and extend the test_parse test to check all valid escape sequences. --- json_tokener.c | 2 ++ tests/test_parse.c | 11 +++++++++++ tests/test_parse.expected | 11 ++++++++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/json_tokener.c b/json_tokener.c index 47768f41de..f5fa8d6030 100644 --- a/json_tokener.c +++ b/json_tokener.c @@ -423,10 +423,12 @@ struct json_object* json_tokener_parse_ex(struct json_tokener *tok, case 'n': case 'r': case 't': + case 'f': if(c == 'b') printbuf_memappend_fast(tok->pb, "\b", 1); else if(c == 'n') printbuf_memappend_fast(tok->pb, "\n", 1); else if(c == 'r') printbuf_memappend_fast(tok->pb, "\r", 1); else if(c == 't') printbuf_memappend_fast(tok->pb, "\t", 1); + else if(c == 'f') printbuf_memappend_fast(tok->pb, "\f", 1); state = saved_state; break; case 'u': diff --git a/tests/test_parse.c b/tests/test_parse.c index 0040760d76..975fb52cb9 100644 --- a/tests/test_parse.c +++ b/tests/test_parse.c @@ -166,6 +166,15 @@ struct incremental_step { /* Strings have a well defined end point, so we can stop at the quote */ { "\"blue\"", -1, -1, json_tokener_success, 0 }, + /* Check each of the escape sequences defined by the spec */ + { "\"\\\"\"", -1, -1, json_tokener_success, 0 }, + { "\"\\\\\"", -1, -1, json_tokener_success, 0 }, + { "\"\\b\"", -1, -1, json_tokener_success, 0 }, + { "\"\\f\"", -1, -1, json_tokener_success, 0 }, + { "\"\\n\"", -1, -1, json_tokener_success, 0 }, + { "\"\\r\"", -1, -1, json_tokener_success, 0 }, + { "\"\\t\"", -1, -1, json_tokener_success, 0 }, + { "[1,2,3]", -1, -1, json_tokener_success, 0 }, /* This behaviour doesn't entirely follow the json spec, but until we have @@ -190,6 +199,8 @@ static void test_incremental_parse() num_error = 0; printf("Starting incremental tests.\n"); + printf("Note: quotes and backslashes seen in the output here are literal values passed\n"); + printf(" to the parse functions. e.g. this is 4 characters: \"\\f\"\n"); string_to_parse = "{ \"foo"; /* } */ printf("json_tokener_parse(%s) ... ", string_to_parse); diff --git a/tests/test_parse.expected b/tests/test_parse.expected index 2481bdeaab..97910dc0ff 100644 --- a/tests/test_parse.expected +++ b/tests/test_parse.expected @@ -21,6 +21,8 @@ new_obj.to_string()={ "abc": 12, "foo": "bar", "bool0": false, "bool1": true, "a json_tokener_parse_versbose() OK ================================== Starting incremental tests. +Note: quotes and backslashes seen in the output here are literal values passed + to the parse functions. e.g. this is 4 characters: "\f" json_tokener_parse({ "foo) ... got error as expected json_tokener_parse_ex(tok, { "foo": 123 }, 14) ... OK: got object of type [object]: { "foo": 123 } json_tokener_parse_ex(tok, { "foo": 456 }, 14) ... OK: got object of type [object]: { "foo": 456 } @@ -39,8 +41,15 @@ json_tokener_parse_ex(tok, "Y" , 3) ... OK: got object of type [string json_tokener_parse_ex(tok, 1 , 1) ... OK: got correct error: continue json_tokener_parse_ex(tok, 2 , 2) ... OK: got object of type [int]: 12 json_tokener_parse_ex(tok, "blue" , 6) ... OK: got object of type [string]: "blue" +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, "\b" , 4) ... OK: got object of type [string]: "\b" +json_tokener_parse_ex(tok, "\f" , 4) ... OK: got object of type [string]: "\u000c" +json_tokener_parse_ex(tok, "\n" , 4) ... OK: got object of type [string]: "\n" +json_tokener_parse_ex(tok, "\r" , 4) ... OK: got object of type [string]: "\r" +json_tokener_parse_ex(tok, "\t" , 4) ... OK: got object of type [string]: "\t" 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,] , 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 -End Incremental Tests OK=20 ERROR=0 +End Incremental Tests OK=27 ERROR=0 ==================================