[PATCH 10/19] virJSONValueNewArray: Use g_new0 to allocate and remove NULL checks from callers

Peter Krempa pkrempa at redhat.com
Fri Jan 31 14:31:14 UTC 2020


Use the glib allocation function that never returns NULL and remove the
now dead-code checks from all callers.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/locking/lock_daemon.c        |  4 ++--
 src/logging/log_handler.c        |  3 +--
 src/network/leaseshelper.c       |  6 +-----
 src/qemu/qemu_agent.c            |  6 +-----
 src/qemu/qemu_backup.c           |  6 ++----
 src/qemu/qemu_block.c            |  9 +++------
 src/qemu/qemu_blockjob.c         |  3 +--
 src/qemu/qemu_checkpoint.c       |  9 +++------
 src/qemu/qemu_driver.c           |  3 +--
 src/qemu/qemu_firmware.c         | 12 ++++--------
 src/qemu/qemu_migration_params.c |  3 +--
 src/qemu/qemu_monitor_json.c     |  3 +--
 src/rpc/virnetserver.c           |  6 ++----
 src/rpc/virnetserverservice.c    |  3 +--
 src/util/virjson.c               | 13 ++-----------
 src/util/virlockspace.c          |  6 ++----
 src/util/virmacmap.c             |  8 ++++----
 tests/qemublocktest.c            |  3 +--
 tests/qemumonitorjsontest.c      |  5 ++---
 19 files changed, 35 insertions(+), 76 deletions(-)

diff --git a/src/locking/lock_daemon.c b/src/locking/lock_daemon.c
index 7ea228ce37..fd376fef2a 100644
--- a/src/locking/lock_daemon.c
+++ b/src/locking/lock_daemon.c
@@ -949,8 +949,8 @@ virLockDaemonPreExecRestart(const char *state_file,
         goto cleanup;
     }

-    if (!(lockspaces = virJSONValueNewArray()))
-        goto cleanup;
+    lockspaces = virJSONValueNewArray();
+
     if (virJSONValueObjectAppend(object, "lockspaces", lockspaces) < 0) {
         virJSONValueFree(lockspaces);
         goto cleanup;
diff --git a/src/logging/log_handler.c b/src/logging/log_handler.c
index 030c9d66e3..973c52c7cd 100644
--- a/src/logging/log_handler.c
+++ b/src/logging/log_handler.c
@@ -619,8 +619,7 @@ virLogHandlerPreExecRestart(virLogHandlerPtr handler)
     if (!ret)
         return NULL;

-    if (!(files = virJSONValueNewArray()))
-        goto error;
+    files = virJSONValueNewArray();

     if (virJSONValueObjectAppend(ret, "files", files) < 0) {
         virJSONValueFree(files);
diff --git a/src/network/leaseshelper.c b/src/network/leaseshelper.c
index f1a061066e..dd1d5f70ee 100644
--- a/src/network/leaseshelper.c
+++ b/src/network/leaseshelper.c
@@ -200,11 +200,7 @@ main(int argc, char **argv)
         break;
     }

-    if (!(leases_array_new = virJSONValueNewArray())) {
-        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-                       _("failed to create json"));
-        goto cleanup;
-    }
+    leases_array_new = virJSONValueNewArray();

     if (virLeaseReadCustomLeaseFile(leases_array_new, custom_lease_file,
                                     delete ? ip : NULL, &server_duid) < 0)
diff --git a/src/qemu/qemu_agent.c b/src/qemu/qemu_agent.c
index 4374235d34..7d01d21a11 100644
--- a/src/qemu/qemu_agent.c
+++ b/src/qemu/qemu_agent.c
@@ -1201,9 +1201,6 @@ qemuAgentMakeStringsArray(const char **strings, unsigned int len)
     size_t i;
     virJSONValuePtr ret = virJSONValueNewArray(), str;

