[PATCH 11/25] virJSONValueObjectInsert: Clear @value on successful insertion

Peter Krempa pkrempa at redhat.com
Fri Feb 12 17:55:24 UTC 2021


The function takes ownership of @value on success so the proper
semantics will be to clear out the @value pointer. Convert @value to a
double pointer to do this.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/util/virjson.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/src/util/virjson.c b/src/util/virjson.c
index a509943fde..80ebcb587c 100644
--- a/src/util/virjson.c
+++ b/src/util/virjson.c
@@ -577,10 +577,10 @@ virJSONValueNewObject(void)
 static int
 virJSONValueObjectInsert(virJSONValuePtr object,
                          const char *key,
-                         virJSONValuePtr value,
+                         virJSONValuePtr *value,
                          bool prepend)
 {
-    virJSONObjectPair pair = { NULL, value };
+    virJSONObjectPair pair = { NULL, *value };
     int ret = -1;

     if (object->type != VIR_JSON_TYPE_OBJECT) {
@@ -604,6 +604,9 @@ virJSONValueObjectInsert(virJSONValuePtr object,
                                  object->data.object.npairs, pair);
     }

+    if (ret == 0)
+        *value = NULL;
+
     VIR_FREE(pair.key);
     return ret;
 }
@@ -614,7 +617,7 @@ virJSONValueObjectAppend(virJSONValuePtr object,
                          const char *key,
                          virJSONValuePtr value)
 {
-    return virJSONValueObjectInsert(object, key, value, false);
+    return virJSONValueObjectInsert(object, key, &value, false);
 }


@@ -627,10 +630,8 @@ virJSONValueObjectInsertString(virJSONValuePtr object,
     virJSONValuePtr jvalue = virJSONValueNewString(value);
     if (!jvalue)
         return -1;
-    if (virJSONValueObjectInsert(object, key, jvalue, prepend) < 0) {
-        virJSONValueFree(jvalue);
+    if (virJSONValueObjectInsert(object, key, &jvalue, prepend) < 0)
         return -1;
-    }
     return 0;
 }

-- 
2.29.2




More information about the libvir-list mailing list