]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virJSONValueArrayAppend: Clear pointer when taking ownership of passed value
authorPeter Krempa <pkrempa@redhat.com>
Thu, 11 Feb 2021 16:57:45 +0000 (17:57 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Sat, 20 Feb 2021 12:26:37 +0000 (13:26 +0100)
The parent array takes ownership of the inserted value once all checks
pass. Don't make the callers second-guess when that happens and modify
the function to take a double pointer so that it can be cleared once the
ownership is taken.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
19 files changed:
src/locking/lock_daemon.c
src/logging/log_handler.c
src/network/leaseshelper.c
src/node_device/node_device_driver.c
src/qemu/qemu_agent.c
src/qemu/qemu_block.c
src/qemu/qemu_command.c
src/qemu/qemu_firmware.c
src/qemu/qemu_migration_cookie.c
src/qemu/qemu_migration_params.c
src/qemu/qemu_monitor_json.c
src/rpc/virnetserver.c
src/rpc/virnetserverservice.c
src/util/virjson.c
src/util/virjson.h
src/util/virlease.c
src/util/virlockspace.c
src/util/virmacmap.c
tests/testutilsqemuschema.c

index 26905a462b0ea8c1d5e96f5b570765061a1caca3..d68c61b099c4a3e3f4c6eb46a951dd22cacbc5d1 100644 (file)
@@ -728,9 +728,8 @@ virLockDaemonPreExecRestart(const char *state_file,
         if (!(child = virLockSpacePreExecRestart(lockspace)))
             return -1;
 
-        if (virJSONValueArrayAppend(lockspaces, child) < 0)
+        if (virJSONValueArrayAppend(lockspaces, &child) < 0)
             return -1;
-        child = NULL;
 
         tmp++;
     }
index d649c4d132aa955d3c211452133c0b32bbc081bd..9e027c757917a3fb329ca793b546f438c9df3451 100644 (file)
@@ -642,9 +642,8 @@ virLogHandlerPreExecRestart(virLogHandlerPtr handler)
             return NULL;
         }
 
-        if (virJSONValueArrayAppend(files, file) < 0)
+        if (virJSONValueArrayAppend(files, &file) < 0)
             return NULL;
-        file = NULL;
     }
 
     if (virJSONValueObjectAppend(ret, "files", &files) < 0)
index c20e63efa9339608a706df0f94a699e1e1cdd2a1..ca2c640672679b5db9932b2c9f6119785a37b651 100644 (file)
@@ -226,12 +226,11 @@ main(int argc, char **argv)
 
     case VIR_LEASE_ACTION_OLD:
     case VIR_LEASE_ACTION_ADD:
-        if (lease_new && virJSONValueArrayAppend(leases_array_new, lease_new) < 0) {
+        if (lease_new && virJSONValueArrayAppend(leases_array_new, &lease_new) < 0) {
             virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("failed to create json"));
             goto cleanup;
         }
-        lease_new = NULL;
 
         G_GNUC_FALLTHROUGH;
     case VIR_LEASE_ACTION_DEL:
index d946e64ad6a0457b9ac60adf844e59e58d3c3d5b..543e5bb90a07c27e1479325cbe4bdebae2f84dc7 100644 (file)
@@ -606,7 +606,7 @@ nodeDeviceDefToMdevctlConfig(virNodeDeviceDefPtr def, char **buf)
             if (virJSONValueObjectAppendString(jsonattr, attr->name, attr->value) < 0)
                 return -1;
 
-            if (virJSONValueArrayAppend(attributes, g_steal_pointer(&jsonattr)) < 0)
+            if (virJSONValueArrayAppend(attributes, &jsonattr) < 0)
                 return -1;
         }
 
