From nobody Tue May 13 11:15:23 2025 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; dkim=fail; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1532639150287760.8288341356193; Thu, 26 Jul 2018 14:05:50 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C108B81115; Thu, 26 Jul 2018 21:05:27 +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 772C93001A69; Thu, 26 Jul 2018 21:05:27 +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 1F00318037FA; Thu, 26 Jul 2018 21:05:27 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w6QL5PYc024882 for ; Thu, 26 Jul 2018 17:05:25 -0400 Received: by smtp.corp.redhat.com (Postfix) id A208C10E4D01; Thu, 26 Jul 2018 21:05:25 +0000 (UTC) Received: from mx1.redhat.com (ext-mx09.extmail.prod.ext.phx2.redhat.com [10.5.110.38]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 85B4110E4D08 for ; Thu, 26 Jul 2018 21:05:19 +0000 (UTC) Received: from mail-pl0-f66.google.com (mail-pl0-f66.google.com [209.85.160.66]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id EEB9632B691 for ; Thu, 26 Jul 2018 21:05:17 +0000 (UTC) Received: by mail-pl0-f66.google.com with SMTP id 31-v6so1345336plc.4 for ; Thu, 26 Jul 2018 14:05:17 -0700 (PDT) Received: from centos.localdomain ([42.106.3.93]) by smtp.gmail.com with ESMTPSA id z10-v6sm3279182pfh.83.2018.07.26.14.05.15 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 26 Jul 2018 14:05:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=pqb0VQA2BSiaDtcxPf8KeJQvuZh35mUZWvMFCVmKOm0=; b=TuZT0SmEBublqulIlVsgy84JQ9WCxBJHOlpX4J6DuXpmaXTFRn/lS7zMyOQI8NsTOh LbRtQTNzYZSrFJphAutSFXwxTSZwwpYZplrGg2Y686Ea6wwn4Mml7g1WAOQnuOuxbBEJ 9GvOAkMs3zAr1bZP7GOLR4VFoFc4PUlnZDRAUqW6kIDD+YDb/G39sHhXdttPcdWKmjZR 7h2Ue6d3LGkIA43ir5a7Yj/BuvbxywZn+ibbkExa+GfRmChP3hO0gJ/LfAQm17Fj8aXt GveNbuVuy15N4CXPPR/8bWPzwOqsbosJn3azPaY6mp+JIDUMjTWgp7ajZT3KTvQf3Q17 eYoA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=pqb0VQA2BSiaDtcxPf8KeJQvuZh35mUZWvMFCVmKOm0=; b=B91IecWfQPOs2Kr0l98TPdDFL7BBM0MoA9Q/04k+G8AJC3lhRmX+D4TbAcKxxhE7R4 LsESO2HXXgNzDCBx1t7YphdlVRWyYkTkYSKwMfJgLRazRJBCzYDAPtEtF89njFXPbF02 arWPjG+O0APEsYJE8064TD4fF0IOcaitrvbGgWuqRS4VyTivCXJmIO/GGUVplZh+b5rd HAJ5a+05cy568MOWdaf8ZMrdyK7YNoHrIPIav9yFs3eIdit4QP11MERSTX0OIXnuuLcL mrsXzm9P5XWP4CxSUz7A4PDfWGEuHYfT9ZhKMTppx+GCgwIIGub181xAdhFdYD1flc5j lAOA== X-Gm-Message-State: AOUpUlF6/s8xtnuqbeqY7akd4Y2NI6X/48Jrz3010PsISLBDrhOqlux6 EHlKTddQxwWbXRTwjE/LAAvxNC/T X-Google-Smtp-Source: AAOMgpeVZ9tImPbHUCyQxc3X+mFMl4b1tRdyUWwCa8rCsmBkRb7HgszrYyrvO3pD2VswZHhEZ/K7Ag== X-Received: by 2002:a17:902:bd44:: with SMTP id b4-v6mr3430784plx.144.1532639117090; Thu, 26 Jul 2018 14:05:17 -0700 (PDT) From: Sukrit Bhatnagar To: libvir-list@redhat.com Date: Tue, 24 Jul 2018 21:22:21 +0530 Message-Id: <1532447562-24010-21-git-send-email-skrtbhtngr@gmail.com> In-Reply-To: <1532447562-24010-1-git-send-email-skrtbhtngr@gmail.com> References: <1532447562-24010-1-git-send-email-skrtbhtngr@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Thu, 26 Jul 2018 21:05:18 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Thu, 26 Jul 2018 21:05:18 +0000 (UTC) for IP:'209.85.160.66' DOMAIN:'mail-pl0-f66.google.com' HELO:'mail-pl0-f66.google.com' FROM:'skrtbhtngr@gmail.com' RCPT:'' X-RedHat-Spam-Score: 1.37 * (DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SORBS_WEB, SPF_PASS) 209.85.160.66 mail-pl0-f66.google.com 209.85.160.66 mail-pl0-f66.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.38 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v2 20/41] util: pci: use VIR_AUTOFREE instead of VIR_FREE for scalar types 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: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.26 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Thu, 26 Jul 2018 21:05:49 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" By making use of GNU C's cleanup attribute handled by the VIR_AUTOFREE macro for declaring scalar variables, majority of the VIR_FREE calls can be dropped, which in turn leads to getting rid of most of our cleanup sections. Signed-off-by: Sukrit Bhatnagar --- src/util/virpci.c | 269 ++++++++++++++++++--------------------------------= ---- 1 file changed, 87 insertions(+), 182 deletions(-) diff --git a/src/util/virpci.c b/src/util/virpci.c index 46f9905..1b3e52f 100644 --- a/src/util/virpci.c +++ b/src/util/virpci.c @@ -253,7 +253,7 @@ int virPCIDeviceGetDriverPathAndName(virPCIDevicePtr dev, char **path, char **= name) { int ret =3D -1; - char *drvlink =3D NULL; + VIR_AUTOFREE(char *) drvlink =3D NULL; =20 *path =3D *name =3D NULL; /* drvlink =3D "/sys/bus/pci/dddd:bb:ss.ff/driver" */ @@ -285,7 +285,6 @@ virPCIDeviceGetDriverPathAndName(virPCIDevicePtr dev, c= har **path, char **name) =20 ret =3D 0; cleanup: - VIR_FREE(drvlink); if (ret < 0) { VIR_FREE(*path); VIR_FREE(*name); @@ -375,32 +374,27 @@ virPCIDeviceRead32(virPCIDevicePtr dev, int cfgfd, un= signed int pos) static int virPCIDeviceReadClass(virPCIDevicePtr dev, uint16_t *device_class) { - char *path =3D NULL; - char *id_str =3D NULL; - int ret =3D -1; + VIR_AUTOFREE(char *) path =3D NULL; + VIR_AUTOFREE(char *) id_str =3D NULL; unsigned int value; =20 if (!(path =3D virPCIFile(dev->name, "class"))) - return ret; + return -1; =20 /* class string is '0xNNNNNN\n' ... i.e. 9 bytes */ if (virFileReadAll(path, 9, &id_str) < 0) - goto cleanup; + return -1; =20 id_str[8] =3D '\0'; if (virStrToLong_ui(id_str, NULL, 16, &value) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, _("Unusual value in %s/devices/%s/class: %s"), PCI_SYSFS, dev->name, id_str); - goto cleanup; + return -1; } =20 *device_class =3D (value >> 8) & 0xFFFF; - ret =3D 0; - cleanup: - VIR_FREE(id_str); - VIR_FREE(path); - return ret; + return 0; } =20 static int @@ -574,7 +568,7 @@ virPCIDeviceDetectFunctionLevelReset(virPCIDevicePtr de= v, int cfgfd) { uint32_t caps; uint8_t pos; - char *path; + VIR_AUTOFREE(char *) path =3D NULL; int found; =20 /* The PCIe Function Level Reset capability allows @@ -614,7 +608,6 @@ virPCIDeviceDetectFunctionLevelReset(virPCIDevicePtr de= v, int cfgfd) return -1; =20 found =3D virFileExists(path); - VIR_FREE(path); if (found) { VIR_DEBUG("%s %s: buggy device didn't advertise FLR, but is a VF; = forcing flr on", dev->id, dev->name); @@ -926,8 +919,8 @@ virPCIDeviceReset(virPCIDevicePtr dev, virPCIDeviceList *activeDevs, virPCIDeviceList *inactiveDevs) { - char *drvPath =3D NULL; - char *drvName =3D NULL; + VIR_AUTOFREE(char *) drvPath =3D NULL; + VIR_AUTOFREE(char *) drvName =3D NULL; int ret =3D -1; int fd =3D -1; int hdrType =3D -1; @@ -1000,8 +993,6 @@ virPCIDeviceReset(virPCIDevicePtr dev, } =20 cleanup: - VIR_FREE(drvPath); - VIR_FREE(drvName); virPCIDeviceConfigClose(dev, fd); return ret; } @@ -1011,7 +1002,7 @@ static int virPCIProbeStubDriver(virPCIStubDriver driver) { const char *drvname =3D NULL; - char *drvpath =3D NULL; + VIR_AUTOFREE(char *) drvpath =3D NULL; bool probed =3D false; =20 if (driver =3D=3D VIR_PCI_STUB_DRIVER_NONE || @@ -1023,20 +1014,15 @@ virPCIProbeStubDriver(virPCIStubDriver driver) } =20 recheck: - if ((drvpath =3D virPCIDriverDir(drvname)) && virFileExists(drvpath)) { + if ((drvpath =3D virPCIDriverDir(drvname)) && virFileExists(drvpath)) /* driver already loaded, return */ - VIR_FREE(drvpath); return 0; - } - - VIR_FREE(drvpath); =20 if (!probed) { - char *errbuf =3D NULL; + VIR_AUTOFREE(char *) errbuf =3D NULL; probed =3D true; if ((errbuf =3D virKModLoad(drvname, true))) { VIR_WARN("failed to load driver %s: %s", drvname, errbuf); - VIR_FREE(errbuf); goto cleanup; } =20 @@ -1064,38 +1050,30 @@ virPCIProbeStubDriver(virPCIStubDriver driver) int virPCIDeviceUnbind(virPCIDevicePtr dev) { - char *path =3D NULL; - char *drvpath =3D NULL; - char *driver =3D NULL; - int ret =3D -1; + VIR_AUTOFREE(char *) path =3D NULL; + VIR_AUTOFREE(char *) drvpath =3D NULL; + VIR_AUTOFREE(char *) driver =3D NULL; =20 if (virPCIDeviceGetDriverPathAndName(dev, &drvpath, &driver) < 0) - goto cleanup; + return -1; =20 - if (!driver) { + if (!driver) /* The device is not bound to any driver */ - ret =3D 0; - goto cleanup; - } + return 0; =20 if (!(path =3D virPCIFile(dev->name, "driver/unbind"))) - goto cleanup; + return -1; =20 if (virFileExists(path)) { if (virFileWriteStr(path, dev->name, 0) < 0) { virReportSystemError(errno, _("Failed to unbind PCI device '%s' from = %s"), dev->name, driver); - goto cleanup; + return -1; } } =20 - ret =3D 0; - cleanup: - VIR_FREE(path); - VIR_FREE(drvpath); - VIR_FREE(driver); - return ret; + return 0; } =20 =20 @@ -1138,8 +1116,7 @@ static int virPCIDeviceBindWithDriverOverride(virPCIDevicePtr dev, const char *driverName) { - int ret =3D -1; - char *path; + VIR_AUTOFREE(char *) path =3D NULL; =20 if (!(path =3D virPCIFile(dev->name, "driver_override"))) return -1; @@ -1149,26 +1126,22 @@ virPCIDeviceBindWithDriverOverride(virPCIDevicePtr = dev, _("Failed to add driver '%s' to driver_overri= de " " interface of PCI device '%s'"), driverName, dev->name); - goto cleanup; + return -1; } =20 if (virPCIDeviceRebind(dev) < 0) - goto cleanup; + return -1; =20 - ret =3D 0; - - cleanup: - VIR_FREE(path); - return ret; + return 0; } =20 static int virPCIDeviceUnbindFromStubWithNewid(virPCIDevicePtr dev) { int result =3D -1; - char *drvdir =3D NULL; - char *path =3D NULL; - char *driver =3D NULL; + VIR_AUTOFREE(char *) drvdir =3D NULL; + VIR_AUTOFREE(char *) path =3D NULL; + VIR_AUTOFREE(char *) driver =3D NULL; =20 /* If the device is currently bound to one of the "well known" * stub drivers, then unbind it, otherwise ignore it. @@ -1257,10 +1230,6 @@ virPCIDeviceUnbindFromStubWithNewid(virPCIDevicePtr = dev) dev->remove_slot =3D false; dev->reprobe =3D false; =20 - VIR_FREE(drvdir); - VIR_FREE(path); - VIR_FREE(driver); - return result; } =20 @@ -1278,8 +1247,7 @@ virPCIDeviceUnbindFromStubWithOverride(virPCIDevicePt= r dev) static int virPCIDeviceUnbindFromStub(virPCIDevicePtr dev) { - int ret; - char *path; + VIR_AUTOFREE(char *) path =3D NULL; =20 /* * Prefer using the device's driver_override interface, falling back @@ -1289,12 +1257,9 @@ virPCIDeviceUnbindFromStub(virPCIDevicePtr dev) return -1; =20 if (virFileExists(path)) - ret =3D virPCIDeviceUnbindFromStubWithOverride(dev); - else - ret =3D virPCIDeviceUnbindFromStubWithNewid(dev); + return virPCIDeviceUnbindFromStubWithOverride(dev); =20 - VIR_FREE(path); - return ret; + return virPCIDeviceUnbindFromStubWithNewid(dev); } =20 static int @@ -1302,9 +1267,9 @@ virPCIDeviceBindToStubWithNewid(virPCIDevicePtr dev) { int result =3D -1; bool reprobe =3D false; - char *stubDriverPath =3D NULL; - char *driverLink =3D NULL; - char *path =3D NULL; /* reused for different purposes */ + VIR_AUTOFREE(char *) stubDriverPath =3D NULL; + VIR_AUTOFREE(char *) driverLink =3D NULL; + VIR_AUTOFREE(char *) path =3D NULL; /* reused for different purposes */ const char *stubDriverName =3D NULL; virErrorPtr err =3D NULL; =20 @@ -1436,10 +1401,6 @@ virPCIDeviceBindToStubWithNewid(virPCIDevicePtr dev) } =20 cleanup: - VIR_FREE(stubDriverPath); - VIR_FREE(driverLink); - VIR_FREE(path); - if (result < 0) virPCIDeviceUnbindFromStub(dev); =20 @@ -1453,10 +1414,9 @@ virPCIDeviceBindToStubWithNewid(virPCIDevicePtr dev) static int virPCIDeviceBindToStubWithOverride(virPCIDevicePtr dev) { - int ret =3D -1; const char *stubDriverName; - char *stubDriverPath =3D NULL; - char *driverLink =3D NULL; + VIR_AUTOFREE(char *) stubDriverPath =3D NULL; + VIR_AUTOFREE(char *) driverLink =3D NULL; =20 /* Check the device is configured to use one of the known stub drivers= */ if (dev->stubDriver =3D=3D VIR_PCI_STUB_DRIVER_NONE) { @@ -1473,35 +1433,28 @@ virPCIDeviceBindToStubWithOverride(virPCIDevicePtr = dev) =20 if (!(stubDriverPath =3D virPCIDriverDir(stubDriverName)) || !(driverLink =3D virPCIFile(dev->name, "driver"))) - goto cleanup; + return -1; =20 if (virFileExists(driverLink)) { if (virFileLinkPointsTo(driverLink, stubDriverPath)) { /* The device is already bound to the correct driver */ VIR_DEBUG("Device %s is already bound to %s", dev->name, stubDriverName); - ret =3D 0; - goto cleanup; + return 0; } } =20 if (virPCIDeviceBindWithDriverOverride(dev, stubDriverName) < 0) - goto cleanup; + return -1; =20 dev->unbind_from_stub =3D true; - ret =3D 0; - - cleanup: - VIR_FREE(stubDriverPath); - VIR_FREE(driverLink); - return ret; + return 0; } =20 static int virPCIDeviceBindToStub(virPCIDevicePtr dev) { - int ret; - char *path; + VIR_AUTOFREE(char *) path =3D NULL; =20 /* * Prefer using the device's driver_override interface, falling back @@ -1511,12 +1464,9 @@ virPCIDeviceBindToStub(virPCIDevicePtr dev) return -1; =20 if (virFileExists(path)) - ret =3D virPCIDeviceBindToStubWithOverride(dev); - else - ret =3D virPCIDeviceBindToStubWithNewid(dev); + return virPCIDeviceBindToStubWithOverride(dev); =20 - VIR_FREE(path); - return ret; + return virPCIDeviceBindToStubWithNewid(dev); } =20 /* virPCIDeviceDetach: @@ -1700,19 +1650,15 @@ virPCIDeviceWaitForCleanup(virPCIDevicePtr dev, con= st char *matcher) static char * virPCIDeviceReadID(virPCIDevicePtr dev, const char *id_name) { - char *path =3D NULL; + VIR_AUTOFREE(char *) path =3D NULL; char *id_str; =20 if (!(path =3D virPCIFile(dev->name, id_name))) return NULL; =20 /* ID string is '0xNNNN\n' ... i.e. 7 bytes */ - if (virFileReadAll(path, 7, &id_str) < 0) { - VIR_FREE(path); + if (virFileReadAll(path, 7, &id_str) < 0) return NULL; - } - - VIR_FREE(path); =20 /* Check for 0x suffix */ if (id_str[0] !=3D '0' || id_str[1] !=3D 'x') { @@ -1755,8 +1701,8 @@ virPCIDeviceNew(unsigned int domain, unsigned int function) { virPCIDevicePtr dev; - char *vendor =3D NULL; - char *product =3D NULL; + VIR_AUTOFREE(char *) vendor =3D NULL; + VIR_AUTOFREE(char *) product =3D NULL; =20 if (VIR_ALLOC(dev) < 0) return NULL; @@ -1806,14 +1752,12 @@ virPCIDeviceNew(unsigned int domain, VIR_DEBUG("%s %s: initialized", dev->id, dev->name); =20 cleanup: - VIR_FREE(product); - VIR_FREE(vendor); return dev; =20 error: virPCIDeviceFree(dev); dev =3D NULL; - goto cleanup; + goto cleanup;; } =20 =20 @@ -2129,8 +2073,7 @@ int virPCIDeviceFileIterate(virPCIDevicePtr dev, virPCIDeviceFileActor actor, void *opaque) { - char *pcidir =3D NULL; - char *file =3D NULL; + VIR_AUTOFREE(char *) pcidir =3D NULL; DIR *dir =3D NULL; int ret =3D -1; struct dirent *ent; @@ -2145,6 +2088,7 @@ int virPCIDeviceFileIterate(virPCIDevicePtr dev, goto cleanup; =20 while ((direrr =3D virDirRead(dir, &ent, pcidir)) > 0) { + VIR_AUTOFREE(char *) file =3D NULL; /* Device assignment requires: * $PCIDIR/config, $PCIDIR/resource, $PCIDIR/resourceNNN, * $PCIDIR/rom, $PCIDIR/reset, $PCIDIR/vendor, $PCIDIR/device @@ -2159,8 +2103,6 @@ int virPCIDeviceFileIterate(virPCIDevicePtr dev, goto cleanup; if ((actor)(dev, file, opaque) < 0) goto cleanup; - - VIR_FREE(file); } } if (direrr < 0) @@ -2170,8 +2112,6 @@ int virPCIDeviceFileIterate(virPCIDevicePtr dev, =20 cleanup: VIR_DIR_CLOSE(dir); - VIR_FREE(file); - VIR_FREE(pcidir); return ret; } =20 @@ -2186,7 +2126,7 @@ virPCIDeviceAddressIOMMUGroupIterate(virPCIDeviceAddr= essPtr orig, virPCIDeviceAddressActor actor, void *opaque) { - char *groupPath =3D NULL; + VIR_AUTOFREE(char *) groupPath =3D NULL; DIR *groupDir =3D NULL; int ret =3D -1; struct dirent *ent; @@ -2222,7 +2162,6 @@ virPCIDeviceAddressIOMMUGroupIterate(virPCIDeviceAddr= essPtr orig, ret =3D 0; =20 cleanup: - VIR_FREE(groupPath); VIR_DIR_CLOSE(groupDir); return ret; } @@ -2341,28 +2280,25 @@ virPCIDeviceAddressGetIOMMUGroupAddresses(virPCIDev= iceAddressPtr devAddr, int virPCIDeviceAddressGetIOMMUGroupNum(virPCIDeviceAddressPtr addr) { - char *devName =3D NULL; - char *devPath =3D NULL; - char *groupPath =3D NULL; + VIR_AUTOFREE(char *) devName =3D NULL; + VIR_AUTOFREE(char *) devPath =3D NULL; + VIR_AUTOFREE(char *) groupPath =3D NULL; const char *groupNumStr; unsigned int groupNum; - int ret =3D -1; =20 if (virAsprintf(&devName, "%.4x:%.2x:%.2x.%.1x", addr->domain, addr->bus, addr->slot, addr->function) < 0) - goto cleanup; + return -1; =20 if (!(devPath =3D virPCIFile(devName, "iommu_group"))) - goto cleanup; - if (virFileIsLink(devPath) !=3D 1) { - ret =3D -2; - goto cleanup; - } + return -1; + if (virFileIsLink(devPath) !=3D 1) + return -2; if (virFileResolveLink(devPath, &groupPath) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, _("Unable to resolve device %s iommu_group symlink = %s"), devName, devPath); - goto cleanup; + return -1; } =20 groupNumStr =3D last_component(groupPath); @@ -2371,16 +2307,10 @@ virPCIDeviceAddressGetIOMMUGroupNum(virPCIDeviceAdd= ressPtr addr) _("device %s iommu_group symlink %s has " "invalid group number %s"), devName, groupPath, groupNumStr); - ret =3D -1; - goto cleanup; + return -1; } =20 - ret =3D groupNum; - cleanup: - VIR_FREE(devName); - VIR_FREE(devPath); - VIR_FREE(groupPath); - return ret; + return groupNum; } =20 =20 @@ -2390,30 +2320,28 @@ virPCIDeviceAddressGetIOMMUGroupNum(virPCIDeviceAdd= ressPtr addr) char * virPCIDeviceGetIOMMUGroupDev(virPCIDevicePtr dev) { - char *devPath =3D NULL; - char *groupPath =3D NULL; + VIR_AUTOFREE(char *) devPath =3D NULL; + VIR_AUTOFREE(char *) groupPath =3D NULL; char *groupDev =3D NULL; =20 if (!(devPath =3D virPCIFile(dev->name, "iommu_group"))) - goto cleanup; + return NULL; if (virFileIsLink(devPath) !=3D 1) { virReportError(VIR_ERR_INTERNAL_ERROR, _("Invalid device %s iommu_group file %s is not a s= ymlink"), dev->name, devPath); - goto cleanup; + return NULL; } if (virFileResolveLink(devPath, &groupPath) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, _("Unable to resolve device %s iommu_group symlink = %s"), dev->name, devPath); - goto cleanup; + return NULL; } if (virAsprintf(&groupDev, "/dev/vfio/%s", last_component(groupPath)) < 0) - goto cleanup; - cleanup: - VIR_FREE(devPath); - VIR_FREE(groupPath); + return NULL; + return groupDev; } =20 @@ -2614,7 +2542,7 @@ virPCIGetDeviceAddressFromSysfsLink(const char *devic= e_link) { virPCIDeviceAddressPtr bdf =3D NULL; char *config_address =3D NULL; - char *device_path =3D NULL; + VIR_AUTOFREE(char *) device_path =3D NULL; =20 if (!virFileExists(device_link)) { VIR_DEBUG("'%s' does not exist", device_link); @@ -2631,19 +2559,16 @@ virPCIGetDeviceAddressFromSysfsLink(const char *dev= ice_link) =20 config_address =3D last_component(device_path); if (VIR_ALLOC(bdf) < 0) - goto out; + return NULL; =20 if (virPCIDeviceAddressParse(config_address, bdf) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, _("Failed to parse PCI config address '%s'"), config_address); VIR_FREE(bdf); - goto out; + return NULL; } =20 - out: - VIR_FREE(device_path); - return bdf; } =20 @@ -2665,7 +2590,7 @@ int virPCIGetPhysicalFunction(const char *vf_sysfs_path, virPCIDeviceAddressPtr *pf) { - char *device_link =3D NULL; + VIR_AUTOFREE(char *) device_link =3D NULL; =20 *pf =3D NULL; =20 @@ -2678,7 +2603,6 @@ virPCIGetPhysicalFunction(const char *vf_sysfs_path, VIR_DEBUG("PF for VF device '%s': %.4x:%.2x:%.2x.%.1x", vf_sysfs_p= ath, (*pf)->domain, (*pf)->bus, (*pf)->slot, (*pf)->function); } - VIR_FREE(device_link); =20 return 0; } @@ -2695,9 +2619,9 @@ virPCIGetVirtualFunctions(const char *sysfs_path, { int ret =3D -1; size_t i; - char *device_link =3D NULL; + VIR_AUTOFREE(char *) totalvfs_file =3D NULL; + VIR_AUTOFREE(char *) totalvfs_str =3D NULL; virPCIDeviceAddressPtr config_addr =3D NULL; - char *totalvfs_file =3D NULL, *totalvfs_str =3D NULL; =20 *virtual_functions =3D NULL; *num_virtual_functions =3D 0; @@ -2719,6 +2643,7 @@ virPCIGetVirtualFunctions(const char *sysfs_path, } =20 do { + VIR_AUTOFREE(char *) device_link =3D NULL; /* look for virtfn%d links until one isn't found */ if (virAsprintf(&device_link, "%s/virtfn%zu", sysfs_path, *num_vir= tual_functions) < 0) goto error; @@ -2736,18 +2661,13 @@ virPCIGetVirtualFunctions(const char *sysfs_path, if (VIR_APPEND_ELEMENT(*virtual_functions, *num_virtual_functions, config_addr) < 0) goto error; - VIR_FREE(device_link); - } while (1); =20 VIR_DEBUG("Found %zu virtual functions for %s", *num_virtual_functions, sysfs_path); ret =3D 0; cleanup: - VIR_FREE(device_link); VIR_FREE(config_addr); - VIR_FREE(totalvfs_file); - VIR_FREE(totalvfs_str); return ret; =20 error: @@ -2765,18 +2685,13 @@ virPCIGetVirtualFunctions(const char *sysfs_path, int virPCIIsVirtualFunction(const char *vf_sysfs_device_link) { - char *vf_sysfs_physfn_link =3D NULL; - int ret =3D -1; + VIR_AUTOFREE(char *) vf_sysfs_physfn_link =3D NULL; =20 if (virAsprintf(&vf_sysfs_physfn_link, "%s/physfn", vf_sysfs_device_link) < 0) - return ret; + return -1; =20 - ret =3D virFileExists(vf_sysfs_physfn_link); - - VIR_FREE(vf_sysfs_physfn_link); - - return ret; + return virFileExists(vf_sysfs_physfn_link); } =20 /* @@ -2868,12 +2783,12 @@ virPCIGetNetName(const char *device_link_sysfs_path, char *physPortID, char **netname) { - char *pcidev_sysfs_net_path =3D NULL; + VIR_AUTOFREE(char *) pcidev_sysfs_net_path =3D NULL; + VIR_AUTOFREE(char *) firstEntryName =3D NULL; + VIR_AUTOFREE(char *) thisPhysPortID =3D NULL; int ret =3D -1; DIR *dir =3D NULL; struct dirent *entry =3D NULL; - char *firstEntryName =3D NULL; - char *thisPhysPortID =3D NULL; size_t i =3D 0; =20 if (virBuildPath(&pcidev_sysfs_net_path, device_link_sysfs_path, @@ -2946,9 +2861,6 @@ virPCIGetNetName(const char *device_link_sysfs_path, } cleanup: VIR_DIR_CLOSE(dir); - VIR_FREE(pcidev_sysfs_net_path); - VIR_FREE(thisPhysPortID); - VIR_FREE(firstEntryName); return ret; } =20 @@ -2959,9 +2871,9 @@ virPCIGetVirtualFunctionInfo(const char *vf_sysfs_dev= ice_path, int *vf_index) { virPCIDeviceAddressPtr pf_config_address =3D NULL; - char *pf_sysfs_device_path =3D NULL; - char *vfname =3D NULL; - char *vfPhysPortID =3D NULL; + VIR_AUTOFREE(char *) pf_sysfs_device_path =3D NULL; + VIR_AUTOFREE(char *) vfname =3D NULL; + VIR_AUTOFREE(char *) vfPhysPortID =3D NULL; int ret =3D -1; =20 if (virPCIGetPhysicalFunction(vf_sysfs_device_path, &pf_config_address= ) < 0) @@ -3016,9 +2928,6 @@ virPCIGetVirtualFunctionInfo(const char *vf_sysfs_dev= ice_path, ret =3D 0; cleanup: VIR_FREE(pf_config_address); - VIR_FREE(pf_sysfs_device_path); - VIR_FREE(vfname); - VIR_FREE(vfPhysPortID); =20 return ret; } @@ -3032,8 +2941,7 @@ virPCIGetMdevTypes(const char *sysfspath, int dirret =3D -1; DIR *dir =3D NULL; struct dirent *entry; - char *types_path =3D NULL; - char *tmppath =3D NULL; + VIR_AUTOFREE(char *) types_path =3D NULL; virMediatedDeviceTypePtr mdev_type =3D NULL; virMediatedDeviceTypePtr *mdev_types =3D NULL; size_t ntypes =3D 0; @@ -3051,6 +2959,7 @@ virPCIGetMdevTypes(const char *sysfspath, } =20 while ((dirret =3D virDirRead(dir, &entry, types_path)) > 0) { + VIR_AUTOFREE(char *) tmppath =3D NULL; /* append the type id to the path and read the attributes from the= re */ if (virAsprintf(&tmppath, "%s/%s", types_path, entry->d_name) < 0) goto cleanup; @@ -3060,8 +2969,6 @@ virPCIGetMdevTypes(const char *sysfspath, =20 if (VIR_APPEND_ELEMENT(mdev_types, ntypes, mdev_type) < 0) goto cleanup; - - VIR_FREE(tmppath); } =20 if (dirret < 0) @@ -3075,8 +2982,6 @@ virPCIGetMdevTypes(const char *sysfspath, for (i =3D 0; i < ntypes; i++) virMediatedDeviceTypeFree(mdev_types[i]); VIR_FREE(mdev_types); - VIR_FREE(types_path); - VIR_FREE(tmppath); VIR_DIR_CLOSE(dir); return ret; } --=20 1.8.3.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list