From nobody Mon Feb 9 06:48:43 2026 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, "