From nobody Sun Feb 8 18:48:55 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 1549981191423378.92617127100027; Tue, 12 Feb 2019 06:19:51 -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 D996C36807; Tue, 12 Feb 2019 14:19:47 +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 A698617CC0; Tue, 12 Feb 2019 14:19:47 +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 5C77C3F60F; Tue, 12 Feb 2019 14:19:47 +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 x1CEJjS8012998 for ; Tue, 12 Feb 2019 09:19:45 -0500 Received: by smtp.corp.redhat.com (Postfix) id 3F76D18A66; Tue, 12 Feb 2019 14:19:45 +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 E413918B23; Tue, 12 Feb 2019 14:19:35 +0000 (UTC) From: John Ferlan To: libvir-list@redhat.com Date: Tue, 12 Feb 2019 09:19:06 -0500 Message-Id: <20190212141906.28995-9-jferlan@redhat.com> In-Reply-To: <20190212141906.28995-1-jferlan@redhat.com> References: <20190212141906.28995-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: =?UTF-8?q?J=C3=A1n=20Tomko?= Subject: [libvirt] [PATCH v3 8/8] util: Introduce VIR_DEFINE_AUTOPTR_FUNC for virStorageSource 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-Type: text/plain; charset="utf-8" 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.30]); Tue, 12 Feb 2019 14:19:50 +0000 (UTC) Let's make use of the auto __cleanup capabilities cleaning up any now unnecessary goto paths. Signed-off-by: John Ferlan Reviewed-by: J=C3=A1n Tomko --- src/conf/domain_conf.c | 3 +- src/qemu/qemu_domain.c | 3 +- src/qemu/qemu_driver.c | 9 +- src/qemu/qemu_migration.c | 3 +- src/storage/storage_backend_gluster.c | 3 +- src/storage/storage_util.c | 25 ++--- src/util/virstoragefile.c | 141 +++++++++++--------------- src/util/virstoragefile.h | 1 + tests/qemublocktest.c | 6 +- tests/virstoragetest.c | 50 ++++----- 10 files changed, 97 insertions(+), 147 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 2d75849e3d..5d49f4388c 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -9065,13 +9065,13 @@ virDomainDiskBackingStoreParse(xmlXPathContextPtr c= txt, unsigned int flags, virDomainXMLOptionPtr xmlopt) { - virStorageSourcePtr backingStore =3D NULL; xmlNodePtr save_ctxt =3D ctxt->node; xmlNodePtr source; char *type =3D NULL; char *format =3D NULL; char *idx =3D NULL; int ret =3D -1; + VIR_AUTOPTR(virStorageSource) backingStore =3D NULL; =20 if (!(ctxt->node =3D virXPathNode("./backingStore", ctxt))) { ret =3D 0; @@ -9132,7 +9132,6 @@ virDomainDiskBackingStoreParse(xmlXPathContextPtr ctx= t, ret =3D 0; =20 cleanup: - virStorageSourceFree(backingStore); VIR_FREE(type); VIR_FREE(format); VIR_FREE(idx); diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index 801d25a44b..ac01e861f7 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -2730,10 +2730,10 @@ qemuDomainObjPrivateXMLParseJobNBDSource(xmlNodePtr= node, { xmlNodePtr savedNode =3D ctxt->node; qemuDomainDiskPrivatePtr diskPriv =3D QEMU_DOMAIN_DISK_PRIVATE(disk); - virStorageSourcePtr migrSource =3D NULL; char *format =3D NULL; char *type =3D NULL; int ret =3D -1; + VIR_AUTOPTR(virStorageSource) migrSource =3D NULL; =20 ctxt->node =3D node; =20 @@ -2781,7 +2781,6 @@ qemuDomainObjPrivateXMLParseJobNBDSource(xmlNodePtr n= ode, ret =3D 0; =20 cleanup: - virStorageSourceFree(migrSource); VIR_FREE(format); VIR_FREE(type); ctxt->node =3D savedNode; diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 1822248749..dc51de0310 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -274,11 +274,11 @@ qemuSecurityChownCallback(const virStorageSource *src, uid_t uid, gid_t gid) { - virStorageSourcePtr cpy =3D NULL; struct stat sb; int save_errno =3D 0; int ret =3D -1; int rv; + VIR_AUTOPTR(virStorageSource) cpy =3D NULL; =20 rv =3D virStorageFileSupportsSecurityDriver(src); if (rv <=3D 0) @@ -319,7 +319,6 @@ qemuSecurityChownCallback(const virStorageSource *src, cleanup: save_errno =3D errno; virStorageFileDeinit(cpy); - virStorageSourceFree(cpy); errno =3D save_errno; =20 return ret; @@ -17958,7 +17957,7 @@ qemuDomainBlockRebase(virDomainPtr dom, const char = *path, const char *base, virDomainObjPtr vm; int ret =3D -1; unsigned long long speed =3D bandwidth; - virStorageSourcePtr dest =3D NULL; + VIR_AUTOPTR(virStorageSource) dest =3D NULL; =20 virCheckFlags(VIR_DOMAIN_BLOCK_REBASE_SHALLOW | VIR_DOMAIN_BLOCK_REBASE_REUSE_EXT | @@ -18020,7 +18019,6 @@ qemuDomainBlockRebase(virDomainPtr dom, const char = *path, const char *base, =20 cleanup: virDomainObjEndAPI(&vm); - virStorageSourceFree(dest); return ret; } =20 @@ -18150,10 +18148,10 @@ qemuDomainBlockCommit(virDomainPtr dom, char *topPath =3D NULL; char *basePath =3D NULL; char *backingPath =3D NULL; - virStorageSourcePtr mirror =3D NULL; unsigned long long speed =3D bandwidth; qemuBlockJobDataPtr job =3D NULL; qemuBlockJobType jobtype =3D QEMU_BLOCKJOB_TYPE_COMMIT; + VIR_AUTOPTR(virStorageSource) mirror =3D NULL; =20 /* XXX Add support for COMMIT_DELETE */ virCheckFlags(VIR_DOMAIN_BLOCK_COMMIT_SHALLOW | @@ -18352,7 +18350,6 @@ qemuDomainBlockCommit(virDomainPtr dom, virFreeError(orig_err); } } - virStorageSourceFree(mirror); qemuBlockJobStartupFinalize(job); qemuDomainObjEndJob(driver, vm); =20 diff --git a/src/qemu/qemu_migration.c b/src/qemu/qemu_migration.c index 3107a279dd..c93ae33476 100644 --- a/src/qemu/qemu_migration.c +++ b/src/qemu/qemu_migration.c @@ -788,9 +788,9 @@ qemuMigrationSrcNBDStorageCopyBlockdev(virQEMUDriverPtr= driver, { qemuBlockStorageSourceAttachDataPtr data =3D NULL; qemuDomainDiskPrivatePtr diskPriv =3D QEMU_DOMAIN_DISK_PRIVATE(disk); - virStorageSourcePtr copysrc =3D NULL; int mon_ret =3D 0; int ret =3D -1; + VIR_AUTOPTR(virStorageSource) copysrc =3D NULL; =20 VIR_DEBUG("starting blockdev mirror for disk=3D%s to host=3D%s", diskA= lias, host); =20 @@ -849,7 +849,6 @@ qemuMigrationSrcNBDStorageCopyBlockdev(virQEMUDriverPtr= driver, =20 cleanup: qemuBlockStorageSourceAttachDataFree(data); - virStorageSourceFree(copysrc); return ret; } =20 diff --git a/src/storage/storage_backend_gluster.c b/src/storage/storage_ba= ckend_gluster.c index 1888314d95..846a647cb6 100644 --- a/src/storage/storage_backend_gluster.c +++ b/src/storage/storage_backend_gluster.c @@ -236,10 +236,10 @@ virStorageBackendGlusterRefreshVol(virStorageBackendG= lusterStatePtr state, { int ret =3D -1; glfs_fd_t *fd =3D NULL; - virStorageSourcePtr meta =3D NULL; ssize_t len; int backingFormat; VIR_AUTOPTR(virStorageVolDef) vol =3D NULL; + VIR_AUTOPTR(virStorageSource) meta =3D NULL; VIR_AUTOFREE(char *) header =3D NULL; =20 *volptr =3D NULL; @@ -323,7 +323,6 @@ virStorageBackendGlusterRefreshVol(virStorageBackendGlu= sterStatePtr state, VIR_STEAL_PTR(*volptr, vol); ret =3D 0; cleanup: - virStorageSourceFree(meta); if (fd) glfs_close(fd); return ret; diff --git a/src/storage/storage_util.c b/src/storage/storage_util.c index 60a42a2828..f18e38733a 100644 --- a/src/storage/storage_util.c +++ b/src/storage/storage_util.c @@ -3357,10 +3357,9 @@ storageBackendProbeTarget(virStorageSourcePtr target, virStorageEncryptionPtr *encryption) { int backingStoreFormat; - int ret =3D -1; int rc; - virStorageSourcePtr meta =3D NULL; struct stat sb; + VIR_AUTOPTR(virStorageSource) meta =3D NULL; VIR_AUTOCLOSE fd =3D -1; =20 if (encryption) @@ -3372,17 +3371,16 @@ storageBackendProbeTarget(virStorageSourcePtr targe= t, fd =3D rc; =20 if (virStorageBackendUpdateVolTargetInfoFD(target, fd, &sb) < 0) - goto cleanup; + return -1; =20 if (S_ISDIR(sb.st_mode)) { if (storageBackendIsPloopDir(target->path)) { if (storageBackendRedoPloopUpdate(target, &sb, &fd, VIR_STORAGE_VOL_FS_PROBE_FLA= GS) < 0) - goto cleanup; + return -1; } else { target->format =3D VIR_STORAGE_FILE_DIR; - ret =3D 0; - goto cleanup; + return 0; } } =20 @@ -3390,11 +3388,11 @@ storageBackendProbeTarget(virStorageSourcePtr targe= t, fd, VIR_STORAGE_FILE_AUTO, &backingStoreFormat))) - goto cleanup; + return -1; =20 if (meta->backingStoreRaw) { if (!(target->backingStore =3D virStorageSourceNewFromBacking(meta= ))) - goto cleanup; + return -1; =20 target->backingStore->format =3D backingStoreFormat; =20 @@ -3405,7 +3403,7 @@ storageBackendProbeTarget(virStorageSourcePtr target, virStorageSourceFree(target->backingStore); =20 if (VIR_ALLOC(target->backingStore) < 0) - goto cleanup; + return -1; =20 target->backingStore->type =3D VIR_STORAGE_TYPE_NETWORK; target->backingStore->path =3D meta->backingStoreRaw; @@ -3434,8 +3432,6 @@ storageBackendProbeTarget(virStorageSourcePtr target, target->format =3D meta->format; =20 /* Default to success below this point */ - ret =3D 0; - if (meta->capacity) target->capacity =3D meta->capacity; =20 @@ -3461,9 +3457,7 @@ storageBackendProbeTarget(virStorageSourcePtr target, VIR_STEAL_PTR(target->compat, meta->compat); } =20 - cleanup: - virStorageSourceFree(meta); - return ret; + return 0; } =20 =20 @@ -3531,11 +3525,11 @@ virStorageBackendRefreshLocal(virStoragePoolObjPtr = pool) struct dirent *ent; struct statvfs sb; struct stat statbuf; - virStorageSourcePtr target =3D NULL; int direrr; int ret =3D -1; VIR_AUTOPTR(virStorageVolDef) vol =3D NULL; VIR_AUTOCLOSE fd =3D -1; + VIR_AUTOPTR(virStorageSource) target =3D NULL; =20 if (virDirOpen(&dir, def->target.path) < 0) goto cleanup; @@ -3626,7 +3620,6 @@ virStorageBackendRefreshLocal(virStoragePoolObjPtr po= ol) ret =3D 0; cleanup: VIR_DIR_CLOSE(dir); - virStorageSourceFree(target); if (ret < 0) virStoragePoolObjClearVols(pool); return ret; diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c index 8d85d9dac4..8eccc428da 100644 --- a/src/util/virstoragefile.c +++ b/src/util/virstoragefile.c @@ -1117,7 +1117,8 @@ static virStorageSourcePtr virStorageFileMetadataNew(const char *path, int format) { - virStorageSourcePtr def =3D NULL; + virStorageSourcePtr ret =3D NULL; + VIR_AUTOPTR(virStorageSource) def =3D NULL; =20 if (VIR_ALLOC(def) < 0) return NULL; @@ -1126,13 +1127,10 @@ virStorageFileMetadataNew(const char *path, def->type =3D VIR_STORAGE_TYPE_FILE; =20 if (VIR_STRDUP(def->path, path) < 0) - goto error; - - return def; + return NULL; =20 - error: - virStorageSourceFree(def); - return NULL; + VIR_STEAL_PTR(ret, def); + return ret; } =20 =20 @@ -1205,11 +1203,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_AUTOPTR(virStorageSource) meta =3D NULL; =20 if (!backingFormat) backingFormat =3D &dummy; @@ -1231,21 +1229,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 +1251,6 @@ virStorageFileGetMetadataFromFD(const char *path, meta->type =3D VIR_STORAGE_TYPE_BLOCK; =20 VIR_STEAL_PTR(ret, meta); - - cleanup: - virStorageSourceFree(meta); return ret; } =20 @@ -2243,7 +2238,8 @@ virStorageSourcePtr virStorageSourceCopy(const virStorageSource *src, bool backingChain) { - virStorageSourcePtr def =3D NULL; + virStorageSourcePtr ret =3D NULL; + VIR_AUTOPTR(virStorageSource) def =3D NULL; =20 if (VIR_ALLOC(def) < 0) return NULL; @@ -2282,60 +2278,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: - virStorageSourceFree(def); - return NULL; + VIR_STEAL_PTR(ret, def); + return ret; } =20 =20 @@ -2577,27 +2570,28 @@ static virStorageSourcePtr virStorageSourceNewFromBackingRelative(virStorageSourcePtr parent, const char *rel) { - virStorageSourcePtr def; + virStorageSourcePtr ret =3D NULL; VIR_AUTOFREE(char *) dirname =3D NULL; + VIR_AUTOPTR(virStorageSource) def =3D NULL; =20 if (VIR_ALLOC(def) < 0) 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) { @@ -2608,25 +2602,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: - virStorageSourceFree(def); - def =3D NULL; - goto cleanup; + VIR_STEAL_PTR(ret, def); + return ret; } =20 =20 @@ -3624,8 +3613,9 @@ virStorageSourcePtr virStorageSourceNewFromBackingAbsolute(const char *path) { const char *json; - virStorageSourcePtr def; + virStorageSourcePtr ret =3D NULL; int rc; + VIR_AUTOPTR(virStorageSource) def =3D NULL; =20 if (VIR_ALLOC(def) < 0) return NULL; @@ -3634,7 +3624,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 @@ -3649,7 +3639,7 @@ virStorageSourceNewFromBackingAbsolute(const char *pa= th) rc =3D virStorageSourceParseBackingColon(def, path); =20 if (rc < 0) - goto error; + return NULL; =20 virStorageSourceNetworkAssignDefaultPorts(def); =20 @@ -3662,11 +3652,8 @@ virStorageSourceNewFromBackingAbsolute(const char *p= ath) } } =20 - return def; - - error: - virStorageSourceFree(def); - return NULL; + VIR_STEAL_PTR(ret, def); + return ret; } =20 =20 @@ -3674,7 +3661,8 @@ virStorageSourcePtr virStorageSourceNewFromBacking(virStorageSourcePtr parent) { struct stat st; - virStorageSourcePtr def; + virStorageSourcePtr ret =3D NULL; + VIR_AUTOPTR(virStorageSource) def =3D NULL; =20 if (virStorageIsRelative(parent->backingStoreRaw)) def =3D virStorageSourceNewFromBackingRelative(parent, @@ -3697,17 +3685,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: - virStorageSourceFree(def); - return NULL; + VIR_STEAL_PTR(ret, def); + return ret; } =20 =20 @@ -3848,9 +3833,8 @@ virStorageSourceUpdateCapacity(virStorageSourcePtr sr= c, ssize_t len, bool probe) { - int ret =3D -1; - virStorageSourcePtr meta =3D NULL; int format =3D src->format; + VIR_AUTOPTR(virStorageSource) 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 @@ -3860,12 +3844,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; } @@ -3878,17 +3862,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: - virStorageSourceFree(meta); - return ret; + return 0; } =20 =20 @@ -4825,10 +4805,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_AUTOPTR(virStorageSource) backingStore =3D NULL; =20 VIR_DEBUG("path=3D%s format=3D%d uid=3D%u gid=3D%u", src->path, src->format, @@ -4911,7 +4891,6 @@ virStorageFileGetMetadataRecurse(virStorageSourcePtr = src, if (virStorageSourceHasBacking(src)) src->backingStore->id =3D depth; virStorageFileDeinit(src); - virStorageSourceFree(backingStore); return ret; } =20 @@ -4980,11 +4959,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_AUTOPTR(virStorageSource) tmp =3D NULL; =20 *backing =3D NULL; =20 @@ -5008,17 +4986,12 @@ 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); =20 - ret =3D 0; - - cleanup: - virStorageSourceFree(tmp); - - return ret; + return 0; } diff --git a/src/util/virstoragefile.h b/src/util/virstoragefile.h index eacc927ea6..8c3a36d473 100644 --- a/src/util/virstoragefile.h +++ b/src/util/virstoragefile.h @@ -544,5 +544,6 @@ void virStorageFileReportBrokenChain(int errcode, virStorageSourcePtr parent); =20 VIR_DEFINE_AUTOPTR_FUNC(virStorageAuthDef, virStorageAuthDefFree); +VIR_DEFINE_AUTOPTR_FUNC(virStorageSource, virStorageSourceFree); =20 #endif /* LIBVIRT_VIRSTORAGEFILE_H */ diff --git a/tests/qemublocktest.c b/tests/qemublocktest.c index 5848f6b5b5..d7e5e72a0b 100644 --- a/tests/qemublocktest.c +++ b/tests/qemublocktest.c @@ -46,14 +46,14 @@ testBackingXMLjsonXML(const void *args) xmlDocPtr xml =3D NULL; xmlXPathContextPtr ctxt =3D NULL; virBuffer buf =3D VIR_BUFFER_INITIALIZER; - virStorageSourcePtr xmlsrc =3D NULL; - virStorageSourcePtr jsonsrc =3D NULL; virJSONValuePtr backendprops =3D NULL; virJSONValuePtr wrapper =3D NULL; char *propsstr =3D NULL; char *protocolwrapper =3D NULL; char *actualxml =3D NULL; int ret =3D -1; + VIR_AUTOPTR(virStorageSource) xmlsrc =3D NULL; + VIR_AUTOPTR(virStorageSource) jsonsrc =3D NULL; =20 if (VIR_ALLOC(xmlsrc) < 0) return -1; @@ -104,8 +104,6 @@ testBackingXMLjsonXML(const void *args) ret =3D 0; =20 cleanup: - virStorageSourceFree(xmlsrc); - virStorageSourceFree(jsonsrc); VIR_FREE(propsstr); VIR_FREE(protocolwrapper); VIR_FREE(actualxml); diff --git a/tests/virstoragetest.c b/tests/virstoragetest.c index 8db1d294b0..646ae78ff0 100644 --- a/tests/virstoragetest.c +++ b/tests/virstoragetest.c @@ -95,7 +95,8 @@ testStorageFileGetMetadata(const char *path, uid_t uid, gid_t gid) { struct stat st; - virStorageSourcePtr def =3D NULL; + virStorageSourcePtr ret =3D NULL; + VIR_AUTOPTR(virStorageSource) def =3D NULL; =20 if (VIR_ALLOC(def) < 0) return NULL; @@ -112,16 +113,13 @@ testStorageFileGetMetadata(const char *path, } =20 if (VIR_STRDUP(def->path, path) < 0) - goto error; + return NULL; =20 if (virStorageFileGetMetadata(def, uid, gid, false) < 0) - goto error; - - return def; + return NULL; =20 - error: - virStorageSourceFree(def); - return NULL; + VIR_STEAL_PTR(ret, def); + return ret; } =20 static int @@ -308,41 +306,40 @@ static int testStorageChain(const void *args) { const struct testChainData *data =3D args; - int ret =3D -1; - virStorageSourcePtr meta; virStorageSourcePtr elt; size_t i =3D 0; + VIR_AUTOPTR(virStorageSource) meta =3D NULL; VIR_AUTOFREE(char *) broken =3D NULL; =20 meta =3D testStorageFileGetMetadata(data->start, data->format, -1, -1); if (!meta) { if (data->flags & EXP_FAIL) { virResetLastError(); - ret =3D 0; + return 0; } - goto cleanup; + return -1; } else if (data->flags & EXP_FAIL) { fprintf(stderr, "call should have failed\n"); - goto cleanup; + return -1; } if (data->flags & EXP_WARN) { if (virGetLastErrorCode() =3D=3D VIR_ERR_OK) { fprintf(stderr, "call should have warned\n"); - goto cleanup; + return -1; } virResetLastError(); if (virStorageFileChainGetBroken(meta, &broken) || !broken) { fprintf(stderr, "call should identify broken part of chain\n"); - goto cleanup; + return -1; } } else { if (virGetLastErrorCode()) { fprintf(stderr, "call should not have warned\n"); - goto cleanup; + return -1; } if (virStorageFileChainGetBroken(meta, &broken) || broken) { fprintf(stderr, "chain should not be identified as broken\n"); - goto cleanup; + return -1; } } =20 @@ -353,7 +350,7 @@ testStorageChain(const void *args) =20 if (i =3D=3D data->nfiles) { fprintf(stderr, "probed chain was too long\n"); - goto cleanup; + return -1; } =20 if (virAsprintf(&expect, @@ -378,24 +375,21 @@ testStorageChain(const void *args) elt->format, virStorageNetProtocolTypeToString(elt->protocol), NULLSTR(elt->nhosts ? elt->hosts[0].name : NULL)) = < 0) { - goto cleanup; + return -1; } if (STRNEQ(expect, actual)) { virTestDifference(stderr, expect, actual); - goto cleanup; + return -1; } elt =3D elt->backingStore; i++; } if (i !=3D data->nfiles) { fprintf(stderr, "probed chain was too short\n"); - goto cleanup; + return -1; } =20 - ret =3D 0; - cleanup: - virStorageSourceFree(meta); - return ret; + return 0; } =20 struct testLookupData @@ -646,9 +640,9 @@ testBackingParse(const void *args) { const struct testBackingParseData *data =3D args; virBuffer buf =3D VIR_BUFFER_INITIALIZER; - virStorageSourcePtr src =3D NULL; int ret =3D -1; VIR_AUTOFREE(char *) xml =3D NULL; + VIR_AUTOPTR(virStorageSource) src =3D NULL; =20 if (!(src =3D virStorageSourceNewFromBackingAbsolute(data->backing))) { if (!data->expect) @@ -680,7 +674,6 @@ testBackingParse(const void *args) ret =3D 0; =20 cleanup: - virStorageSourceFree(src); virBufferFreeAndReset(&buf); =20 return ret; @@ -696,10 +689,10 @@ mymain(void) struct testPathCanonicalizeData data3; struct testPathRelativeBacking data4; struct testBackingParseData data5; - virStorageSourcePtr chain =3D NULL; virStorageSourcePtr chain2; /* short for chain->backingStore */ virStorageSourcePtr chain3; /* short for chain2->backingStore */ VIR_AUTOPTR(virCommand) cmd =3D NULL; + VIR_AUTOPTR(virStorageSource) chain =3D NULL; =20 if (storageRegisterAll() < 0) return EXIT_FAILURE; @@ -1580,7 +1573,6 @@ mymain(void) =20 cleanup: /* Final cleanup */ - virStorageSourceFree(chain); testCleanupImages(); =20 return ret =3D=3D 0 ? EXIT_SUCCESS : EXIT_FAILURE; --=20 2.20.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list