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:45:26 +0000 |
| commit | a1490d9b6a3a21774d68f085f805d5f7a68b305e (patch) | |
| tree | fef387429291efba651a52926672116cc47e4861 | |
| parent | c34b3541f9d2cb388bc20fecc9cf412d6ab0716d (diff) | |
| download | json-glib-a1490d9b6a3a21774d68f085f805d5f7a68b305e.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 ee4e765..812f038 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 }, |
