From nobody Sun Feb 8 19:38:01 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; 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 1532175007117666.3789090643234; Sat, 21 Jul 2018 05:10:07 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 829ECC057FA1; Sat, 21 Jul 2018 12:09:59 +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 411715C20D; Sat, 21 Jul 2018 12:09:58 +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 E3CE74EE10; Sat, 21 Jul 2018 12:09:57 +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 w6LC8SeE016441 for ; Sat, 21 Jul 2018 08:08:28 -0400 Received: by smtp.corp.redhat.com (Postfix) id 2F5B310694C2; Sat, 21 Jul 2018 12:08:28 +0000 (UTC) Received: from mx1.redhat.com (ext-mx11.extmail.prod.ext.phx2.redhat.com [10.5.110.40]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 27815100164C for ; Sat, 21 Jul 2018 12:08:26 +0000 (UTC) Received: from mail-pl0-f65.google.com (mail-pl0-f65.google.com [209.85.160.65]) (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 B07563084042 for ; Sat, 21 Jul 2018 12:08:14 +0000 (UTC) Received: by mail-pl0-f65.google.com with SMTP id t17-v6so3749467ply.13 for ; Sat, 21 Jul 2018 05:08:14 -0700 (PDT) Received: from centos.localdomain ([42.106.1.69]) by smtp.gmail.com with ESMTPSA id s16-v6sm5861694pfm.114.2018.07.21.05.08.11 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 21 Jul 2018 05:08:13 -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=2TKu+iM3xmwUCZZg37xDF/xQBd5PNg+RQk5RyaeZflA=; b=YpgK9rq6b4of2ES7nc6HbbMS156nBUWD3TLkOTAjw8J7y52/41Z7pCH1SLAbR1TdrM ulF4qXOhl30ocuRoxjZcPG7RClV7qqB8Ty/X3DyDX5kWTc/M12LXm10XQxu6XvJYvWZ9 2xPfnjyWIlzGVZt9hqwpiO6+82CUQvk5m/QFeVJ5hOeKTmj9DjNXkWi3HaQRrotwvk2u g2rjgEog2DgdO+PuhjnJBDT8RdJTklUG30LIf+oGTI48A4AEOb52+wpxhvPiHzqjegdP 7aRfpL4N0M8hOqME69rawWpAf+aqr2mcD+IOJuKfOTqnNTRrHLrG1KVhDBVwRl6I3Yje Qp9w== 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=2TKu+iM3xmwUCZZg37xDF/xQBd5PNg+RQk5RyaeZflA=; b=LsOxRQbR8xjHh3WLqGtKBsxAbK7ndVAn06m4vO04TDBdhx47oUujDYcl6B7SBPfQ9P tWeHy/n1uAVo39tyoA5TgZzNMdK/qrBxhYJkplHJbO17mHSHHUBg7wSvKh/wahRiANDy AsJ5wG7HYSSDHVkneVYNxNeACV8kiRn2Q99sZ5R1AFQHSVhWS9xGnRxaO2/drCtKo8Y4 Ol/r9xcwV+KKn6pt4embB6d2QMjIlVezAIZaID0WnaUppc4U/bpfnkv4fAauEHh51jP3 Pys0c2VLHEymuiPWGUZITdZZyxBbpmVeFn5iEDtdgiFYcs3XQ/bpgQ2KvIwsQGpBDsWb Z84g== X-Gm-Message-State: AOUpUlEMnpMCBfZn0K8g5OnIVvQRJpSN4H2wsG+8DWvVeQ8E7aun3FSr HOqZ/4EpB7G+IxEgRDEZvfZpaK1d X-Google-Smtp-Source: AAOMgpf6lEkEblI/779uQ6aNl73S0T5FEBQZuAe15/sU7tjOn8UkVW9TFq7AB00vE4Tz4A3xNep7vQ== X-Received: by 2002:a17:902:5a83:: with SMTP id r3-v6mr5571444pli.78.1532174893921; Sat, 21 Jul 2018 05:08:13 -0700 (PDT) From: Sukrit Bhatnagar To: libvir-list@redhat.com Date: Sat, 21 Jul 2018 17:36:52 +0530 Message-Id: <1532174832-21644-21-git-send-email-skrtbhtngr@gmail.com> In-Reply-To: <1532174832-21644-1-git-send-email-skrtbhtngr@gmail.com> References: <1532174832-21644-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.40]); Sat, 21 Jul 2018 12:08:14 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.40]); Sat, 21 Jul 2018 12:08:14 +0000 (UTC) for IP:'209.85.160.65' DOMAIN:'mail-pl0-f65.google.com' HELO:'mail-pl0-f65.google.com' FROM:'skrtbhtngr@gmail.com' RCPT:'' X-RedHat-Spam-Score: 1.281 * (DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, NML_ADSP_CUSTOM_MED, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_PASS, T_DKIM_INVALID) 209.85.160.65 mail-pl0-f65.google.com 209.85.160.65 mail-pl0-f65.google.com X-Scanned-By: MIMEDefang 2.84 on 10.5.110.40 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v1 20/40] 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.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Sat, 21 Jul 2018 12:10:06 +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 | 271 ++++++++++++++++++--------------------------------= ---- 1 file changed, 87 insertions(+), 184 deletions(-) diff --git a/src/util/virpci.c b/src/util/virpci.c index a606462..1f6ac0b 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); + return virPCIDeviceUnbindFromStubWithOverride(dev); else - ret =3D virPCIDeviceUnbindFromStubWithNewid(dev); - - 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); + return virPCIDeviceBindToStubWithOverride(dev); else - ret =3D virPCIDeviceBindToStubWithNewid(dev); - - 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; @@ -1805,15 +1751,11 @@ virPCIDeviceNew(unsigned int domain, =20 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; + return NULL; } =20 =20 @@ -2129,8 +2071,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 +2086,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 +2101,6 @@ int virPCIDeviceFileIterate(virPCIDevicePtr dev, goto cleanup; if ((actor)(dev, file, opaque) < 0) goto cleanup; - - VIR_FREE(file); } } if (direrr < 0) @@ -2170,8 +2110,6 @@ int virPCIDeviceFileIterate(virPCIDevicePtr dev, =20 cleanup: VIR_DIR_CLOSE(dir); - VIR_FREE(file); - VIR_FREE(pcidir); return ret; } =20 @@ -2186,7 +2124,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 +2160,6 @@ virPCIDeviceAddressIOMMUGroupIterate(virPCIDeviceAddr= essPtr orig, ret =3D 0; =20 cleanup: - VIR_FREE(groupPath); VIR_DIR_CLOSE(groupDir); return ret; } @@ -2341,28 +2278,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 +2305,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 +2318,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 +2540,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 +2557,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 +2588,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 +2601,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 +2617,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 +2641,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 +2659,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 +2683,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 +2781,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 +2859,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 +2869,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 +2926,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 +2939,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 +2957,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 +2967,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 +2980,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