From nobody Mon Feb 9 09:08:29 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.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 170.10.133.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=1669990652; cv=none; d=zohomail.com; s=zohoarc; b=KdSvrSGcXqujzBJiUCPWbn36sHyzp9GCc7OvsELMnfuAZwcI+htoa9GMddanuQrmWQ33yrqKGCC68+L3qyTyhBeqwuLLKpiF4FTWN2v45/Jy0JnVp0BtxaO3/KXiBLnkEk4Lo62jm28jNOx/D/Y6IqFuBOkQqgiXKt7U51AtzZs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1669990652; 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=3kpkE9ytUQGrYt0H5odc/PdM1KWS9K5VoDnKqB0zLSg=; b=KfiNfkycCBCG/YEgW5Itocu722KOhQ6Nd7nspNoxBVrVjWL97oBiZtTbcXrFdyttkSV+urYL0HSOAoyi3/3xVw37hrCHES+TBWqybn9UFBxtlPiCLNRucnTNlCRG69FkNfJFdR0F5BAzu0AceeiVswYc8y7DPzSicK8YaLfBFak= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1669990652440259.36902550827995; Fri, 2 Dec 2022 06:17:32 -0800 (PST) Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-450-wGU9ZFgdNWC0x5jIjWUTXA-1; Fri, 02 Dec 2022 09:17:26 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 589F22A59578; Fri, 2 Dec 2022 14:17:23 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 442F9C16922; Fri, 2 Dec 2022 14:17:23 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 644491946A41; Fri, 2 Dec 2022 14:17:22 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 7AB7B19465B8 for ; Fri, 2 Dec 2022 14:17:21 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 7018C40E9788; Fri, 2 Dec 2022 14:17:21 +0000 (UTC) Received: from speedmetal.redhat.com (ovpn-208-20.brq.redhat.com [10.40.208.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id A1F0740E9787 for ; Fri, 2 Dec 2022 14:17:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669990651; 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=3kpkE9ytUQGrYt0H5odc/PdM1KWS9K5VoDnKqB0zLSg=; b=BAkycxLkLkfNltZEGOXFofasCTro8Hyn5pbMSG0TrAoaVZjaP6iyKikOFE6LtXCnL1j1Wc U9Z7FqcKWIFix3bewnpSme3CYkXlyGzysdgnMYr9bbL+OFvPEz+j6zltljVqyHV0BobveW jEDPJvddtKwswVUMihz2Qhrf5F+1/Mg= X-MC-Unique: wGU9ZFgdNWC0x5jIjWUTXA-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 13/13] virshFindDisk: Sanitize use of 'tmp' variable Date: Fri, 2 Dec 2022 15:16:57 +0100 Message-Id: <1656aaa12bfe50101e234bb364ef7c54d4e507c6.1669990545.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1669990654559100001 Content-Type: text/plain; charset="utf-8" The return value of virXMLPropString was assigned into 'tmp' multiple times and to prevent static analyzers moaning about a potential leak a short-circuited if+logic or was used. Replace the code by having a helper variable for each possibility and also replace the for-loop to iterate elements. Signed-off-by: Peter Krempa Reviewed-by: J=C3=A1n Tomko --- tools/virsh-domain.c | 48 +++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 8bd058a33a..4d8690d9db 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -12642,7 +12642,6 @@ virshFindDisk(const char *doc, g_autoptr(xmlXPathContext) ctxt =3D NULL; g_autofree xmlNodePtr *nodes =3D NULL; ssize_t nnodes; - xmlNodePtr cur =3D NULL; size_t i; xml =3D virXMLParseStringCtxt(doc, _("(domain_definition)"), &ctxt); @@ -12658,6 +12657,13 @@ virshFindDisk(const char *doc, /* search disk using @path */ for (i =3D 0; i < nnodes; i++) { + xmlNodePtr sourceNode; + g_autofree char *sourceFile =3D NULL; + g_autofree char *sourceDev =3D NULL; + g_autofree char *sourceDir =3D NULL; + g_autofree char *sourceName =3D NULL; + g_autofree char *targetDev =3D NULL; + if (type =3D=3D VIRSH_FIND_DISK_CHANGEABLE) { g_autofree char *device =3D virXMLPropString(nodes[i], "device= "); @@ -12668,29 +12674,25 @@ virshFindDisk(const char *doc, continue; } - cur =3D nodes[i]->children; - while (cur !=3D NULL) { - if (cur->type =3D=3D XML_ELEMENT_NODE) { - g_autofree char *tmp =3D NULL; - - if (virXMLNodeNameEqual(cur, "source")) { - if ((tmp =3D virXMLPropString(cur, "file")) || - (tmp =3D virXMLPropString(cur, "dev")) || - (tmp =3D virXMLPropString(cur, "dir")) || - (tmp =3D virXMLPropString(cur, "name"))) { - } - } else if (virXMLNodeNameEqual(cur, "target")) { - tmp =3D virXMLPropString(cur, "dev"); - } + if ((sourceNode =3D virXMLNodeGetSubelement(nodes[i], "source"))) { + sourceFile =3D virXMLPropString(sourceNode, "file"); + sourceDev =3D virXMLPropString(sourceNode, "dev"); + sourceDir =3D virXMLPropString(sourceNode, "dir"); + sourceName =3D virXMLPropString(sourceNode, "name"); + } - if (STREQ_NULLABLE(tmp, path)) { - xmlNodePtr ret =3D xmlCopyNode(nodes[i], 1); - /* drop backing store since they are not needed here */ - virshDiskDropBackingStore(ret); - return ret; - } - } - cur =3D cur->next; + ctxt->node =3D nodes[i]; + targetDev =3D virXPathString("string(./target/@dev)", ctxt); + + if (STREQ_NULLABLE(targetDev, path) || + STREQ_NULLABLE(sourceFile, path) || + STREQ_NULLABLE(sourceDev, path) || + STREQ_NULLABLE(sourceDir, path) || + STREQ_NULLABLE(sourceName, path)) { + xmlNodePtr ret =3D xmlCopyNode(nodes[i], 1); + /* drop backing store since they are not needed here */ + virshDiskDropBackingStore(ret); + return ret; } } --=20 2.38.1