From 1207c209f11097f7348952dc849a59d95a5b1923 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Fri, 15 Dec 2017 16:10:47 +0000 Subject: json-parser: Fix getting immutable root nodes from empty input MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If parsing an empty document, it’s allowed to return NULL from json_parser_get_root(). This was broken for immutable parsers when immutability support was added (an assertion fails). Fix that, and also document that json_parser_get_root() may return NULL. Do the same for json_parser_steal_root() too, which is another way that the root node may be NULL. Add a unit test. Signed-off-by: Philip Withnall (cherry picked from commit ef5707cd0b8d1a1a4ab4edf9ae58c7b3176ef93b) Signed-off-by: Emmanuele Bassi --- json-glib/json-parser.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'json-glib/json-parser.c') diff --git a/json-glib/json-parser.c b/json-glib/json-parser.c index bdb7123..0f87ba4 100644 --- a/json-glib/json-parser.c +++ b/json-glib/json-parser.c @@ -1179,9 +1179,11 @@ json_parser_load_from_data (JsonParser *parser, * json_parser_get_root: * @parser: a #JsonParser * - * Retrieves the top level node from the parsed JSON stream. + * Retrieves the top level node from the parsed JSON stream. If the parser input + * was an empty string, or if parsing failed, this will be %NULL. It will also + * be %NULL if it has been stolen using json_parser_steal_root(). * - * Return value: (transfer none): the root #JsonNode . The returned + * Return value: (transfer none) (nullable): the root #JsonNode . The returned * node is owned by the #JsonParser and should never be modified * or freed. */ @@ -1191,7 +1193,8 @@ json_parser_get_root (JsonParser *parser) g_return_val_if_fail (JSON_IS_PARSER (parser), NULL); /* Sanity check. */ - g_return_val_if_fail (!parser->priv->is_immutable || + g_return_val_if_fail (parser->priv->root == NULL || + !parser->priv->is_immutable || json_node_is_immutable (parser->priv->root), NULL); return parser->priv->root; @@ -1201,9 +1204,10 @@ json_parser_get_root (JsonParser *parser) * json_parser_steal_root: * @parser: a #JsonParser * - * Steals the top level node from the parsed JSON stream. + * Steals the top level node from the parsed JSON stream. This will be %NULL + * in the same situations as json_parser_get_root() returns %NULL. * - * Returns: (transfer full): the top level #JsonNode + * Returns: (transfer full) (nullable): the top level #JsonNode * * Since: 1.4 */ @@ -1214,6 +1218,11 @@ json_parser_steal_root (JsonParser *parser) g_return_val_if_fail (JSON_IS_PARSER (parser), NULL); + /* Sanity check. */ + g_return_val_if_fail (parser->priv->root == NULL || + !parser->priv->is_immutable || + json_node_is_immutable (parser->priv->root), NULL); + return g_steal_pointer (&priv->root); } -- cgit v1.2.1