From nobody Mon May 6 23:48:49 2024 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=1628694202; cv=none; d=zohomail.com; s=zohoarc; b=TcwTQSHn/tXCnf7kLIcCNOP1GTtMRjpmco8DZsH3i4U4H45/35BOkqLUc1OxXlpIp0PLuaB9T/xkhF2OK1nMXbuzDvu0q4+6rVLZCtEou/WXzPUqY1lOoMptw2fAiDZ+wHNHvVUOsnGUP/s6CyRcAWrymxQehrig3k64/6B/pxM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1628694202; h=Content-Type:Content-Transfer-Encoding:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=JX/J1oLp7JQVQch1ZDMPwMG4BQE5hTBXFSzkqQEjc7g=; b=WaEJ+XVUggmSZ8bE7PfDoSufjCNzTLKqCNpa12C2XRewsEFmLoYz3WytuQqinenFF8iOqg1Ns83HGUL7d6mPvZv4JY3DO5zJkUPyYj2LqchUnJCUyy0QrHUzcJn2PpMdxttfyDyGzzicf4eqNZimnl6aqPi0Z3y26tEtVFZ6SsI= 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 162869420200872.01676172519376; Wed, 11 Aug 2021 08:03:22 -0700 (PDT) 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-430-dZ7KZuWhPwS9k0iQZQdoSA-1; Wed, 11 Aug 2021 11:03:18 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 075C31008063; Wed, 11 Aug 2021 15:03:13 +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 C0CA728553; Wed, 11 Aug 2021 15:03:12 +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 85328181A100; Wed, 11 Aug 2021 15:03:12 +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 17BF2w77004992 for ; Wed, 11 Aug 2021 11:02:58 -0400 Received: by smtp.corp.redhat.com (Postfix) id 061205D9C6; Wed, 11 Aug 2021 15:02:58 +0000 (UTC) Received: from hjs.redhat.com (unknown [10.43.2.53]) by smtp.corp.redhat.com (Postfix) with ESMTP id 82AA25D9CA for ; Wed, 11 Aug 2021 15:02:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1628694201; 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:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=JX/J1oLp7JQVQch1ZDMPwMG4BQE5hTBXFSzkqQEjc7g=; b=IHg9FZE0ydH5/AjHHxqD9lOmRYcjNP5v35tHX1clv7n5wvFlxqKE4aAybr5k3nZ9vSbW52 0zOqal4RERQ2ujTBDi38KzoOoT1aAhx2yZCXwh2ekBxIpohSc35Fvwz35hMLxx67iJkS5Q A0Tl/FmvbDgVLFsK8dV1bxUKAmchdzo= X-MC-Unique: dZ7KZuWhPwS9k0iQZQdoSA-1 From: =?UTF-8?q?J=C3=A1n=20Tomko?= To: libvir-list@redhat.com Subject: [libvirt PATCH] virsh: refactor cmdDomblkinfo Date: Wed, 11 Aug 2021 17:02:50 +0200 Message-Id: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 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.84 on 10.5.11.23 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-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1628694219301100001 Use automatic memory cleanup to get rid of the cleanup section, and of the memory leak that happens inside the loop, because cap, alloc and phy are only freed once per function. Signed-off-by: J=C3=A1n Tomko Reviewed-by: Martin Kletzander --- tools/virsh-domain-monitor.c | 62 +++++++++++++++--------------------- 1 file changed, 25 insertions(+), 37 deletions(-) diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c index a2bf5c05fc..55c8dcf118 100644 --- a/tools/virsh-domain-monitor.c +++ b/tools/virsh-domain-monitor.c @@ -452,22 +452,16 @@ static bool cmdDomblkinfo(vshControl *ctl, const vshCmd *cmd) { virDomainBlockInfo info; - virDomainPtr dom; - bool ret =3D false; + g_autoptr(virshDomain) dom =3D NULL; bool human =3D false; bool all =3D false; const char *device =3D NULL; - xmlDocPtr xmldoc =3D NULL; - xmlXPathContextPtr ctxt =3D NULL; + g_autoptr(xmlDoc) xmldoc =3D NULL; + g_autoptr(xmlXPathContext) ctxt =3D NULL; int ndisks; size_t i; - xmlNodePtr *disks =3D NULL; - char *target =3D NULL; - char *protocol =3D NULL; - char *cap =3D NULL; - char *alloc =3D NULL; - char *phy =3D NULL; - vshTable *table =3D NULL; + g_autofree xmlNodePtr *disks =3D NULL; + g_autoptr(vshTable) table =3D NULL; =20 VSH_EXCLUSIVE_OPTIONS("all", "device"); =20 @@ -477,7 +471,7 @@ cmdDomblkinfo(vshControl *ctl, const vshCmd *cmd) all =3D vshCommandOptBool(cmd, "all"); if (!all && vshCommandOptStringQuiet(ctl, cmd, "device", &device) <=3D= 0) { vshError(ctl, "command 'domblkinfo' requires option"); - goto cleanup; + return false; } =20 human =3D vshCommandOptBool(cmd, "human"); @@ -487,18 +481,24 @@ cmdDomblkinfo(vshControl *ctl, const vshCmd *cmd) int rc; =20 if (virshDomainGetXML(ctl, cmd, 0, &xmldoc, &ctxt) < 0) - goto cleanup; + return false; =20 ndisks =3D virXPathNodeSet("./devices/disk", ctxt, &disks); if (ndisks < 0) - goto cleanup; + return false; =20 /* title */ table =3D vshTableNew(_("Target"), _("Capacity"), _("Allocation"),= _("Physical"), NULL); if (!table) - goto cleanup; + return false; =20 for (i =3D 0; i < ndisks; i++) { + g_autofree char *target =3D NULL; + g_autofree char *protocol =3D NULL; + g_autofree char *cap =3D NULL; + g_autofree char *alloc =3D NULL; + g_autofree char *phy =3D NULL; + ctxt->node =3D disks[i]; protocol =3D virXPathString("string(./source/@protocol)", ctxt= ); target =3D virXPathString("string(./target/@dev)", ctxt); @@ -517,7 +517,7 @@ cmdDomblkinfo(vshControl *ctl, const vshCmd *cmd) memset(&info, 0, sizeof(info)); vshResetLibvirtError(); } else { - goto cleanup; + return false; } } } else { @@ -527,41 +527,29 @@ cmdDomblkinfo(vshControl *ctl, const vshCmd *cmd) } =20 if (!cmdDomblkinfoGet(&info, &cap, &alloc, &phy, human)) - goto cleanup; + return false; if (vshTableRowAppend(table, target, cap, alloc, phy, NULL) < = 0) - goto cleanup; - - VIR_FREE(target); - VIR_FREE(protocol); + return false; } =20 vshTablePrintToStdout(table, ctl); =20 } else { + g_autofree char *cap =3D NULL; + g_autofree char *alloc =3D NULL; + g_autofree char *phy =3D NULL; + if (virDomainGetBlockInfo(dom, device, &info, 0) < 0) - goto cleanup; + return false; =20 if (!cmdDomblkinfoGet(&info, &cap, &alloc, &phy, human)) - goto cleanup; + return false; vshPrint(ctl, "%-15s %s\n", _("Capacity:"), cap); vshPrint(ctl, "%-15s %s\n", _("Allocation:"), alloc); vshPrint(ctl, "%-15s %s\n", _("Physical:"), phy); } =20 - ret =3D true; - - cleanup: - vshTableFree(table); - VIR_FREE(cap); - VIR_FREE(alloc); - VIR_FREE(phy); - virshDomainFree(dom); - VIR_FREE(target); - VIR_FREE(protocol); - VIR_FREE(disks); - xmlXPathFreeContext(ctxt); - xmlFreeDoc(xmldoc); - return ret; + return true; } =20 /* --=20 2.31.1