From nobody Tue Feb 10 02:50:06 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) client-ip=63.128.21.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1613152691; cv=none; d=zohomail.com; s=zohoarc; b=FuISJITXZv7A9GF1K0glGlgu2Z2ws2jn+uvS3Sk8ymoURifQAPKA+W9kykYiGzQDO6LeW3ITTW7Pwa+19zkgDPm96Xh8yL8PA0JTBqNTg3O5+9AcD2z+jpEu7QrZdAGTmHufMrqNLUK2FHDRpoqqlIMd9QKESPoAKb8GZaV/1+Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1613152691; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=0gLacWkBDvsAvULNGYNfOIYhy13n5xEPQvxCjzHEUqw=; b=kX2dn5Cb5xUSJUguvEqTh1uzwUmI1a/HkqIsr7PiWI+TaR3VZZzQJT213EIPr1yWAdQxG42qRtFrPCXrIDg1VBJQBslAMFKXJ/DMnecLZH6GecJ9kLO8zMltdx7AA2zyPdP4azXT3MYzm9ge2qy8/I0y7GbgssZJY9gH4aSQteo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by mx.zohomail.com with SMTPS id 1613152690730783.7463571761234; Fri, 12 Feb 2021 09:58:10 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-293-BFQLcbexNnO65mFW_0etRg-1; Fri, 12 Feb 2021 12:57:26 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 48BCF10513CC; Fri, 12 Feb 2021 17:56:48 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 252315D6AB; Fri, 12 Feb 2021 17:56:48 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id DC45618095CC; Fri, 12 Feb 2021 17:56:47 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 11CHuj0e010985 for ; Fri, 12 Feb 2021 12:56:45 -0500 Received: by smtp.corp.redhat.com (Postfix) id 774275C3E0; Fri, 12 Feb 2021 17:56:45 +0000 (UTC) Received: from speedmetal.lan (unknown [10.40.208.53]) by smtp.corp.redhat.com (Postfix) with ESMTP id 79ED05C23D for ; Fri, 12 Feb 2021 17:56:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1613152686; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=0gLacWkBDvsAvULNGYNfOIYhy13n5xEPQvxCjzHEUqw=; b=HWZKFu59LHBKR3WRZ2fYiymYRoaImB2vv17h7NBrnPPY4muRqrEoZuaLBQZeqX7jQ/rLaw kZQ3qHzcIFTF9Z+IHraYsM/NGHMYPFcFFVSyNpQy3vPRdTHvA/0gwPawZgY8ZYg4ULuq0n nUOAHlyoCXhEQ/M33R4V/05oftRRdoM= X-MC-Unique: BFQLcbexNnO65mFW_0etRg-1 From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 20/25] virJSONValueArrayAppend: Clear pointer when taking ownership of passed value Date: Fri, 12 Feb 2021 18:55:33 +0100 Message-Id: <92aac862492454e10ea3506b0d307bdcc6199b07.1613152441.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" 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 --- 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_params.c | 4 +--- src/qemu/qemu_monitor_json.c | 11 +++-------- src/rpc/virnetserver.c | 6 ++---- src/rpc/virnetserverservice.c | 3 +-- src/util/virjson.c | 12 +++++------- src/util/virjson.h | 3 ++- src/util/virlease.c | 2 +- src/util/virlockspace.c | 6 ++---- src/util/virmacmap.c | 6 ++---- tests/testutilsqemuschema.c | 3 +-- 18 files changed, 32 insertions(+), 65 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 =3D virLockSpacePreExecRestart(lockspace))) return -1; - if (virJSONValueArrayAppend(lockspaces, child) < 0) + if (virJSONValueArrayAppend(lockspaces, &child) < 0) return -1; - child =3D 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 =3D 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_n= ew) < 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 =3D NULL; G_GNUC_FALLTHROUGH; case VIR_LEASE_ACTION_DEL: diff --git a/src/node_device/node_device_driver.c b/src/node_device/node_de= vice_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, c= har **buf) if (virJSONValueObjectAppendString(jsonattr, attr->name, attr-= >value) < 0) return -1; - if (virJSONValueArrayAppend(attributes, g_steal_pointer(&jsona= ttr)) < 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, unsig= ned int len) for (i =3D 0; i < len; i++) { g_autoptr(virJSONValue) str =3D virJSONValueNewString(strings[i]); - if (virJSONValueArrayAppend(ret, str) < 0) + if (virJSONValueArrayAppend(ret, &str) < 0) return NULL; - str =3D 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 =3D NULL; } if (*nmodified =3D=3D 0) { diff --git a/src/qemu/qemu_block.c b/src/qemu/qemu_block.c index 1df51ba97b..94cbb1d12e 100644 --- a/src/qemu/qemu_block.c +++ b/src/qemu/qemu_block.c @@ -534,10 +534,8 @@ qemuBlockStorageSourceBuildHostsJSONSocketAddress(virS= torageSourcePtr src, if (!(server =3D qemuBlockStorageSourceBuildJSONSocketAddress(host= , legacy))) return NULL; - if (virJSONValueArrayAppend(servers, server) < 0) + if (virJSONValueArrayAppend(servers, &server) < 0) return NULL; - - server =3D NULL; } return g_steal_pointer(&servers); @@ -622,10 +620,8 @@ qemuBlockStorageSourceBuildHostsJSONInetSocketAddress(= virStorageSourcePtr src) if (!(server =3D qemuBlockStorageSourceBuildJSONInetSocketAddress(= host))) return NULL; - if (virJSONValueArrayAppend(servers, server) < 0) + if (virJSONValueArrayAppend(servers, &server) < 0) return NULL; - - server =3D NULL; } return g_steal_pointer(&servers); @@ -912,16 +908,12 @@ qemuBlockStorageSourceGetRBDProps(virStorageSourcePtr= src, authmodes =3D virJSONValueNewArray(); if (!(mode =3D virJSONValueNewString("cephx")) || - virJSONValueArrayAppend(authmodes, mode) < 0) + virJSONValueArrayAppend(authmodes, &mode) < 0) return NULL; - mode =3D NULL; - if (!(mode =3D virJSONValueNewString("none")) || - virJSONValueArrayAppend(authmodes, mode) < 0) + virJSONValueArrayAppend(authmodes, &mode) < 0) return NULL; - - mode =3D NULL; } if (virJSONValueObjectCreate(&ret, diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index d27d5eb55b..3037d1d5a5 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -10200,9 +10200,8 @@ qemuBuildChannelGuestfwdNetdevProps(virDomainChrDef= Ptr chr) chr->info.alias) < 0) return NULL; - if (virJSONValueArrayAppend(guestfwdarr, guestfwdstrobj) < 0) + if (virJSONValueArrayAppend(guestfwdarr, &guestfwdstrobj) < 0) return NULL; - guestfwdstrobj =3D 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 =3D NULL; } if (virJSONValueObjectAppend(doc, "targets", &targetsJSON) < 0) diff --git a/src/qemu/qemu_migration_params.c b/src/qemu/qemu_migration_par= ams.c index 510dad783a..302a9933e1 100644 --- a/src/qemu/qemu_migration_params.c +++ b/src/qemu/qemu_migration_params.c @@ -770,10 +770,8 @@ qemuMigrationCapsToJSON(virBitmapPtr caps, NULL) < 0) return NULL; - if (virJSONValueArrayAppend(json, cap) < 0) + if (virJSONValueArrayAppend(json, &cap) < 0) return NULL; - - cap =3D NULL; } return g_steal_pointer(&json); diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c index 924e03b4da..94365c775b 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 =3D NULL; ret =3D 0; cleanup: @@ -4976,11 +4975,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 =3D NULL; - } cmd =3D qemuMonitorJSONMakeCommand("send-key", @@ -9289,10 +9285,9 @@ qemuMonitorJSONTransactionBitmapMergeSourceAddBitmap= (virJSONValuePtr sources, NULL) < 0) return -1; - if (virJSONValueArrayAppend(sources, sourceobj) < 0) + if (virJSONValueArrayAppend(sources, &sourceobj) < 0) return -1; - sourceobj =3D 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(virNetServer= Ptr srv) if (!(child =3D virNetServerServicePreExecRestart(srv->services[i]= ))) goto error; - if (virJSONValueArrayAppend(services, child) < 0) + if (virJSONValueArrayAppend(services, &child) < 0) goto error; - child =3D NULL; } if (virJSONValueObjectAppend(object, "services", &services) < 0) @@ -598,9 +597,8 @@ virJSONValuePtr virNetServerPreExecRestart(virNetServer= Ptr srv) if (!(child =3D virNetServerClientPreExecRestart(srv->clients[i]))) goto error; - if (virJSONValueArrayAppend(clients, child) < 0) + if (virJSONValueArrayAppend(clients, &child) < 0) goto error; - child =3D 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(virNe= tServerServicePtr svc) if (!(child =3D virNetSocketPreExecRestart(svc->socks[i]))) return NULL; - if (virJSONValueArrayAppend(socks, child) < 0) + if (virJSONValueArrayAppend(socks, &child) < 0) return NULL; - child =3D NULL; } if (virJSONValueObjectAppend(object, "socks", &socks) < 0) diff --git a/src/util/virjson.c b/src/util/virjson.c index 5b3aa49a7f..65215f03c5 100644 --- a/src/util/virjson.c +++ b/src/util/virjson.c @@ -774,7 +774,7 @@ virJSONValueObjectAppendNull(virJSONValuePtr object, int virJSONValueArrayAppend(virJSONValuePtr array, - virJSONValuePtr value) + virJSONValuePtr *value) { if (array->type !=3D VIR_JSON_TYPE_ARRAY) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("expecting JSON arr= ay")); @@ -785,7 +785,7 @@ virJSONValueArrayAppend(virJSONValuePtr array, array->data.array.nvalues + 1) < 0) return -1; - array->data.array.values[array->data.array.nvalues] =3D value; + array->data.array.values[array->data.array.nvalues] =3D g_steal_pointe= r(value); array->data.array.nvalues++; return 0; @@ -798,9 +798,8 @@ virJSONValueArrayAppendString(virJSONValuePtr object, { g_autoptr(virJSONValue) jvalue =3D virJSONValueNewString(value); - if (virJSONValueArrayAppend(object, jvalue) < 0) + if (virJSONValueArrayAppend(object, &jvalue) < 0) return -1; - jvalue =3D NULL; return 0; } @@ -1243,9 +1242,8 @@ virJSONValueNewArrayFromBitmap(virBitmapPtr bitmap) while ((pos =3D virBitmapNextSetBit(bitmap, pos)) > -1) { g_autoptr(virJSONValue) newelem =3D virJSONValueNewNumberLong(pos); - if (virJSONValueArrayAppend(ret, newelem) < 0) + if (virJSONValueArrayAppend(ret, &newelem) < 0) return NULL; - newelem =3D NULL; } return g_steal_pointer(&ret); @@ -1588,7 +1586,7 @@ virJSONParserInsertValue(virJSONParserPtr parser, } if (virJSONValueArrayAppend(state->value, - value) < 0) + &value) < 0) return -1; } break; 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(virBitmapP= tr 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_arra= y_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(virLockSpa= cePtr lockspace) if (!owner) goto error; - if (virJSONValueArrayAppend(owners, owner) < 0) + if (virJSONValueArrayAppend(owners, &owner) < 0) goto error; - owner =3D NULL; } if (virJSONValueObjectAppend(child, "owners", &owners) < 0) goto error; - if (virJSONValueArrayAppend(resources, child) < 0) + if (virJSONValueArrayAppend(resources, &child) < 0) goto error; - child =3D 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 =3D macs; next; next =3D next->next) { g_autoptr(virJSONValue) m =3D virJSONValueNewString((const char *)= next->data); - if (virJSONValueArrayAppend(arr, m) < 0) + if (virJSONValueArrayAppend(arr, &m) < 0) return -1; - m =3D 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 =3D 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 p= os G_GNUC_UNUSED, if (!(copy =3D virJSONValueCopy(item))) return -1; - if (virJSONValueArrayAppend(array, copy) < 0) + if (virJSONValueArrayAppend(array, ©) < 0) return -1; - copy =3D NULL; return 1; } --=20 2.29.2