diff options
author | Emmanuele Bassi <ebassi@gnome.org> | 2016-02-25 09:42:54 +0000 |
---|---|---|
committer | Emmanuele Bassi <ebassi@gnome.org> | 2016-02-25 09:47:40 +0000 |
commit | 13a632853fb28cab6c9d3c2bff2823e77467abaa (patch) | |
tree | 377590fe043bb9a8cf18702c2ea212b8e986690c | |
parent | 12389dcce39e5248014ef645fd5df583212b415a (diff) | |
download | json-glib-13a632853fb28cab6c9d3c2bff2823e77467abaa.tar.gz |
parser: Detect missing commas in arrays
Just like we detect trailing commas, we should also detect missing ones
to avoid parsing invalid JSON successfully.
-rw-r--r-- | json-glib/json-parser.c | 15 | ||||
-rw-r--r-- | json-glib/tests/invalid.c | 31 |
2 files changed, 45 insertions, 1 deletions
diff --git a/json-glib/json-parser.c b/json-glib/json-parser.c index c0fa855..254f6b9 100644 --- a/json-glib/json-parser.c +++ b/json-glib/json-parser.c @@ -488,12 +488,25 @@ json_parse_array (JsonParser *parser, next_token = json_scanner_peek_next_token (scanner); + /* look for missing commas */ + if (next_token != G_TOKEN_COMMA && next_token != G_TOKEN_RIGHT_BRACE) + { + priv->error_code = JSON_PARSER_ERROR_MISSING_COMMA; + + json_array_unref (array); + json_node_free (priv->current_node); + json_node_free (element); + priv->current_node = old_current; + + return G_TOKEN_COMMA; + } + + /* look for trailing commas */ if (next_token == G_TOKEN_COMMA) { token = json_scanner_get_next_token (scanner); next_token = json_scanner_peek_next_token (scanner); - /* look for trailing commas */ if (next_token == G_TOKEN_RIGHT_BRACE) { priv->error_code = JSON_PARSER_ERROR_TRAILING_COMMA; diff --git a/json-glib/tests/invalid.c b/json-glib/tests/invalid.c index bac540f..c5fad16 100644 --- a/json-glib/tests/invalid.c +++ b/json-glib/tests/invalid.c @@ -143,6 +143,33 @@ test_invalid_object (gconstpointer user_data) } static void +test_missing_comma (gconstpointer user_data) +{ + const char *json = user_data; + GError *error = NULL; + JsonParser *parser; + gboolean res; + + parser = json_parser_new (); + g_assert (JSON_IS_PARSER (parser)); + + if (g_test_verbose ()) + g_print ("invalid data: '%s'...", json); + + res = json_parser_load_from_data (parser, json, -1, &error); + + g_assert (!res); + g_assert_error (error, JSON_PARSER_ERROR, JSON_PARSER_ERROR_MISSING_COMMA); + + if (g_test_verbose ()) + g_print ("expected error: %s\n", error->message); + + g_clear_error (&error); + + g_object_unref (parser); +} + +static void test_trailing_comma (gconstpointer user_data) { const char *json = user_data; @@ -206,6 +233,10 @@ static const struct { "object-6", "{ \"a\" : 0 \"b\" : 1 }", test_invalid_object }, { "object-7", "{ \"\" : false }", test_invalid_object }, + /* missing commas */ + { "missing-comma-1", "[ true false ]", test_missing_comma }, + { "missing-comma-2", "{ \"foo\" : 42 \"bar\": null }", test_missing_comma }, + /* trailing commas */ { "trailing-comma-1", "[ true, ]", test_trailing_comma }, { "trailing-comma-2", "{ \"foo\" : 42, }", test_trailing_comma }, |