From nobody Tue May 7 10:38:42 2024 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 155050363706964.39254517230563; Mon, 18 Feb 2019 07:27:17 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 848CB315D3F; Mon, 18 Feb 2019 15:27:14 +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 2046C5D70E; Mon, 18 Feb 2019 15:27:14 +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 1AF57181A00A; Mon, 18 Feb 2019 15:27:13 +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 x1IFRBof010000 for ; Mon, 18 Feb 2019 10:27:11 -0500 Received: by smtp.corp.redhat.com (Postfix) id BDEE15ED29; Mon, 18 Feb 2019 15:27:11 +0000 (UTC) Received: from libvirt-fedora-29.redhat.com (ovpn-117-20.phx2.redhat.com [10.3.117.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id 447055ED4F; Mon, 18 Feb 2019 15:27:07 +0000 (UTC) From: John Ferlan To: libvir-list@redhat.com Date: Mon, 18 Feb 2019 10:27:06 -0500 Message-Id: <20190218152706.17360-1-jferlan@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH] util: Use VIR_AUTOUNREF 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.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Mon, 18 Feb 2019 15:27:15 +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 --- BTW: I did try this with a linked travis-ci and github branch, and it worked for me. I'm avoiding altering virStorageFileMetadataNew... src/util/virstoragefile.c | 130 +++++++++++++++----------------------- 1 file changed, 52 insertions(+), 78 deletions(-) diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c index b2e308d81d..003183bf76 100644 --- a/src/util/virstoragefile.c +++ b/src/util/virstoragefile.c @@ -1205,11 +1205,11 @@ virStorageFileGetMetadataFromFD(const char *path, =20 { virStorageSourcePtr ret =3D NULL; - virStorageSourcePtr meta =3D NULL; ssize_t len =3D VIR_STORAGE_MAX_HEADER; struct stat sb; int dummy; VIR_AUTOFREE(char *) buf =3D NULL; + VIR_AUTOUNREF(virStorageSourcePtr) meta =3D NULL; =20 if (!backingFormat) backingFormat =3D &dummy; @@ -1231,21 +1231,21 @@ virStorageFileGetMetadataFromFD(const char *path, meta->type =3D VIR_STORAGE_TYPE_DIR; meta->format =3D VIR_STORAGE_FILE_DIR; VIR_STEAL_PTR(ret, meta); - goto cleanup; + return ret; } =20 if (lseek(fd, 0, SEEK_SET) =3D=3D (off_t)-1) { virReportSystemError(errno, _("cannot seek to start of '%s'"), met= a->path); - goto cleanup; + return NULL; } =20 if ((len =3D virFileReadHeaderFD(fd, len, &buf)) < 0) { virReportSystemError(errno, _("cannot read header '%s'"), meta->pa= th); - goto cleanup; + return NULL; } =20 if (virStorageFileGetMetadataInternal(meta, buf, len, backingFormat) <= 0) - goto cleanup; + return NULL; =20 if (S_ISREG(sb.st_mode)) meta->type =3D VIR_STORAGE_TYPE_FILE; @@ -1253,9 +1253,6 @@ virStorageFileGetMetadataFromFD(const char *path, meta->type =3D VIR_STORAGE_TYPE_BLOCK; =20 VIR_STEAL_PTR(ret, meta); - - cleanup: - virObjectUnref(meta); return ret; } =20 @@ -2243,7 +2240,8 @@ virStorageSourcePtr virStorageSourceCopy(const virStorageSource *src, bool backingChain) { - virStorageSourcePtr def =3D NULL; + virStorageSourcePtr ret =3D NULL; + VIR_AUTOUNREF(virStorageSourcePtr) def =3D NULL; =20 if (!(def =3D virStorageSourceNew())) return NULL; @@ -2282,60 +2280,57 @@ virStorageSourceCopy(const virStorageSource *src, VIR_STRDUP(def->compat, src->compat) < 0 || VIR_STRDUP(def->tlsAlias, src->tlsAlias) < 0 || VIR_STRDUP(def->tlsCertdir, src->tlsCertdir) < 0) - goto error; + return NULL; =20 if (src->nhosts) { if (!(def->hosts =3D virStorageNetHostDefCopy(src->nhosts, src->ho= sts))) - goto error; + return NULL; =20 def->nhosts =3D src->nhosts; } =20 if (src->srcpool && !(def->srcpool =3D virStorageSourcePoolDefCopy(src->srcpool))) - goto error; + return NULL; =20 if (src->features && !(def->features =3D virBitmapNewCopy(src->features))) - goto error; + return NULL; =20 if (src->encryption && !(def->encryption =3D virStorageEncryptionCopy(src->encryption))) - goto error; + return NULL; =20 if (src->perms && !(def->perms =3D virStoragePermsCopy(src->perms))) - goto error; + return NULL; =20 if (src->timestamps && !(def->timestamps =3D virStorageTimestampsCopy(src->timestamps))) - goto error; + return NULL; =20 if (virStorageSourceSeclabelsCopy(def, src) < 0) - goto error; + return NULL; =20 if (src->auth && !(def->auth =3D virStorageAuthDefCopy(src->auth))) - goto error; + return NULL; =20 if (src->pr && !(def->pr =3D virStoragePRDefCopy(src->pr))) - goto error; + return NULL; =20 if (virStorageSourceInitiatorCopy(&def->initiator, &src->initiator)) - goto error; + return NULL; =20 if (backingChain && src->backingStore) { if (!(def->backingStore =3D virStorageSourceCopy(src->backingStore, true))) - goto error; + return NULL; } =20 - return def; - - error: - virObjectUnref(def); - return NULL; + VIR_STEAL_PTR(ret, def); + return ret; } =20 =20 @@ -2601,27 +2596,28 @@ static virStorageSourcePtr virStorageSourceNewFromBackingRelative(virStorageSourcePtr parent, const char *rel) { - virStorageSourcePtr def; + virStorageSourcePtr ret =3D NULL; VIR_AUTOFREE(char *) dirname =3D NULL; + VIR_AUTOUNREF(virStorageSourcePtr) def =3D NULL; =20 if (!(def =3D virStorageSourceNew())) return NULL; =20 /* store relative name */ if (VIR_STRDUP(def->relPath, parent->backingStoreRaw) < 0) - goto error; + return NULL; =20 if (!(dirname =3D mdir_name(parent->path))) { virReportOOMError(); - goto error; + return NULL; } =20 if (STRNEQ(dirname, "/")) { if (virAsprintf(&def->path, "%s/%s", dirname, rel) < 0) - goto error; + return NULL; } else { if (virAsprintf(&def->path, "/%s", rel) < 0) - goto error; + return NULL; } =20 if (virStorageSourceGetActualType(parent) =3D=3D VIR_STORAGE_TYPE_NETW= ORK) { @@ -2632,25 +2628,20 @@ virStorageSourceNewFromBackingRelative(virStorageSo= urcePtr parent, if (parent->nhosts) { if (!(def->hosts =3D virStorageNetHostDefCopy(parent->nhosts, parent->hosts))) - goto error; + return NULL; =20 def->nhosts =3D parent->nhosts; } =20 if (VIR_STRDUP(def->volume, parent->volume) < 0) - goto error; + return NULL; } else { /* set the type to _FILE, the caller shall update it to the actual= type */ def->type =3D VIR_STORAGE_TYPE_FILE; } =20 - cleanup: - return def; - - error: - virObjectUnref(def); - def =3D NULL; - goto cleanup; + VIR_STEAL_PTR(ret, def); + return ret; } =20 =20 @@ -3648,8 +3639,9 @@ virStorageSourcePtr virStorageSourceNewFromBackingAbsolute(const char *path) { const char *json; - virStorageSourcePtr def; + virStorageSourcePtr ret =3D NULL; int rc; + VIR_AUTOUNREF(virStorageSourcePtr) def =3D NULL; =20 if (!(def =3D virStorageSourceNew())) return NULL; @@ -3658,7 +3650,7 @@ virStorageSourceNewFromBackingAbsolute(const char *pa= th) def->type =3D VIR_STORAGE_TYPE_FILE; =20 if (VIR_STRDUP(def->path, path) < 0) - goto error; + return NULL; } else { def->type =3D VIR_STORAGE_TYPE_NETWORK; =20 @@ -3673,7 +3665,7 @@ virStorageSourceNewFromBackingAbsolute(const char *pa= th) rc =3D virStorageSourceParseBackingColon(def, path); =20 if (rc < 0) - goto error; + return NULL; =20 virStorageSourceNetworkAssignDefaultPorts(def); =20 @@ -3686,11 +3678,8 @@ virStorageSourceNewFromBackingAbsolute(const char *p= ath) } } =20 - return def; - - error: - virObjectUnref(def); - return NULL; + VIR_STEAL_PTR(ret, def); + return ret; } =20 =20 @@ -3698,7 +3687,8 @@ virStorageSourcePtr virStorageSourceNewFromBacking(virStorageSourcePtr parent) { struct stat st; - virStorageSourcePtr def; + virStorageSourcePtr ret =3D NULL; + VIR_AUTOUNREF(virStorageSourcePtr) def =3D NULL; =20 if (virStorageIsRelative(parent->backingStoreRaw)) def =3D virStorageSourceNewFromBackingRelative(parent, @@ -3721,17 +3711,14 @@ virStorageSourceNewFromBacking(virStorageSourcePtr = parent) =20 /* copy parent's labelling and other top level stuff */ if (virStorageSourceInitChainElement(def, parent, true) < 0) - goto error; + return NULL; =20 def->readonly =3D true; def->detected =3D true; } =20 - return def; - - error: - virObjectUnref(def); - return NULL; + VIR_STEAL_PTR(ret, def); + return ret; } =20 =20 @@ -3872,9 +3859,8 @@ virStorageSourceUpdateCapacity(virStorageSourcePtr sr= c, ssize_t len, bool probe) { - int ret =3D -1; - virStorageSourcePtr meta =3D NULL; int format =3D src->format; + VIR_AUTOUNREF(virStorageSourcePtr) meta =3D NULL; =20 /* Raw files: capacity is physical size. For all other files: if * the metadata has a capacity, use that, otherwise fall back to @@ -3884,12 +3870,12 @@ virStorageSourceUpdateCapacity(virStorageSourcePtr = src, virReportError(VIR_ERR_INTERNAL_ERROR, _("no disk format for %s and probing is disable= d"), src->path); - goto cleanup; + return -1; } =20 if ((format =3D virStorageFileProbeFormatFromBuf(src->path, buf, len)) < 0) - goto cleanup; + return -1; =20 src->format =3D format; } @@ -3902,17 +3888,13 @@ virStorageSourceUpdateCapacity(virStorageSourcePtr = src, if (src->encryption && meta->encryption) src->encryption->payload_offset =3D meta->encryption->payload_= offset; } else { - goto cleanup; + return -1; } =20 if (src->encryption && src->encryption->payload_offset !=3D -1) src->capacity -=3D src->encryption->payload_offset * 512; =20 - ret =3D 0; - - cleanup: - virObjectUnref(meta); - return ret; + return 0; } =20 =20 @@ -4849,10 +4831,10 @@ virStorageFileGetMetadataRecurse(virStorageSourcePt= r src, int ret =3D -1; const char *uniqueName; ssize_t headerLen; - virStorageSourcePtr backingStore =3D NULL; int backingFormat; int rv; VIR_AUTOFREE(char *) buf =3D NULL; + VIR_AUTOUNREF(virStorageSourcePtr) backingStore =3D NULL; =20 VIR_DEBUG("path=3D%s format=3D%d uid=3D%u gid=3D%u", src->path, src->format, @@ -4935,7 +4917,6 @@ virStorageFileGetMetadataRecurse(virStorageSourcePtr = src, if (virStorageSourceHasBacking(src)) src->backingStore->id =3D depth; virStorageFileDeinit(src); - virObjectUnref(backingStore); return ret; } =20 @@ -5004,11 +4985,10 @@ int virStorageFileGetBackingStoreStr(virStorageSourcePtr src, char **backing) { - virStorageSourcePtr tmp =3D NULL; ssize_t headerLen; - int ret =3D -1; int rv; VIR_AUTOFREE(char *) buf =3D NULL; + VIR_AUTOUNREF(virStorageSourcePtr) tmp =3D NULL; =20 *backing =3D NULL; =20 @@ -5032,17 +5012,11 @@ virStorageFileGetBackingStoreStr(virStorageSourcePt= r src, } =20 if (!(tmp =3D virStorageSourceCopy(src, false))) - goto cleanup; + return -1; =20 if (virStorageFileGetMetadataInternal(tmp, buf, headerLen, NULL) < 0) - goto cleanup; + return -1; =20 VIR_STEAL_PTR(*backing, tmp->backingStoreRaw); - - ret =3D 0; - - cleanup: - virObjectUnref(tmp); - - return ret; + return 0; } --=20 2.20.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list