[PATCH 14/25] virJSONValueNewArrayFromBitmap: Refactor cleanup

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


Use g_autoptr for the JSON value objects and remove the cleanup label
and inline freeing of objects.

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

diff --git a/src/util/virjson.c b/src/util/virjson.c
index adf1cfbcbc..e4d71d3e09 100644
--- a/src/util/virjson.c
+++ b/src/util/virjson.c
@@ -1241,29 +1241,21 @@ virJSONValueGetArrayAsBitmap(const virJSONValue *val,
 virJSONValuePtr
 virJSONValueNewArrayFromBitmap(virBitmapPtr bitmap)
 {
-    virJSONValuePtr ret;
+    g_autoptr(virJSONValue) ret = virJSONValueNewArray();
     ssize_t pos = -1;

-    ret = virJSONValueNewArray();
-
     if (!bitmap)
-        return ret;
+        return g_steal_pointer(&ret);

     while ((pos = virBitmapNextSetBit(bitmap, pos)) > -1) {
-        virJSONValuePtr newelem;
+        g_autoptr(virJSONValue) newelem = virJSONValueNewNumberLong(pos);

-        if (!(newelem = virJSONValueNewNumberLong(pos)) ||
-            virJSONValueArrayAppend(ret, newelem) < 0) {
-            virJSONValueFree(newelem);
-            goto error;
-        }
+        if (virJSONValueArrayAppend(ret, newelem) < 0)
+            return NULL;
+        newelem = NULL;
     }

-    return ret;
-
- error:
-    virJSONValueFree(ret);
-    return NULL;
+    return g_steal_pointer(&ret);
 }


-- 
2.29.2




More information about the libvir-list mailing list