From nobody Mon Feb 9 02:13:25 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 1549651114545823.1168611426077; Fri, 8 Feb 2019 10:38:34 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 52468882EA; Fri, 8 Feb 2019 18:38:32 +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 E174417F3F; Fri, 8 Feb 2019 18:38:31 +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 89A2318033BB; Fri, 8 Feb 2019 18:38:31 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x18IbhwD007671 for ; Fri, 8 Feb 2019 13:37:43 -0500 Received: by smtp.corp.redhat.com (Postfix) id 813F1383F; Fri, 8 Feb 2019 18:37:43 +0000 (UTC) Received: from unknown0050b6a41c42.attlocal.net.com (ovpn-117-20.phx2.redhat.com [10.3.117.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id 25F1F60BE0; Fri, 8 Feb 2019 18:37:43 +0000 (UTC) From: John Ferlan To: libvir-list@redhat.com Date: Fri, 8 Feb 2019 13:37:12 -0500 Message-Id: <20190208183726.30903-19-jferlan@redhat.com> In-Reply-To: <20190208183726.30903-1-jferlan@redhat.com> References: <20190208183726.30903-1-jferlan@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com Cc: Erik Skultety Subject: [libvirt] [PATCH v2 18/32] util: Use VIR_AUTOFREE for virstoragefile 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.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Fri, 08 Feb 2019 18:38:33 +0000 (UTC) Content-Type: text/plain; charset="utf-8" Let's make use of the auto __cleanup capabilities cleaning up any now unnecessary goto paths. Signed-off-by: John Ferlan Reviewed-by: Erik Skultety Reviewed-by: J=C3=A1n Tomko --- src/util/virstoragefile.c | 128 ++++++++++++++------------------------ 1 file changed, 46 insertions(+), 82 deletions(-) diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c index c6425308fb..fddfea65db 100644 --- a/src/util/virstoragefile.c +++ b/src/util/virstoragefile.c @@ -595,9 +595,10 @@ vmdk4GetBackingStore(char **res, size_t buf_size) { static const char prefix[] =3D "parentFileNameHint=3D\""; - char *desc, *start, *end; + char *start, *end; size_t len; int ret =3D BACKING_STORE_ERROR; + VIR_AUTOFREE(char *) desc =3D NULL; =20 if (VIR_ALLOC_N(desc, VIR_STORAGE_MAX_HEADER) < 0) goto cleanup; @@ -645,7 +646,6 @@ vmdk4GetBackingStore(char **res, ret =3D BACKING_STORE_OK; =20 cleanup: - VIR_FREE(desc); return ret; } =20 @@ -1084,7 +1084,7 @@ virStorageFileProbeFormat(const char *path, uid_t uid= , gid_t gid) int ret =3D -1; struct stat sb; ssize_t len =3D VIR_STORAGE_MAX_HEADER; - char *header =3D NULL; + VIR_AUTOFREE(char *) header =3D NULL; =20 if ((fd =3D virFileOpenAs(path, O_RDONLY, 0, uid, gid, 0)) < 0) { virReportSystemError(-fd, _("Failed to open file '%s'"), path); @@ -1115,7 +1115,6 @@ virStorageFileProbeFormat(const char *path, uid_t uid= , gid_t gid) ret =3D virStorageFileProbeFormatFromBuf(path, header, len); =20 cleanup: - VIR_FREE(header); VIR_FORCE_CLOSE(fd); =20 return ret; @@ -1215,10 +1214,10 @@ virStorageFileGetMetadataFromFD(const char *path, { virStorageSourcePtr ret =3D NULL; virStorageSourcePtr meta =3D NULL; - char *buf =3D NULL; ssize_t len =3D VIR_STORAGE_MAX_HEADER; struct stat sb; int dummy; + VIR_AUTOFREE(char *) buf =3D NULL; =20 if (!backingFormat) backingFormat =3D &dummy; @@ -1265,7 +1264,6 @@ virStorageFileGetMetadataFromFD(const char *path, =20 cleanup: virStorageSourceFree(meta); - VIR_FREE(buf); return ret; } =20 @@ -1614,8 +1612,7 @@ virStorageFileParseChainIndex(const char *diskTarget, unsigned int *chainIndex) { unsigned int idx =3D 0; - char *target =3D NULL; - int ret =3D 0; + VIR_AUTOFREE(char *) target =3D NULL; =20 *chainIndex =3D 0; =20 @@ -1626,21 +1623,18 @@ virStorageFileParseChainIndex(const char *diskTarge= t, return 0; =20 if (idx =3D=3D 0) - goto cleanup; + return 0; =20 if (STRNEQ(diskTarget, target)) { virReportError(VIR_ERR_INVALID_ARG, _("requested target '%s' does not match target '%s'= "), target, diskTarget); - ret =3D -1; - goto cleanup; + return -1; } =20 *chainIndex =3D idx; =20 - cleanup: - VIR_FREE(target); - return ret; + return 0; } =20 =20 @@ -1891,8 +1885,8 @@ virStorageAuthDefParse(xmlNodePtr node, xmlNodePtr saveNode =3D ctxt->node; virStorageAuthDefPtr ret =3D NULL; xmlNodePtr secretnode =3D NULL; - char *authtype =3D NULL; VIR_AUTOPTR(virStorageAuthDef) authdef =3D NULL; + VIR_AUTOFREE(char *) authtype =3D NULL; =20 ctxt->node =3D node; =20 @@ -1939,7 +1933,6 @@ virStorageAuthDefParse(xmlNodePtr node, VIR_STEAL_PTR(ret, authdef); =20 cleanup: - VIR_FREE(authtype); ctxt->node =3D saveNode; =20 return ret; @@ -1983,10 +1976,10 @@ virStoragePRDefParseXML(xmlXPathContextPtr ctxt) { virStoragePRDefPtr prd; virStoragePRDefPtr ret =3D NULL; - char *managed =3D NULL; - char *type =3D NULL; - char *path =3D NULL; - char *mode =3D NULL; + VIR_AUTOFREE(char *) managed =3D NULL; + VIR_AUTOFREE(char *) type =3D NULL; + VIR_AUTOFREE(char *) path =3D NULL; + VIR_AUTOFREE(char *) mode =3D NULL; =20 if (VIR_ALLOC(prd) < 0) return NULL; @@ -2045,10 +2038,6 @@ virStoragePRDefParseXML(xmlXPathContextPtr ctxt) VIR_STEAL_PTR(ret, prd); =20 cleanup: - VIR_FREE(mode); - VIR_FREE(path); - VIR_FREE(type); - VIR_FREE(managed); virStoragePRDefFree(prd); return ret; } @@ -2601,8 +2590,8 @@ static virStorageSourcePtr virStorageSourceNewFromBackingRelative(virStorageSourcePtr parent, const char *rel) { - char *dirname =3D NULL; virStorageSourcePtr ret; + VIR_AUTOFREE(char *) dirname =3D NULL; =20 if (VIR_ALLOC(ret) < 0) return NULL; @@ -2645,7 +2634,6 @@ virStorageSourceNewFromBackingRelative(virStorageSour= cePtr parent, } =20 cleanup: - VIR_FREE(dirname); return ret; =20 error: @@ -2809,8 +2797,8 @@ int virStorageSourceParseRBDColonString(const char *rbdstr, virStorageSourcePtr src) { - char *options =3D NULL; char *p, *e, *next; + VIR_AUTOFREE(char *) options =3D NULL; VIR_AUTOPTR(virStorageAuthDef) authdef =3D NULL; =20 /* optionally skip the "rbd:" prefix if provided */ @@ -2818,19 +2806,19 @@ virStorageSourceParseRBDColonString(const char *rbd= str, rbdstr +=3D strlen("rbd:"); =20 if (VIR_STRDUP(src->path, rbdstr) < 0) - goto error; + return -1; =20 p =3D strchr(src->path, ':'); if (p) { if (VIR_STRDUP(options, p + 1) < 0) - goto error; + return -1; *p =3D '\0'; } =20 /* snapshot name */ if ((p =3D strchr(src->path, '@'))) { if (VIR_STRDUP(src->snapshot, p + 1) < 0) - goto error; + return -1; *p =3D '\0'; } =20 @@ -2838,7 +2826,7 @@ virStorageSourceParseRBDColonString(const char *rbdst= r, if ((p =3D strchr(src->path, '/'))) { VIR_STEAL_PTR(src->volume, src->path); if (VIR_STRDUP(src->path, p + 1) < 0) - goto error; + return -1; *p =3D '\0'; } =20 @@ -2866,14 +2854,14 @@ virStorageSourceParseRBDColonString(const char *rbd= str, if (STRPREFIX(p, "id=3D")) { /* formulate authdef for src->auth */ if (VIR_ALLOC(authdef) < 0) - goto error; + return -1; =20 if (VIR_STRDUP(authdef->username, p + strlen("id=3D")) < 0) - goto error; + return -1; =20 if (VIR_STRDUP(authdef->secrettype, virSecretUsageTypeToString(VIR_SECRET_USAGE_TYP= E_CEPH)) < 0) - goto error; + return -1; VIR_STEAL_PTR(src->auth, authdef); src->authInherited =3D true; =20 @@ -2897,7 +2885,7 @@ virStorageSourceParseRBDColonString(const char *rbdst= r, } =20 if (virStorageSourceRBDAddHost(src, h) < 0) - goto error; + return -1; =20 h =3D sep; } @@ -2905,16 +2893,11 @@ virStorageSourceParseRBDColonString(const char *rbd= str, =20 if (STRPREFIX(p, "conf=3D") && VIR_STRDUP(src->configFile, p + strlen("conf=3D")) < 0) - goto error; + return -1; =20 p =3D next; } - VIR_FREE(options); return 0; - - error: - VIR_FREE(options); - return -1; } =20 =20 @@ -2983,36 +2966,35 @@ static int virStorageSourceParseBackingColon(virStorageSourcePtr src, const char *path) { - char *protocol =3D NULL; const char *p; - int ret =3D -1; + VIR_AUTOFREE(char *) protocol =3D NULL; =20 if (!(p =3D strchr(path, ':'))) { virReportError(VIR_ERR_INTERNAL_ERROR, _("invalid backing protocol string '%s'"), path); - goto cleanup; + return -1; } =20 if (VIR_STRNDUP(protocol, path, p - path) < 0) - goto cleanup; + return -1; =20 if ((src->protocol =3D virStorageNetProtocolTypeFromString(protocol)) = < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, _("invalid backing protocol '%s'"), protocol); - goto cleanup; + return -1; } =20 switch ((virStorageNetProtocol) src->protocol) { case VIR_STORAGE_NET_PROTOCOL_NBD: if (virStorageSourceParseNBDColonString(path, src) < 0) - goto cleanup; + return -1; break; =20 case VIR_STORAGE_NET_PROTOCOL_RBD: if (virStorageSourceParseRBDColonString(path, src) < 0) - goto cleanup; + return -1; break; =20 case VIR_STORAGE_NET_PROTOCOL_SHEEPDOG: @@ -3021,7 +3003,7 @@ virStorageSourceParseBackingColon(virStorageSourcePtr= src, virReportError(VIR_ERR_INTERNAL_ERROR, _("backing store parser is not implemented for prot= ocol %s"), protocol); - goto cleanup; + return -1; =20 case VIR_STORAGE_NET_PROTOCOL_HTTP: case VIR_STORAGE_NET_PROTOCOL_HTTPS: @@ -3035,14 +3017,10 @@ virStorageSourceParseBackingColon(virStorageSourceP= tr src, virReportError(VIR_ERR_INTERNAL_ERROR, _("malformed backing store path for protocol %s"), protocol); - goto cleanup; + return -1; } =20 - ret =3D 0; - - cleanup: - VIR_FREE(protocol); - return ret; + return 0; } =20 =20 @@ -3593,9 +3571,9 @@ virStorageSourceParseBackingJSONInternal(virStorageSo= urcePtr src, virJSONValuePtr deflattened =3D NULL; virJSONValuePtr file; const char *drvname; - char *str =3D NULL; size_t i; int ret =3D -1; + VIR_AUTOFREE(char *) str =3D NULL; =20 if (!(deflattened =3D virJSONValueObjectDeflatten(json))) goto cleanup; @@ -3628,7 +3606,6 @@ virStorageSourceParseBackingJSONInternal(virStorageSo= urcePtr src, "driver '%s'"), drvname); =20 cleanup: - VIR_FREE(str); virJSONValueFree(deflattened); return ret; } @@ -3997,12 +3974,12 @@ virStorageFileCanonicalizePath(const char *path, bool beginDoubleSlash =3D false; char **components =3D NULL; size_t ncomponents =3D 0; - char *linkpath =3D NULL; - char *currentpath =3D NULL; size_t i =3D 0; size_t j =3D 0; int rc; char *ret =3D NULL; + VIR_AUTOFREE(char *) linkpath =3D NULL; + VIR_AUTOFREE(char *) currentpath =3D NULL; =20 if (path[0] =3D=3D '/') { beginSlash =3D true; @@ -4131,8 +4108,6 @@ virStorageFileCanonicalizePath(const char *path, cleanup: virHashFree(cycle); virStringListFreeCount(components, ncomponents); - VIR_FREE(linkpath); - VIR_FREE(currentpath); =20 return ret; } @@ -4175,25 +4150,22 @@ virStorageFileGetRelativeBackingPath(virStorageSour= cePtr top, char **relpath) { virStorageSourcePtr next; - char *tmp =3D NULL; - char *path =3D NULL; - char ret =3D -1; + VIR_AUTOFREE(char *) tmp =3D NULL; + VIR_AUTOFREE(char *) path =3D NULL; =20 *relpath =3D NULL; =20 for (next =3D top; virStorageSourceIsBacking(next); next =3D next->bac= kingStore) { - if (!next->relPath) { - ret =3D 1; - goto cleanup; - } + if (!next->relPath) + return 1; =20 if (!(tmp =3D virStorageFileRemoveLastPathComponent(path))) - goto cleanup; + return -1; =20 VIR_FREE(path); =20 if (virAsprintf(&path, "%s%s", tmp, next->relPath) < 0) - goto cleanup; + return -1; =20 VIR_FREE(tmp); =20 @@ -4205,17 +4177,11 @@ virStorageFileGetRelativeBackingPath(virStorageSour= cePtr top, virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("failed to resolve relative backing name: " "base image is not in backing chain")); - goto cleanup; + return -1; } =20 VIR_STEAL_PTR(*relpath, path); - - ret =3D 0; - - cleanup: - VIR_FREE(path); - VIR_FREE(tmp); - return ret; + return 0; } =20 =20 @@ -4866,11 +4832,11 @@ virStorageFileGetMetadataRecurse(virStorageSourcePt= r src, { int ret =3D -1; const char *uniqueName; - char *buf =3D NULL; ssize_t headerLen; virStorageSourcePtr backingStore =3D NULL; int backingFormat; int rv; + VIR_AUTOFREE(char *) buf =3D NULL; =20 VIR_DEBUG("path=3D%s format=3D%d uid=3D%u gid=3D%u", src->path, src->format, @@ -4952,7 +4918,6 @@ virStorageFileGetMetadataRecurse(virStorageSourcePtr = src, cleanup: if (virStorageSourceHasBacking(src)) src->backingStore->id =3D depth; - VIR_FREE(buf); virStorageFileDeinit(src); virStorageSourceFree(backingStore); return ret; @@ -5024,10 +4989,10 @@ virStorageFileGetBackingStoreStr(virStorageSourcePt= r src, char **backing) { virStorageSourcePtr tmp =3D NULL; - char *buf =3D NULL; ssize_t headerLen; int ret =3D -1; int rv; + VIR_AUTOFREE(char *) buf =3D NULL; =20 *backing =3D NULL; =20 @@ -5061,7 +5026,6 @@ virStorageFileGetBackingStoreStr(virStorageSourcePtr = src, ret =3D 0; =20 cleanup: - VIR_FREE(buf); virStorageSourceFree(tmp); =20 return ret; --=20 2.20.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list