index d6816ef9dede546c603a9cbe2067b6a204b4e6eb..9aec0fdb4b94193cfc098cb18467a8fe30b0e599 100644 (file)
@@ -1180,9 +1180,8 @@ qemuAgentMakeStringsArray(const char **strings, unsigned int len)
     for (i = 0; i < len; i++) {
         g_autoptr(virJSONValue) str = virJSONValueNewString(strings[i]);
 
-        if (virJSONValueArrayAppend(ret, str) < 0)
+        if (virJSONValueArrayAppend(ret, &str) < 0)
             return NULL;
-        str = NULL;
     }
 
     return g_steal_pointer(&ret);
@@ -1514,10 +1513,8 @@ qemuAgentSetVCPUsCommand(qemuAgentPtr agent,
         if (virJSONValueObjectAppendBoolean(cpu, "online", in->online) < 0)
             goto cleanup;
 
-        if (virJSONValueArrayAppend(cpus, cpu) < 0)
+        if (virJSONValueArrayAppend(cpus, &cpu) < 0)
             goto cleanup;
-
-        cpu = NULL;
     }
 
     if (*nmodified == 0) {
index 3262d2bfa903cec0c2ff4dfaa4476880eafc6559..0000ebbd07332951bc38784bf4c2620479be7bbc 100644 (file)
@@ -529,10 +529,8 @@ qemuBlockStorageSourceBuildHostsJSONSocketAddress(virStorageSourcePtr src,
         if (!(server = qemuBlockStorageSourceBuildJSONSocketAddress(host, legacy)))
               return NULL;
 
-        if (virJSONValueArrayAppend(servers, server) < 0)
+        if (virJSONValueArrayAppend(servers, &server) < 0)
             return NULL;
-
-        server = NULL;
     }
 
     return g_steal_pointer(&servers);
@@ -617,10 +615,8 @@ qemuBlockStorageSourceBuildHostsJSONInetSocketAddress(virStorageSourcePtr src)
         if (!(server = qemuBlockStorageSourceBuildJSONInetSocketAddress(host)))
             return NULL;
 
-        if (virJSONValueArrayAppend(servers, server) < 0)
+        if (virJSONValueArrayAppend(servers, &server) < 0)
             return NULL;
-
-        server = NULL;
     }
 
     return g_steal_pointer(&servers);
@@ -907,16 +903,12 @@ qemuBlockStorageSourceGetRBDProps(virStorageSourcePtr src,
         authmodes = virJSONValueNewArray();
 
         if (!(mode = virJSONValueNewString("cephx")) ||
-            virJSONValueArrayAppend(authmodes, mode) < 0)
+            virJSONValueArrayAppend(authmodes, &mode) < 0)
             return NULL;
 
-        mode = NULL;
-
         if (!(mode = virJSONValueNewString("none")) ||
-            virJSONValueArrayAppend(authmodes, mode) < 0)
+            virJSONValueArrayAppend(authmodes, &mode) < 0)
             return NULL;
-
-        mode = NULL;
     }
 
     if (virJSONValueObjectCreate(&ret,
index d801018aa24c7bb20cc729f371a24bb60ad90a84..f255b0f881b6cffa6898ed1e6c2b8b4630581f07 100644 (file)
@@ -10218,9 +10218,8 @@ qemuBuildChannelGuestfwdNetdevProps(virDomainChrDefPtr chr)
                                              chr->info.alias) < 0)
         return NULL;
 
-    if (virJSONValueArrayAppend(guestfwdarr, guestfwdstrobj) < 0)
+    if (virJSONValueArrayAppend(guestfwdarr, &guestfwdstrobj) < 0)
         return NULL;
