From 6431b20c3ef2fdf31bcce0a04439c2dc3d2a61bc Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Thu, 11 Feb 2021 17:57:45 +0100 Subject: [PATCH] virJSONValueArrayAppend: Clear pointer when taking ownership of passed value 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 Reviewed-by: Michal Privoznik --- src/locking/lock_daemon.c | 3 +-- src/logging/log_handler.c | 3 +-- src/network/leaseshelper.c | 3 +-- src/node_device/node_device_driver.c | 2 +- src/qemu/qemu_agent.c | 7 ++----- src/qemu/qemu_block.c | 16 ++++------------ src/qemu/qemu_command.c | 3 +-- src/qemu/qemu_firmware.c | 4 +--- src/qemu/qemu_migration_cookie.c | 6 ++---- src/qemu/qemu_migration_params.c | 4 +--- src/qemu/qemu_monitor_json.c | 11 +++-------- src/rpc/virnetserver.c | 6 ++---- src/rpc/virnetserverservice.c | 3 +-- src/util/virjson.c | 17 ++++++----------- src/util/virjson.h | 3 ++- src/util/virlease.c | 2 +- src/util/virlockspace.c | 6 ++---- src/util/virmacmap.c | 6 ++---- tests/testutilsqemuschema.c | 3 +-- 19 files changed, 35 insertions(+), 73 deletions(-) diff --git a/src/locking/lock_daemon.c b/src/locking/lock_daemon.c index 26905a462b..d68c61b099 100644 --- a/src/locking/lock_daemon.c +++ b/src/locking/lock_daemon.c @@ -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++; } diff --git a/src/logging/log_handler.c b/src/logging/log_handler.c index d649c4d132..9e027c7579 100644 --- a/src/logging/log_handler.c +++ b/src/logging/log_handler.c @@ -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) diff --git a/src/network/leaseshelper.c b/src/network/leaseshelper.c index c20e63efa9..ca2c640672 100644 --- a/src/network/leaseshelper.c +++ b/src/network/leaseshelper.c @@ -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: diff --git a/src/node_device/node_device_driver.c b/src/node_device/node_device_driver.c index d946e64ad6..543e5bb90a 100644 --- a/src/node_device/node_device_driver.c +++ b/src/node_device/node_device_driver.c @@ -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; } diff --git a/src/qemu/qemu_agent.c b/src/qemu/qemu_agent.c index d6816ef9de..9aec0fdb4b 100644 --- a/src/qemu/qemu_agent.c +++ b/src/qemu/qemu_agent.c @@ -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) { diff --git a/src/qemu/qemu_block.c b/src/qemu/qemu_block.c index 3262d2bfa9..0000ebbd07 100644 --- a/src/qemu/qemu_block.c +++ b/src/qemu/qemu_block.c @@ -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, diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index d801018aa2..f255b0f881 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -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", diff --git a/src/qemu/qemu_firmware.c b/src/qemu/qemu_firmware.c index 5e8fdd0ff1..d3198e2d45 100644 --- a/src/qemu/qemu_firmware.c +++ b/src/qemu/qemu_firmware.c @@ -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) diff --git a/src/qemu/qemu_migration_cookie.c b/src/qemu/qemu_migration_cookie.c index 186fe7bc9e..07a97b75ff 100644 --- a/src/qemu/qemu_migration_cookie.c +++ b/src/qemu/qemu_migration_cookie.c @@ -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; } diff --git a/src/qemu/qemu_migration_params.c b/src/qemu/qemu_migration_params.c index 8197450456..593eb93ffc 100644 --- a/src/qemu/qemu_migration_params.c +++ b/src/qemu/qemu_migration_params.c @@ -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); diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c index 9aaa730b72..72af979d1a 100644 --- a/src/qemu/qemu_monitor_json.c +++ b/src/qemu/qemu_monitor_json.c @@ -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; } diff --git a/src/rpc/virnetserver.c b/src/rpc/virnetserver.c index c2650ade09..30faa0190b 100644 --- a/src/rpc/virnetserver.c +++ b/src/rpc/virnetserver.c @@ -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) diff --git a/src/rpc/virnetserverservice.c b/src/rpc/virnetserverservice.c index fece6305e8..ce2dd4f28b 100644 --- a/src/rpc/virnetserverservice.c +++ b/src/rpc/virnetserverservice.c @@ -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) diff --git a/src/util/virjson.c b/src/util/virjson.c index 5b3aa49a7f..29202173b6 100644 --- a/src/util/virjson.c +++ b/src/util/virjson.c @@ -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; } diff --git a/src/util/virjson.h b/src/util/virjson.h index ca99ae3b70..c22bc1fb45 100644 --- a/src/util/virjson.h +++ b/src/util/virjson.h @@ -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); diff --git a/src/util/virlease.c b/src/util/virlease.c index 3d68bb660c..59eaabd4d9 100644 --- a/src/util/virlease.c +++ b/src/util/virlease.c @@ -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; diff --git a/src/util/virlockspace.c b/src/util/virlockspace.c index 9ba6e83251..684a3320ed 100644 --- a/src/util/virlockspace.c +++ b/src/util/virlockspace.c @@ -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++; } diff --git a/src/util/virmacmap.c b/src/util/virmacmap.c index 12df325933..bb14513c30 100644 --- a/src/util/virmacmap.c +++ b/src/util/virmacmap.c @@ -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; } diff --git a/tests/testutilsqemuschema.c b/tests/testutilsqemuschema.c index 4bb303a427..a842a219e2 100644 --- a/tests/testutilsqemuschema.c +++ b/tests/testutilsqemuschema.c @@ -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, ©) < 0) return -1; - copy = NULL; return 1; }