From nobody Tue May 7 17:40:33 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 1542386260649763.0343860758227; Fri, 16 Nov 2018 08:37:40 -0800 (PST) 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 mx1.redhat.com (Postfix) with ESMTPS id 1274658E35; Fri, 16 Nov 2018 16:37:38 +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 137B85D6B3; Fri, 16 Nov 2018 16:37:37 +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 9EB8F181B9E4; Fri, 16 Nov 2018 16:37:35 +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 wAGGLbJD015883 for ; Fri, 16 Nov 2018 11:21:37 -0500 Received: by smtp.corp.redhat.com (Postfix) id E950317574; Fri, 16 Nov 2018 16:21:37 +0000 (UTC) Received: from kinshicho.brq.redhat.com (unknown [10.43.2.63]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6E9A75C6A5 for ; Fri, 16 Nov 2018 16:21:37 +0000 (UTC) From: Andrea Bolognani To: libvir-list@redhat.com Date: Fri, 16 Nov 2018 17:21:29 +0100 Message-Id: <20181116162131.25744-2-abologna@redhat.com> In-Reply-To: <20181116162131.25744-1-abologna@redhat.com> References: <20181116162131.25744-1-abologna@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 1/3] conf: Fix error flow in virDomainPCIAddressEnsureAddr() 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.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Fri, 16 Nov 2018 16:37:38 +0000 (UTC) Content-Type: text/plain; charset="utf-8" This avoids setting 'ret' multiple times, which will result in errors being masked if the first operation fails but the second one succeeds. Introduced-by: f183b87fc1dbcc6446ac3c1cef9cdd345b9725fb Spotted-by: Coverity Reported-by: John Ferlan Signed-off-by: Andrea Bolognani Reviewed-by: Pavel Hrdina --- src/conf/domain_addr.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/conf/domain_addr.c b/src/conf/domain_addr.c index 3e1d767e4f..cc9ea82a33 100644 --- a/src/conf/domain_addr.c +++ b/src/conf/domain_addr.c @@ -940,15 +940,21 @@ virDomainPCIAddressEnsureAddr(virDomainPCIAddressSetP= tr addrs, addrStr, flags, true)) goto cleanup; =20 - ret =3D virDomainPCIAddressReserveAddrInternal(addrs, &dev->addr.p= ci, - flags, dev->isolation= Group, - true); + if (virDomainPCIAddressReserveAddrInternal(addrs, &dev->addr.pci, + flags, dev->isolationGr= oup, + true) < 0) { + goto cleanup; + } } else { - ret =3D virDomainPCIAddressReserveNextAddr(addrs, dev, flags, -1); + if (virDomainPCIAddressReserveNextAddr(addrs, dev, flags, -1) < 0) + goto cleanup; } =20 dev->addr.pci.extFlags =3D dev->pciAddrExtFlags; - ret =3D virDomainPCIAddressExtensionEnsureAddr(addrs, &dev->addr.pci); + if (virDomainPCIAddressExtensionEnsureAddr(addrs, &dev->addr.pci) < 0) + goto cleanup; + + ret =3D 0; =20 cleanup: VIR_FREE(addrStr); --=20 2.19.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Tue May 7 17:40:33 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 1542387167434961.2417289990651; Fri, 16 Nov 2018 08:52:47 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3449930820CE; Fri, 16 Nov 2018 16:52:45 +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 EBB7B5DA9A; Fri, 16 Nov 2018 16:52:44 +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 8EE394EA39; Fri, 16 Nov 2018 16:52:44 +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 wAGGLc68015893 for ; Fri, 16 Nov 2018 11:21:38 -0500 Received: by smtp.corp.redhat.com (Postfix) id E78B35C6A5; Fri, 16 Nov 2018 16:21:38 +0000 (UTC) Received: from kinshicho.brq.redhat.com (unknown [10.43.2.63]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 43CC617574 for ; Fri, 16 Nov 2018 16:21:38 +0000 (UTC) From: Andrea Bolognani To: libvir-list@redhat.com Date: Fri, 16 Nov 2018 17:21:30 +0100 Message-Id: <20181116162131.25744-3-abologna@redhat.com> In-Reply-To: <20181116162131.25744-1-abologna@redhat.com> References: <20181116162131.25744-1-abologna@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 2/3] conf: Add several cleanup paths 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.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.47]); Fri, 16 Nov 2018 16:52:46 +0000 (UTC) Content-Type: text/plain; charset="utf-8" In many cases, an early exit from a function would cause memory allocated by local virBuffer instances not to be released. Provide proper cleanup paths to solve the issue. Signed-off-by: Andrea Bolognani Reviewed-by: Pavel Hrdina --- src/conf/domain_conf.c | 137 ++++++++++++++++++++++++++++++----------- 1 file changed, 100 insertions(+), 37 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index c3dbba6919..1d04cac104 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -24353,13 +24353,14 @@ virDomainControllerDefFormat(virBufferPtr buf, const char *model =3D NULL; const char *modelName =3D NULL; virBuffer childBuf =3D VIR_BUFFER_INITIALIZER; + int ret =3D -1; =20 virBufferSetChildIndent(&childBuf, buf); =20 if (!type) { virReportError(VIR_ERR_INTERNAL_ERROR, _("unexpected controller type %d"), def->type); - return -1; + goto cleanup; } =20 if (def->model !=3D -1) { @@ -24368,7 +24369,7 @@ virDomainControllerDefFormat(virBufferPtr buf, if (!model) { virReportError(VIR_ERR_INTERNAL_ERROR, _("unexpected model type %d"), def->model); - return -1; + goto cleanup; } } =20 @@ -24432,7 +24433,7 @@ virDomainControllerDefFormat(virBufferPtr buf, virReportError(VIR_ERR_INTERNAL_ERROR, _("unexpected model name value %d"), def->opts.pciopts.modelName); - return -1; + goto cleanup; } virBufferAsprintf(&childBuf, "\n", modelNa= me); } @@ -24483,7 +24484,7 @@ virDomainControllerDefFormat(virBufferPtr buf, } =20 if (virBufferCheckError(&childBuf) < 0) - return -1; + goto cleanup; =20 if (virBufferUse(&childBuf)) { virBufferAddLit(buf, ">\n"); @@ -24493,6 +24494,11 @@ virDomainControllerDefFormat(virBufferPtr buf, virBufferAddLit(buf, "/>\n"); } =20 + ret =3D 0; + + cleanup: + virBufferFreeAndReset(&childBuf); + return 0; } =20 @@ -24523,17 +24529,18 @@ virDomainFSDefFormat(virBufferPtr buf, const char *wrpolicy =3D virDomainFSWrpolicyTypeToString(def->wrpolicy= ); const char *src =3D def->src->path; virBuffer driverBuf =3D VIR_BUFFER_INITIALIZER; + int ret =3D -1; =20 if (!type) { virReportError(VIR_ERR_INTERNAL_ERROR, _("unexpected filesystem type %d"), def->type); - return -1; + goto cleanup; } =20 if (!accessmode) { virReportError(VIR_ERR_INTERNAL_ERROR, _("unexpected accessmode %d"), def->accessmode); - return -1; + goto cleanup; } =20 =20 @@ -24557,7 +24564,7 @@ virDomainFSDefFormat(virBufferPtr buf, virDomainVirtioOptionsFormat(&driverBuf, def->virtio); =20 if (virBufferCheckError(&driverBuf) < 0) - return -1; + goto cleanup; =20 if (virBufferUse(&driverBuf)) { virBufferAddLit(buf, "\n"); - return 0; + + ret =3D 0; + + cleanup: + virBufferFreeAndReset(&driverBuf); + + return ret; } =20 =20 @@ -25847,13 +25860,14 @@ virDomainSoundDefFormat(virBufferPtr buf, const char *model =3D virDomainSoundModelTypeToString(def->model); virBuffer childBuf =3D VIR_BUFFER_INITIALIZER; size_t i; + int ret =3D -1; =20 virBufferSetChildIndent(&childBuf, buf); =20 if (!model) { virReportError(VIR_ERR_INTERNAL_ERROR, _("unexpected sound model %d"), def->model); - return -1; + goto cleanup; } =20 for (i =3D 0; i < def->ncodecs; i++) @@ -25862,7 +25876,7 @@ virDomainSoundDefFormat(virBufferPtr buf, virDomainDeviceInfoFormat(&childBuf, &def->info, flags); =20 if (virBufferCheckError(&childBuf) < 0) - return -1; + goto cleanup; =20 virBufferAsprintf(buf, "\n"); } =20 + ret =3D 0; + + cleanup: + virBufferFreeAndReset(&childBuf); + return 0; } =20 @@ -25884,11 +25903,12 @@ virDomainMemballoonDefFormat(virBufferPtr buf, { const char *model =3D virDomainMemballoonModelTypeToString(def->model); virBuffer childrenBuf =3D VIR_BUFFER_INITIALIZER; + int ret =3D -1; =20 if (!model) { virReportError(VIR_ERR_INTERNAL_ERROR, _("unexpected memballoon model %d"), def->model); - return -1; + goto cleanup; } =20 virBufferAsprintf(buf, "virtio); =20 - if (virBufferCheckError(&driverBuf) < 0) { - virBufferFreeAndReset(&childrenBuf); - return -1; - } + if (virBufferCheckError(&driverBuf) < 0) + goto cleanup; + if (virBufferUse(&driverBuf)) { virBufferAddLit(&childrenBuf, "\n"); @@ -25931,6 +25950,11 @@ virDomainMemballoonDefFormat(virBufferPtr buf, virBufferAddLit(buf, "\n"); } =20 + ret =3D 0; + + cleanup: + virBufferFreeAndReset(&childrenBuf); + return 0; } =20 @@ -25958,25 +25982,26 @@ virDomainWatchdogDefFormat(virBufferPtr buf, const char *model =3D virDomainWatchdogModelTypeToString(def->model); const char *action =3D virDomainWatchdogActionTypeToString(def->action= ); virBuffer childBuf =3D VIR_BUFFER_INITIALIZER; + int ret =3D -1; =20 virBufferSetChildIndent(&childBuf, buf); =20 if (!model) { virReportError(VIR_ERR_INTERNAL_ERROR, _("unexpected watchdog model %d"), def->model); - return -1; + goto cleanup; } =20 if (!action) { virReportError(VIR_ERR_INTERNAL_ERROR, _("unexpected watchdog action %d"), def->action); - return -1; + goto cleanup; } =20 virDomainDeviceInfoFormat(&childBuf, &def->info, flags); =20 if (virBufferCheckError(&childBuf) < 0) - return -1; + goto cleanup; =20 virBufferAsprintf(buf, "\n"); } =20 - return 0; + ret =3D 0; + + cleanup: + virBufferFreeAndReset(&childBuf); + + return ret; } =20 static int virDomainPanicDefFormat(virBufferPtr buf, virDomainPanicDefPtr def) { virBuffer childrenBuf =3D VIR_BUFFER_INITIALIZER; + int ret =3D -1; =20 virBufferAddLit(buf, "info, 0); =20 if (virBufferCheckError(&childrenBuf) < 0) - return -1; + goto cleanup; =20 if (virBufferUse(&childrenBuf)) { virBufferAddLit(buf, ">\n"); @@ -26016,8 +26047,13 @@ static int virDomainPanicDefFormat(virBufferPtr bu= f, } else { virBufferAddLit(buf, "/>\n"); } + + ret =3D 0; + + cleanup: virBufferFreeAndReset(&childrenBuf); - return 0; + + return ret; } =20 static int @@ -26067,6 +26103,7 @@ virDomainRNGDefFormat(virBufferPtr buf, const char *model =3D virDomainRNGModelTypeToString(def->model); const char *backend =3D virDomainRNGBackendTypeToString(def->backend); virBuffer driverBuf =3D VIR_BUFFER_INITIALIZER; + int ret =3D -1; =20 virBufferAsprintf(buf, "\n", model); virBufferAdjustIndent(buf, 2); @@ -26085,11 +26122,11 @@ virDomainRNGDefFormat(virBufferPtr buf, =20 case VIR_DOMAIN_RNG_BACKEND_EGD: if (virDomainChrAttrsDefFormat(buf, def->source.chardev, false) < = 0) - return -1; + goto cleanup; virBufferAddLit(buf, ">\n"); virBufferAdjustIndent(buf, 2); if (virDomainChrSourceDefFormat(buf, def->source.chardev, flags) <= 0) - return -1; + goto cleanup; virBufferAdjustIndent(buf, -2); virBufferAddLit(buf, "\n"); =20 @@ -26099,7 +26136,7 @@ virDomainRNGDefFormat(virBufferPtr buf, =20 virDomainVirtioOptionsFormat(&driverBuf, def->virtio); if (virBufferCheckError(&driverBuf) < 0) - return -1; + goto cleanup; =20 if (virBufferUse(&driverBuf)) { virBufferAddLit(buf, "\n"); - return 0; + + ret =3D 0; + + cleanup: + virBufferFreeAndReset(&driverBuf); + + return ret; } =20 void @@ -26258,18 +26301,19 @@ virDomainVideoDefFormat(virBufferPtr buf, { const char *model =3D virDomainVideoTypeToString(def->type); virBuffer driverBuf =3D VIR_BUFFER_INITIALIZER; + int ret =3D -1; =20 if (!model) { virReportError(VIR_ERR_INTERNAL_ERROR, _("unexpected video model %d"), def->type); - return -1; + goto cleanup; } =20 virBufferAddLit(buf, "\n"); @@ -26271,7 +26297,8 @@ virDomainMemoryDefFormat(virBufferPtr buf, =20 virDomainMemoryTargetDefFormat(buf, def); =20 - virDomainDeviceInfoFormat(buf, &def->info, flags); + if (virDomainDeviceInfoFormat(buf, &def->info, flags) < 0) + return -1; =20 virBufferAdjustIndent(buf, -2); virBufferAddLit(buf, "\n"); @@ -26348,7 +26375,8 @@ virDomainVideoDefFormat(virBufferPtr buf, virBufferAddLit(buf, "/>\n"); } =20 - virDomainDeviceInfoFormat(buf, &def->info, flags); + if (virDomainDeviceInfoFormat(buf, &def->info, flags) < 0) + goto cleanup; =20 virBufferAdjustIndent(buf, -2); virBufferAddLit(buf, "\n"); @@ -26404,7 +26432,8 @@ virDomainInputDefFormat(virBufferPtr buf, virBufferAddLit(&childbuf, "/>\n"); } virBufferEscapeString(&childbuf, "\n", def->sour= ce.evdev); - virDomainDeviceInfoFormat(&childbuf, &def->info, flags); + if (virDomainDeviceInfoFormat(&childbuf, &def->info, flags) < 0) + goto cleanup; =20 if (virBufferCheckError(&childbuf) < 0) goto cleanup; @@ -27004,9 +27033,11 @@ virDomainHostdevDefFormat(virBufferPtr buf, if (def->shareable) virBufferAddLit(buf, "\n"); =20 - virDomainDeviceInfoFormat(buf, def->info, - flags | VIR_DOMAIN_DEF_FORMAT_ALLOW_BOOT - | VIR_DOMAIN_DEF_FORMAT_ALLOW_ROM); + if (virDomainDeviceInfoFormat(buf, def->info, + flags | VIR_DOMAIN_DEF_FORMAT_ALLOW_BOOT + | VIR_DOMAIN_DEF_FORMAT_ALLOW_ROM) < 0) { + return -1; + } =20 virBufferAdjustIndent(buf, -2); virBufferAddLit(buf, "\n"); @@ -27032,8 +27063,10 @@ virDomainRedirdevDefFormat(virBufferPtr buf, if (virDomainChrSourceDefFormat(buf, def->source, flags) < 0) return -1; =20 - virDomainDeviceInfoFormat(buf, &def->info, - flags | VIR_DOMAIN_DEF_FORMAT_ALLOW_BOOT); + if (virDomainDeviceInfoFormat(buf, &def->info, + flags | VIR_DOMAIN_DEF_FORMAT_ALLOW_BOOT= ) < 0) { + return -1; + } virBufferAdjustIndent(buf, -2); virBufferAddLit(buf, "\n"); return 0; @@ -27095,7 +27128,8 @@ virDomainHubDefFormat(virBufferPtr buf, goto cleanup; } =20 - virDomainDeviceInfoFormat(&childBuf, &def->info, flags); + if (virDomainDeviceInfoFormat(&childBuf, &def->info, flags) < 0) + goto cleanup; =20 if (virBufferCheckError(&childBuf) < 0) goto cleanup; @@ -27816,7 +27850,8 @@ virDomainVsockDefFormat(virBufferPtr buf, if (virXMLFormatElement(&childBuf, "cid", &cidAttrBuf, NULL) < 0) goto cleanup; =20 - virDomainDeviceInfoFormat(&childBuf, &vsock->info, 0); + if (virDomainDeviceInfoFormat(&childBuf, &vsock->info, 0) < 0) + goto cleanup; =20 if (virXMLFormatElement(buf, "vsock", &attrBuf, &childBuf) < 0) goto cleanup; --=20 2.19.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list