-    if (!ret)
-        return NULL;
-
     for (i = 0; i < len; i++) {
         str = virJSONValueNewString(strings[i]);
         if (!str)
@@ -1534,8 +1531,7 @@ qemuAgentSetVCPUsCommand(qemuAgentPtr mon,
     *nmodified = 0;

     /* create the key data array */
-    if (!(cpus = virJSONValueNewArray()))
-        goto cleanup;
+    cpus = virJSONValueNewArray();

     for (i = 0; i < ninfo; i++) {
         qemuAgentCPUInfoPtr in = &info[i];
diff --git a/src/qemu/qemu_backup.c b/src/qemu/qemu_backup.c
index 8b1e9a7e19..2cc6ff7a42 100644
--- a/src/qemu/qemu_backup.c
+++ b/src/qemu/qemu_backup.c
@@ -180,8 +180,7 @@ qemuBackupDiskPrepareOneBitmapsChain(virDomainMomentDefPtr *incremental,
     g_autoptr(virJSONValue) ret = NULL;
     size_t incridx = 0;

-    if (!(ret = virJSONValueNewArray()))
-        return NULL;
+    ret = virJSONValueNewArray();

     if (!(bitmap = qemuBlockNamedNodeDataGetBitmapByName(blockNamedNodeData,
                                                          backingChain,
@@ -819,8 +818,7 @@ qemuBackupBegin(virDomainObjPtr vm,
         !(incremental = qemuBackupBeginCollectIncrementalCheckpoints(vm, def->incremental)))
         goto endjob;

-    if (!(actions = virJSONValueNewArray()))
-        goto endjob;
+    actions = virJSONValueNewArray();

     /* The 'chk' checkpoint must be rolled back if the transaction command
      * which creates it on disk is not executed or fails */
diff --git a/src/qemu/qemu_block.c b/src/qemu/qemu_block.c
index 13e240fdac..03f029368e 100644
--- a/src/qemu/qemu_block.c
+++ b/src/qemu/qemu_block.c
@@ -524,8 +524,7 @@ qemuBlockStorageSourceBuildHostsJSONSocketAddress(virStorageSourcePtr src,
     virStorageNetHostDefPtr host;
     size_t i;

-    if (!(servers = virJSONValueNewArray()))
-        return NULL;
+    servers = virJSONValueNewArray();

     for (i = 0; i < src->nhosts; i++) {
         host = src->hosts + i;
@@ -590,8 +589,7 @@ qemuBlockStorageSourceBuildHostsJSONInetSocketAddress(virStorageSourcePtr src)
     virStorageNetHostDefPtr host;
     size_t i;

-    if (!(servers = virJSONValueNewArray()))
-        return NULL;
+    servers = virJSONValueNewArray();

     for (i = 0; i < src->nhosts; i++) {
         host = src->hosts + i;
@@ -837,8 +835,7 @@ qemuBlockStorageSourceGetRBDProps(virStorageSourcePtr src,
         username = srcPriv->secinfo->s.aes.username;
         keysecret = srcPriv->secinfo->s.aes.alias;
         /* the auth modes are modelled after our old command line generator */
-        if (!(authmodes = virJSONValueNewArray()))
-            return NULL;
+        authmodes = virJSONValueNewArray();

         if (!(mode = virJSONValueNewString("cephx")) ||
             virJSONValueArrayAppend(authmodes, mode) < 0)
diff --git a/src/qemu/qemu_blockjob.c b/src/qemu/qemu_blockjob.c
index e04fcf69d1..3dc9222a6f 100644
--- a/src/qemu/qemu_blockjob.c
+++ b/src/qemu/qemu_blockjob.c
@@ -1344,8 +1344,7 @@ qemuBlockJobProcessEventConcludedBackup(virQEMUDriverPtr driver,
         return;

     if (job->data.backup.bitmap) {
-        if (!(actions = virJSONValueNewArray()))
-            return;
+        actions = virJSONValueNewArray();

         if (qemuMonitorTransactionBitmapRemove(actions,
                                                job->disk->src->nodeformat,
diff --git a/src/qemu/qemu_checkpoint.c b/src/qemu/qemu_checkpoint.c
index 6ef35ea1a1..a60fbcdbac 100644
--- a/src/qemu/qemu_checkpoint.c
+++ b/src/qemu/qemu_checkpoint.c
@@ -217,8 +217,7 @@ qemuCheckpointDiscardDiskBitmaps(virStorageSourcePtr src,
                     return -1;
             }

-            if (!(arr = virJSONValueNewArray()))
-                return -1;
+            arr = virJSONValueNewArray();

             if (qemuMonitorTransactionBitmapMergeSourceAddBitmap(arr,
                                                                  n->nodeformat,
@@ -261,8 +260,7 @@ qemuCheckpointDiscardBitmaps(virDomainObjPtr vm,
     g_autoptr(GSList) relabelimages = NULL;
     GSList *next;

-    if (!(actions = virJSONValueNewArray()))
-        return -1;
+    actions = virJSONValueNewArray();

     qemuDomainObjEnterMonitor(driver, vm);
     blockNamedNodeData = qemuMonitorBlockGetNamedNodeData(priv->mon);
@@ -535,8 +533,7 @@ qemuCheckpointCreateCommon(virQEMUDriverPtr driver,
     if ((parent = virDomainCheckpointGetCurrent(vm->checkpoints)))
         (*def)->parent.parent_name = g_strdup(parent->def->name);

-    if (!(tmpactions = virJSONValueNewArray()))
-        return -1;
+    tmpactions = virJSONValueNewArray();

     if (qemuCheckpointAddActions(vm, tmpactions, parent, *def) < 0)
         return -1;
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 47f0754a1a..021e5a2732 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -15639,8 +15639,7 @@ qemuDomainSnapshotCreateDiskActive(virQEMUDriverPtr driver,
     if (virDomainObjCheckActive(vm) < 0)
         return -1;

-    if (!(actions = virJSONValueNewArray()))
-        return -1;
+    actions = virJSONValueNewArray();

     if (blockdev &&
         !(blockNamedNodeData = qemuBlockGetNamedNodeData(vm, asyncJob)))
diff --git a/src/qemu/qemu_firmware.c b/src/qemu/qemu_firmware.c
index 7fb57913e8..68e2c6b40f 100644
--- a/src/qemu/qemu_firmware.c
+++ b/src/qemu/qemu_firmware.c
@@ -651,8 +651,7 @@ qemuFirmwareInterfaceFormat(virJSONValuePtr doc,
     g_autoptr(virJSONValue) interfacesJSON = NULL;
     size_t i;

-    if (!(interfacesJSON = virJSONValueNewArray()))
-        return -1;
+    interfacesJSON = virJSONValueNewArray();

     for (i = 0; i < fw->ninterfaces; i++) {
         if (virJSONValueArrayAppendString(interfacesJSON,
@@ -799,8 +798,7 @@ qemuFirmwareTargetFormat(virJSONValuePtr doc,
     g_autoptr(virJSONValue) targetsJSON = NULL;
     size_t i;

-    if (!(targetsJSON = virJSONValueNewArray()))
-        return -1;
+    targetsJSON = virJSONValueNewArray();

     for (i = 0; i < fw->ntargets; i++) {
         qemuFirmwareTargetPtr t = fw->targets[i];
@@ -816,8 +814,7 @@ qemuFirmwareTargetFormat(virJSONValuePtr doc,
                                            virQEMUCapsArchToString(t->architecture)) < 0)
             return -1;

-        if (!(machines = virJSONValueNewArray()))
-            return -1;
+        machines = virJSONValueNewArray();

         for (j = 0; j < t->nmachines; j++) {
             if (virJSONValueArrayAppendString(machines,
@@ -851,8 +848,7 @@ qemuFirmwareFeatureFormat(virJSONValuePtr doc,
     g_autoptr(virJSONValue) featuresJSON = NULL;
     size_t i;

-    if (!(featuresJSON = virJSONValueNewArray()))
-        return -1;
+    featuresJSON = virJSONValueNewArray();

     for (i = 0; i < fw->nfeatures; i++) {
         if (virJSONValueArrayAppendString(featuresJSON,
diff --git a/src/qemu/qemu_migration_params.c b/src/qemu/qemu_migration_params.c
index 1b28e5e031..dd1ad9349b 100644
--- a/src/qemu/qemu_migration_params.c
+++ b/src/qemu/qemu_migration_params.c
@@ -785,8 +785,7 @@ qemuMigrationCapsToJSON(virBitmapPtr caps,
     qemuMigrationCapability bit;
     const char *name;

-    if (!(json = virJSONValueNewArray()))
-        return NULL;
+    json = virJSONValueNewArray();

     for (bit = 0; bit < QEMU_MIGRATION_CAP_LAST; bit++) {
         bool supported = false;
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index 981d091ba0..dc1fc310ca 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -4800,8 +4800,7 @@ int qemuMonitorJSONSendKey(qemuMonitorPtr mon,
     size_t i;

     /* create the key data array */
-    if (!(keys = virJSONValueNewArray()))
-        goto cleanup;
+    keys = virJSONValueNewArray();

     for (i = 0; i < nkeycodes; i++) {
         if (keycodes[i] > 0xffff) {
diff --git a/src/rpc/virnetserver.c b/src/rpc/virnetserver.c
index 4122636805..c87dade1a8 100644
--- a/src/rpc/virnetserver.c
+++ b/src/rpc/virnetserver.c
@@ -603,8 +603,7 @@ virJSONValuePtr virNetServerPreExecRestart(virNetServerPtr srv)
         goto error;
     }

-    if (!(services = virJSONValueNewArray()))
-        goto error;
+    services = virJSONValueNewArray();

     if (virJSONValueObjectAppend(object, "services", services) < 0) {
         virJSONValueFree(services);
@@ -622,8 +621,7 @@ virJSONValuePtr virNetServerPreExecRestart(virNetServerPtr srv)
         }
     }

-    if (!(clients = virJSONValueNewArray()))
-        goto error;
+    clients = virJSONValueNewArray();

     if (virJSONValueObjectAppend(object, "clients", clients) < 0) {
         virJSONValueFree(clients);
diff --git a/src/rpc/virnetserverservice.c b/src/rpc/virnetserverservice.c
index 5d1178f899..0a003e5814 100644
--- a/src/rpc/virnetserverservice.c
+++ b/src/rpc/virnetserverservice.c
@@ -353,8 +353,7 @@ virJSONValuePtr virNetServerServicePreExecRestart(virNetServerServicePtr svc)
     if (virJSONValueObjectAppendNumberUint(object, "nrequests_client_max", svc->nrequests_client_max) < 0)
         goto error;

-    if (!(socks = virJSONValueNewArray()))
-        goto error;
+    socks = virJSONValueNewArray();

     if (virJSONValueObjectAppend(object, "socks", socks) < 0) {
         virJSONValueFree(socks);
diff --git a/src/util/virjson.c b/src/util/virjson.c
index 50993648eb..ca57df816f 100644
--- a/src/util/virjson.c
+++ b/src/util/virjson.c
@@ -561,10 +561,7 @@ virJSONValueNewNull(void)
 virJSONValuePtr
 virJSONValueNewArray(void)
 {
-    virJSONValuePtr val;
-
-    if (VIR_ALLOC(val) < 0)
-        return NULL;
+    virJSONValuePtr val = g_new0(virJSONValue, 1);

     val->type = VIR_JSON_TYPE_ARRAY;

@@ -1265,8 +1262,7 @@ virJSONValueNewArrayFromBitmap(virBitmapPtr bitmap)
     virJSONValuePtr ret;
     ssize_t pos = -1;

-    if (!(ret = virJSONValueNewArray()))
-        return NULL;
+    ret = virJSONValueNewArray();

     if (!bitmap)
         return ret;
@@ -1522,8 +1518,6 @@ virJSONValueCopy(const virJSONValue *in)
         break;
     case VIR_JSON_TYPE_ARRAY:
         out = virJSONValueNewArray();
-        if (!out)
-            return NULL;
         for (i = 0; i < in->data.array.nvalues; i++) {
             virJSONValuePtr val = NULL;
             if (!(val = virJSONValueCopy(in->data.array.values[i])))
@@ -1782,9 +1776,6 @@ virJSONParserHandleStartArray(void *ctx)

     VIR_DEBUG("parser=%p", parser);

-    if (!value)
-        return 0;
-
     if (virJSONParserInsertValue(parser, value) < 0) {
         virJSONValueFree(value);
         return 0;
diff --git a/src/util/virlockspace.c b/src/util/virlockspace.c
index 59d47daae8..a44377f89e 100644
--- a/src/util/virlockspace.c
+++ b/src/util/virlockspace.c
@@ -443,8 +443,7 @@ virJSONValuePtr virLockSpacePreExecRestart(virLockSpacePtr lockspace)
         virJSONValueObjectAppendString(object, "directory", lockspace->dir) < 0)
         goto error;

-    if (!(resources = virJSONValueNewArray()))
-        goto error;
+    resources = virJSONValueNewArray();

     if (virJSONValueObjectAppend(object, "resources", resources) < 0) {
         virJSONValueFree(resources);
@@ -479,8 +478,7 @@ virJSONValuePtr virLockSpacePreExecRestart(virLockSpacePtr lockspace)
             goto error;
         }

-        if (!(owners = virJSONValueNewArray()))
-            goto error;
+        owners = virJSONValueNewArray();

         if (virJSONValueObjectAppend(child, "owners", owners) < 0) {
             virJSONValueFree(owners);
diff --git a/src/util/virmacmap.c b/src/util/virmacmap.c
index cd74f67678..ec589334ea 100644
--- a/src/util/virmacmap.c
+++ b/src/util/virmacmap.c
@@ -206,10 +206,11 @@ virMACMapHashDumper(void *payload,
     size_t i;
     int ret = -1;

-    if (!(obj = virJSONValueNewObject()) ||
-        !(arr = virJSONValueNewArray()))
+    if (!(obj = virJSONValueNewObject()))
         goto cleanup;

+    arr = virJSONValueNewArray();
+
     for (i = 0; macs[i]; i++) {
         virJSONValuePtr m = virJSONValueNewString(macs[i]);

@@ -244,8 +245,7 @@ virMacMapDumpStrLocked(virMacMapPtr mgr,
     virJSONValuePtr arr;
     int ret = -1;

-    if (!(arr = virJSONValueNewArray()))
-        goto cleanup;
+    arr = virJSONValueNewArray();

     if (virHashForEach(mgr->macs, virMACMapHashDumper, arr) < 0)
         goto cleanup;
diff --git a/tests/qemublocktest.c b/tests/qemublocktest.c
index 50dcb86810..cd61334b4a 100644
--- a/tests/qemublocktest.c
+++ b/tests/qemublocktest.c
@@ -737,8 +737,7 @@ testQemuCheckpointDeleteMerge(const void *opaque)
         return -1;
     }

-    if (!(actions = virJSONValueNewArray()))
-        return -1;
+    actions = virJSONValueNewArray();

     if (qemuCheckpointDiscardDiskBitmaps(data->chain,
                                          nodedata,
diff --git a/tests/qemumonitorjsontest.c b/tests/qemumonitorjsontest.c
index 0334f83628..2c696a2e8b 100644
--- a/tests/qemumonitorjsontest.c
+++ b/tests/qemumonitorjsontest.c
@@ -2948,9 +2948,8 @@ testQemuMonitorJSONTransaction(const void *opaque)
     if (!(test = qemuMonitorTestNewSchema(data->xmlopt, data->schema)))
         return -1;

-    if (!(actions = virJSONValueNewArray()) ||
-        !(mergebitmaps = virJSONValueNewArray()))
-        return -1;
+    actions = virJSONValueNewArray();
+    mergebitmaps = virJSONValueNewArray();

     if (qemuMonitorTransactionBitmapMergeSourceAddBitmap(mergebitmaps, "node1", "bitmap1") < 0 ||
         qemuMonitorTransactionBitmapMergeSourceAddBitmap(mergebitmaps, "node2", "bitmap2") < 0)
-- 
2.24.1




More information about the libvir-list mailing list