-    guestfwdstrobj = NULL;
 
     if (virJSONValueObjectCreate(&ret,
                                  "s:type", "user",
index 5e8fdd0ff15097fe501abb832cb4a3fffece8032..d3198e2d45cf97ecd3e8f1e05b75804c0c2be52a 100644 (file)
@@ -812,10 +812,8 @@ qemuFirmwareTargetFormat(virJSONValuePtr doc,
         if (virJSONValueObjectAppend(target, "machines", &machines) < 0)
             return -1;
 
-        if (virJSONValueArrayAppend(targetsJSON, target) < 0)
+        if (virJSONValueArrayAppend(targetsJSON, &target) < 0)
             return -1;
-
-        target = NULL;
     }
 
     if (virJSONValueObjectAppend(doc, "targets", &targetsJSON) < 0)
index 186fe7bc9e88f5bac66d5958fb26f00842b98f7b..07a97b75ff63421d714984ed9607f2059392b9bb 100644 (file)
@@ -1665,10 +1665,9 @@ qemuMigrationCookieBlockDirtyBitmapsToParams(GSList *disks,
                                          NULL) < 0)
                 return -1;
 
-            if (virJSONValueArrayAppend(jsonbitmaps, jsonbitmap) < 0)
+            if (virJSONValueArrayAppend(jsonbitmaps, &jsonbitmap) < 0)
                 return -1;
 
-            jsonbitmap = NULL;
             hasBitmaps = true;
         }
 
@@ -1682,10 +1681,9 @@ qemuMigrationCookieBlockDirtyBitmapsToParams(GSList *disks,
                                      NULL) < 0)
             return -1;
 
-        if (virJSONValueArrayAppend(map, jsondisk) < 0)
+        if (virJSONValueArrayAppend(map, &jsondisk) < 0)
             return -1;
 
-        jsondisk = NULL;
         hasDisks = true;
     }
 
index 8197450456689e90e6b062084ed2a59f4866aa1b..593eb93ffc32ee9da958a5cf56f27ed8816031ee 100644 (file)
@@ -797,10 +797,8 @@ qemuMigrationCapsToJSON(virBitmapPtr caps,
                                      NULL) < 0)
             return NULL;
 
-        if (virJSONValueArrayAppend(json, cap) < 0)
+        if (virJSONValueArrayAppend(json, &cap) < 0)
             return NULL;
-
-        cap = NULL;
     }
 
     return g_steal_pointer(&json);
index 9aaa730b72d56c1a10aee437061217e07a3bacf2..72af979d1ad7db280f851ceac57b54066a82e91c 100644 (file)
@@ -525,10 +525,9 @@ qemuMonitorJSONTransactionAdd(virJSONValuePtr actions,
                                  "A:data", &data, NULL) < 0)
         goto cleanup;
 
-    if (virJSONValueArrayAppend(actions, entry) < 0)
+    if (virJSONValueArrayAppend(actions, &entry) < 0)
         goto cleanup;
 
-    entry = NULL;
     ret = 0;
 
  cleanup:
@@ -4987,11 +4986,8 @@ int qemuMonitorJSONSendKey(qemuMonitorPtr mon,
         if (virJSONValueObjectAppendNumberInt(key, "data", keycodes[i]) < 0)
             goto cleanup;
 
-        if (virJSONValueArrayAppend(keys, key) < 0)
+        if (virJSONValueArrayAppend(keys, &key) < 0)
             goto cleanup;
-
-        key = NULL;
-
     }
 
     cmd = qemuMonitorJSONMakeCommand("send-key",
@@ -9324,10 +9320,9 @@ qemuMonitorJSONTransactionBitmapMergeSourceAddBitmap(virJSONValuePtr sources,
                                  NULL) < 0)
         return -1;
 
-    if (virJSONValueArrayAppend(sources, sourceobj) < 0)
+    if (virJSONValueArrayAppend(sources, &sourceobj) < 0)
         return -1;
 
-    sourceobj = NULL;
     return 0;
 }
 
index c2650ade09f999a4a642a7821046bcd1f70680aa..30faa0190b55288f587bc9c339ea3f7fe37990b7 100644 (file)
@@ -585,9 +585,8 @@ virJSONValuePtr virNetServerPreExecRestart(virNetServerPtr srv)
         if (!(child = virNetServerServicePreExecRestart(srv->services[i])))
             goto error;
 
