[json-glib/coverity-leak] builder: Don't leak memory if json_builder_get_root fails sanity check




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

    builder: Don't leak memory if json_builder_get_root fails sanity check
    
    Coverity noticed a leak 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 commit addresses the coverity complaint by using g_autoptr.

 json-glib/json-builder.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
---
diff --git a/json-glib/json-builder.c b/json-glib/json-builder.c
index 45f6cbe..69d27e4 100644
--- a/json-glib/json-builder.c
+++ b/json-glib/json-builder.c
@@ -307,7 +307,7 @@ json_builder_new_immutable (void)
 JsonNode *
 json_builder_get_root (JsonBuilder *builder)
 {
-  JsonNode *root = NULL;
+  g_autoptr(JsonNode) root = NULL;
 
   g_return_val_if_fail (JSON_IS_BUILDER (builder), NULL);
 
@@ -319,7 +319,7 @@ json_builder_get_root (JsonBuilder *builder)
                         root == NULL ||
                         json_node_is_immutable (root), NULL);
 
-  return root;
+  return g_steal_pointer (&root);
 }
 
 /**


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