[json-glib/wip/ordered-iter] Properly detect multiple top level statements
- From: Emmanuele Bassi <ebassi src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [json-glib/wip/ordered-iter] Properly detect multiple top level statements
- Date: Mon, 24 Aug 2020 14:59:09 +0000 (UTC)
commit 5008c4a3b4310563ee2dc19d9c6724134a193133
Author: Emmanuele Bassi <ebassi gnome org>
Date: Mon Aug 24 15:53:24 2020 +0100
Properly detect multiple top level statements
JSON can only have one top level statement. If we get multiple
statements, we should error out appropriately, and we should also avoid
leaking the node for the previously parsed statement.
Fixes: #45
json-glib/json-parser.c | 27 ++++++++++++---------------
json-glib/tests/invalid.c | 1 +
2 files changed, 13 insertions(+), 15 deletions(-)
---
diff --git a/json-glib/json-parser.c b/json-glib/json-parser.c
index 3d77975..4e08848 100644
--- a/json-glib/json-parser.c
+++ b/json-glib/json-parser.c
@@ -122,20 +122,9 @@ json_parser_clear (JsonParser *parser)
{
JsonParserPrivate *priv = parser->priv;
- g_free (priv->variable_name);
- priv->variable_name = NULL;
-
- if (priv->last_error)
- {
- g_error_free (priv->last_error);
- priv->last_error = NULL;
- }
-
- if (priv->root)
- {
- json_node_unref (priv->root);
- priv->root = NULL;
- }
+ g_clear_pointer (&priv->variable_name, g_free);
+ g_clear_pointer (&priv->last_error, g_error_free);
+ g_clear_pointer (&priv->root, json_node_unref);
}
static void
@@ -880,6 +869,14 @@ json_parse_statement (JsonParser *parser,
case G_TOKEN_FLOAT:
case G_TOKEN_STRING:
case G_TOKEN_IDENTIFIER:
+ if (priv->root != NULL)
+ {
+ JSON_NOTE (PARSER, "Only one top level statement is possible");
+ json_scanner_get_next_token (scanner);
+ priv->error_code = JSON_PARSER_ERROR_INVALID_BAREWORD;
+ return G_TOKEN_EOF;
+ }
+
JSON_NOTE (PARSER, "Statement is a value");
token = json_scanner_get_next_token (scanner);
return json_parse_value (parser, scanner, token, &priv->root);
@@ -888,7 +885,7 @@ json_parse_statement (JsonParser *parser,
JSON_NOTE (PARSER, "Unknown statement");
json_scanner_get_next_token (scanner);
priv->error_code = JSON_PARSER_ERROR_INVALID_BAREWORD;
- return G_TOKEN_SYMBOL;
+ return priv->root != NULL ? G_TOKEN_EOF : G_TOKEN_SYMBOL;
}
}
diff --git a/json-glib/tests/invalid.c b/json-glib/tests/invalid.c
index b782566..0ab2d30 100644
--- a/json-glib/tests/invalid.c
+++ b/json-glib/tests/invalid.c
@@ -208,6 +208,7 @@ static const struct
{ "bareword-3", "{ \"foo\" : ponies }", test_invalid_bareword },
{ "bareword-4", "[ 3, 2, 1, lift_off ]", test_invalid_bareword },
{ "bareword-5", "{ foo : 42 }", test_invalid_bareword },
+ { "bareword-6", "\"st\"ring\"", test_invalid_bareword },
/* values */
{ "values-1", "[ -false ]", test_invalid_value },
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]