-        if (virJSONValueArrayAppend(services, child) < 0)
+        if (virJSONValueArrayAppend(services, &child) < 0)
             goto error;
-        child = NULL;
     }
 
     if (virJSONValueObjectAppend(object, "services", &services) < 0)
@@ -598,9 +597,8 @@ virJSONValuePtr virNetServerPreExecRestart(virNetServerPtr srv)
         if (!(child = virNetServerClientPreExecRestart(srv->clients[i])))
             goto error;
 
-        if (virJSONValueArrayAppend(clients, child) < 0)
+        if (virJSONValueArrayAppend(clients, &child) < 0)
             goto error;
-        child = NULL;
     }
 
     if (virJSONValueObjectAppend(object, "clients", &clients) < 0)
index fece6305e87e57c068c5cebd9965be8ae353894d..ce2dd4f28ba06aa2d1d3f9de0f6ce06fe6505be2 100644 (file)
@@ -352,9 +352,8 @@ virJSONValuePtr virNetServerServicePreExecRestart(virNetServerServicePtr svc)
         if (!(child = virNetSocketPreExecRestart(svc->socks[i])))
             return NULL;
 
-        if (virJSONValueArrayAppend(socks, child) < 0)
+        if (virJSONValueArrayAppend(socks, &child) < 0)
             return NULL;
-        child = NULL;
     }
 
     if (virJSONValueObjectAppend(object, "socks", &socks) < 0)
index 5b3aa49a7f7dc8b7ae73267c99da5c38b2ad0501..29202173b62aa20b1f9c17c536d786cb96262bb4 100644 (file)
@@ -298,8 +298,7 @@ virJSONValueObjectAddVArgs(virJSONValuePtr obj,
                 return -1;
             }
 
-            if ((rc = virJSONValueObjectAppend(obj, key, val)) == 0)
-                *val = NULL;
+            rc = virJSONValueObjectAppend(obj, key, val);
         }   break;
 
         case 'M':
@@ -774,7 +773,7 @@ virJSONValueObjectAppendNull(virJSONValuePtr object,
 
 int
 virJSONValueArrayAppend(virJSONValuePtr array,
-                        virJSONValuePtr value)
+                        virJSONValuePtr *value)
 {
     if (array->type != VIR_JSON_TYPE_ARRAY) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("expecting JSON array"));
@@ -785,7 +784,7 @@ virJSONValueArrayAppend(virJSONValuePtr array,
                       array->data.array.nvalues + 1) < 0)
         return -1;
 
-    array->data.array.values[array->data.array.nvalues] = value;
+    array->data.array.values[array->data.array.nvalues] = g_steal_pointer(value);
     array->data.array.nvalues++;
 
     return 0;
@@ -798,9 +797,8 @@ virJSONValueArrayAppendString(virJSONValuePtr object,
 {
     g_autoptr(virJSONValue) jvalue = virJSONValueNewString(value);
 
-    if (virJSONValueArrayAppend(object, jvalue) < 0)
+    if (virJSONValueArrayAppend(object, &jvalue) < 0)
         return -1;
-    jvalue = NULL;
 
     return 0;
 }
@@ -1243,9 +1241,8 @@ virJSONValueNewArrayFromBitmap(virBitmapPtr bitmap)
     while ((pos = virBitmapNextSetBit(bitmap, pos)) > -1) {
         g_autoptr(virJSONValue) newelem = virJSONValueNewNumberLong(pos);
 
-        if (virJSONValueArrayAppend(ret, newelem) < 0)
+        if (virJSONValueArrayAppend(ret, &newelem) < 0)
             return NULL;
-        newelem = NULL;
     }
 
     return g_steal_pointer(&ret);
@@ -1588,7 +1585,7 @@ virJSONParserInsertValue(virJSONParserPtr parser,
             }
 
             if (virJSONValueArrayAppend(state->value,
-                                        value) < 0)
+                                        &value) < 0)
                 return -1;
         }   break;
 
