[json-glib/coverity-leak] builder, parser: Use g_assert for sanity checks




commit 2629c993cf1ef104cd343395aa45524836446577
Author: Ray Strode <rstrode redhat com>
Date:   Tue Oct 19 14:57:47 2021 -0400

    builder,parser: Use g_assert for sanity checks
    
    Coverity noticed a leak in json_builder_get_root that can't happen
    in practice. Namely, if internal state gets screwed up and runtime checks
    are enabled, json_builder_get_root may return NULL without freeing a copy of the
    builder root it just made.
    
    This is because of a g_return_val_if_fail call to bail early if an
    internal consistency sanity check fails.
    
    This commit addresses the coverity complaint by using g_assert instead
    of g_return_val_if_fail for this sanity check, and other similar
    sanity checks, in the code.

 json-glib/json-builder.c |  6 +++---
 json-glib/json-parser.c  | 12 ++++++------
 2 files changed, 9 insertions(+), 9 deletions(-)
---
diff --git a/json-glib/json-builder.c b/json-glib/json-builder.c
index 45f6cbe..1106ae5 100644
--- a/json-glib/json-builder.c
+++ b/json-glib/json-builder.c
@@ -315,9 +315,9 @@ json_builder_get_root (JsonBuilder *builder)
     root = json_node_copy (builder->priv->root);
 
   /* Sanity check. */
-  g_return_val_if_fail (!builder->priv->immutable ||
-                        root == NULL ||
-                        json_node_is_immutable (root), NULL);
+  g_assert (!builder->priv->immutable ||
+            root == NULL ||
+            json_node_is_immutable (root));
 
   return root;
 }
diff --git a/json-glib/json-parser.c b/json-glib/json-parser.c
index c5e58f4..27cd07a 100644
--- a/json-glib/json-parser.c
+++ b/json-glib/json-parser.c
@@ -1299,9 +1299,9 @@ 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->root == NULL ||
-                        !parser->priv->is_immutable ||
-                        json_node_is_immutable (parser->priv->root), NULL);
+  g_assert (parser->priv->root == NULL ||
+            !parser->priv->is_immutable ||
+            json_node_is_immutable (parser->priv->root));
 
   return parser->priv->root;
 }
@@ -1327,9 +1327,9 @@ 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);
+  g_assert (parser->priv->root == NULL ||
+            !parser->priv->is_immutable ||
+            json_node_is_immutable (parser->priv->root));
 
   return g_steal_pointer (&priv->root);
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]