From nobody Wed Nov 27 17:43:49 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1544016030797218.70047143911415; Wed, 5 Dec 2018 05:20:30 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 681293154847; Wed, 5 Dec 2018 13:20:28 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1C0D651C8D; Wed, 5 Dec 2018 13:20:28 +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 C308D3F7D5; Wed, 5 Dec 2018 13:20:27 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id wB5DKQSB027554 for ; Wed, 5 Dec 2018 08:20:26 -0500 Received: by smtp.corp.redhat.com (Postfix) id 61F6F5D982; Wed, 5 Dec 2018 13:20:26 +0000 (UTC) Received: from angien.brq.redhat.com (unknown [10.43.2.229]) by smtp.corp.redhat.com (Postfix) with ESMTP id B38465D965; Wed, 5 Dec 2018 13:20:25 +0000 (UTC) From: Peter Krempa To: libvir-list@redhat.com Date: Wed, 5 Dec 2018 14:20:13 +0100 Message-Id: <7384b05b5b8ed81c08e33c180404fd5db64f5f96.1544015957.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Cc: Peter Krempa Subject: [libvirt] [PATCH 2/5] util: xml: Always consume args of virXMLFormatElement 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: , Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.41]); Wed, 05 Dec 2018 13:20:29 +0000 (UTC) Content-Type: text/plain; charset="utf-8" The function clears and frees the passed buffers on success, but not in one case of failure. Modify the control flow that the args are always consumed, record it in the docs and remove few pointless cleanup paths in callers. Signed-off-by: Peter Krempa --- src/conf/domain_conf.c | 12 ++---------- src/qemu/qemu_domain.c | 5 +---- src/util/virxml.c | 16 ++++++++++++---- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 0c40a98f6b..b70dca6c61 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -24042,7 +24042,6 @@ virDomainDiskDefFormatIotune(virBufferPtr buf, virDomainDiskDefPtr disk) { virBuffer childBuf =3D VIR_BUFFER_INITIALIZER; - int ret =3D -1; virBufferSetChildIndent(&childBuf, buf); @@ -24077,10 +24076,7 @@ virDomainDiskDefFormatIotune(virBufferPtr buf, FORMAT_IOTUNE(read_iops_sec_max_length); FORMAT_IOTUNE(write_iops_sec_max_length); - ret =3D virXMLFormatElement(buf, "iotune", NULL, &childBuf); - - virBufferFreeAndReset(&childBuf); - return ret; + return virXMLFormatElement(buf, "iotune", NULL, &childBuf); } #undef FORMAT_IOTUNE @@ -24091,7 +24087,6 @@ virDomainDiskDefFormatDriver(virBufferPtr buf, virDomainDiskDefPtr disk) { virBuffer driverBuf =3D VIR_BUFFER_INITIALIZER; - int ret =3D -1; virBufferEscapeString(&driverBuf, " name=3D'%s'", virDomainDiskGetDriv= er(disk)); @@ -24143,10 +24138,7 @@ virDomainDiskDefFormatDriver(virBufferPtr buf, virDomainVirtioOptionsFormat(&driverBuf, disk->virtio); - ret =3D virXMLFormatElement(buf, "driver", &driverBuf, NULL); - - virBufferFreeAndReset(&driverBuf); - return ret; + return virXMLFormatElement(buf, "driver", &driverBuf, NULL); } diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index dd17174e25..77814a0c2a 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -2234,14 +2234,11 @@ qemuDomainObjPrivateXMLFormatBlockjobs(virBufferPtr= buf, { virBuffer attrBuf =3D VIR_BUFFER_INITIALIZER; bool bj =3D qemuDomainHasBlockjob(vm, false); - int ret; virBufferAsprintf(&attrBuf, " active=3D'%s'", virTristateBoolTypeToString(virTristateBoolFromBool(= bj))); - ret =3D virXMLFormatElement(buf, "blockjobs", &attrBuf, NULL); - virBufferFreeAndReset(&attrBuf); - return ret; + return virXMLFormatElement(buf, "blockjobs", &attrBuf, NULL); } diff --git a/src/util/virxml.c b/src/util/virxml.c index 998d974882..3ed44e9036 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -1359,6 +1359,8 @@ virXMLValidatorFree(virXMLValidatorPtr validator) * @childBuf are NULL or are empty buffers the element is not * formatted. * + * Both passed buffers are always consumed and freed. + * * Returns 0 on success, -1 on error. */ int @@ -1367,15 +1369,16 @@ virXMLFormatElement(virBufferPtr buf, virBufferPtr attrBuf, virBufferPtr childBuf) { + int ret =3D -1; + if ((!attrBuf || virBufferUse(attrBuf) =3D=3D 0) && (!childBuf || virBufferUse(childBuf) =3D=3D 0)) { return 0; } if ((attrBuf && virBufferCheckError(attrBuf) < 0) || - (childBuf && virBufferCheckError(childBuf) < 0)) { - return -1; - } + (childBuf && virBufferCheckError(childBuf) < 0)) + goto cleanup; virBufferAsprintf(buf, "<%s", name); @@ -1390,5 +1393,10 @@ virXMLFormatElement(virBufferPtr buf, virBufferAddLit(buf, "/>\n"); } - return 0; + ret =3D 0; + + cleanup: + virBufferFreeAndReset(attrBuf); + virBufferFreeAndReset(childBuf); + return ret; } --=20 2.19.2 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list