@@ -2088,8 +2085,6 @@ virJSONValueObjectDeflattenWorker(const char *key,
         if (virJSONValueObjectAppend(retobj, key, &newval) < 0)
             return -1;
 
-        newval = NULL;
-
         return 0;
     }
 
index ca99ae3b70fbd89f125fe92fe8c69f5ef246f6a3..c22bc1fb45e14ba0edcb09be6a6aef9be4ee0697 100644 (file)
@@ -72,7 +72,8 @@ virJSONValuePtr virJSONValueNewArrayFromBitmap(virBitmapPtr bitmap);
 int virJSONValueObjectAppend(virJSONValuePtr object,
                              const char *key,
                              virJSONValuePtr *value);
-int virJSONValueArrayAppend(virJSONValuePtr object, virJSONValuePtr value);
+int virJSONValueArrayAppend(virJSONValuePtr object,
+                            virJSONValuePtr *value);
 int virJSONValueArrayConcat(virJSONValuePtr a,
                             virJSONValuePtr c);
 
index 3d68bb660cc13784ba11822d427d42d429d08fd9..59eaabd4d9a190e2f2c08fdb5fadb84b6208e9bd 100644 (file)
@@ -119,7 +119,7 @@ virLeaseReadCustomLeaseFile(virJSONValuePtr leases_array_new,
         }
 
         /* Move old lease to new array */
-        if (virJSONValueArrayAppend(leases_array_new, lease_tmp) < 0) {
+        if (virJSONValueArrayAppend(leases_array_new, &lease_tmp) < 0) {
             virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("failed to create json"));
             return -1;
index 9ba6e83251d54d40a7a3f137e97ea30587bc9645..684a3320ed4ca08f8875cc390bdfc00dbae04f52 100644 (file)
@@ -455,17 +455,15 @@ virJSONValuePtr virLockSpacePreExecRestart(virLockSpacePtr lockspace)
             if (!owner)
                 goto error;
 
-            if (virJSONValueArrayAppend(owners, owner) < 0)
+            if (virJSONValueArrayAppend(owners, &owner) < 0)
                 goto error;
-            owner = NULL;
         }
 
         if (virJSONValueObjectAppend(child, "owners", &owners) < 0)
             goto error;
 
-        if (virJSONValueArrayAppend(resources, child) < 0)
+        if (virJSONValueArrayAppend(resources, &child) < 0)
             goto error;
-        child = NULL;
 
         tmp++;
     }
index 12df32593370732d033988096e47ad58d49c5f1b..bb14513c301cecafab0941c1913062ba648e5d10 100644 (file)
@@ -216,18 +216,16 @@ virMACMapHashDumper(void *payload,
     for (next = macs; next; next = next->next) {
         g_autoptr(virJSONValue) m = virJSONValueNewString((const char *) next->data);
 
-        if (virJSONValueArrayAppend(arr, m) < 0)
+        if (virJSONValueArrayAppend(arr, &m) < 0)
             return -1;
-        m = NULL;
     }
 
     if (virJSONValueObjectAppendString(obj, "domain", name) < 0 ||
         virJSONValueObjectAppend(obj, "macs", &arr) < 0)
         return -1;
 
-    if (virJSONValueArrayAppend(data, obj) < 0)
+    if (virJSONValueArrayAppend(data, &obj) < 0)
         return -1;
-    obj = NULL;
 
     return 0;
 }
index 4bb303a427528de3fc259050661661e67049c255..a842a219e2ee5effc69a8132940bcd32b9bace6c 100644 (file)
@@ -168,9 +168,8 @@ testQEMUSchemaValidateObjectMergeVariantMember(size_t pos G_GNUC_UNUSED,
     if (!(copy = virJSONValueCopy(item)))
         return -1;
 
-    if (virJSONValueArrayAppend(array, copy) < 0)
+    if (virJSONValueArrayAppend(array, &copy) < 0)
         return -1;
-    copy = NULL;
 
     return 1;
 }