]> xenbits.xensource.com Git - libvirt.git/commitdiff
virJSONValueNewArrayFromBitmap: Refactor cleanup
authorPeter Krempa <pkrempa@redhat.com>
Fri, 12 Feb 2021 09:55:56 +0000 (10:55 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Sat, 20 Feb 2021 12:26:37 +0000 (13:26 +0100)
Use g_autoptr for the JSON value objects and remove the cleanup label
and inline freeing of objects.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
src/util/virjson.c

index adf1cfbcbcda3f6ae13a24a943604cd261002637..e4d71d3e0934176fb7a811c7e1a0f0b180b012b1 100644 (file)
@@ -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);
 }