[PATCH v3 8/8] util: Introduce VIR_DEFINE_AUTOPTR_FUNC for virStorageSource

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Let's make use of the auto __cleanup capabilities cleaning up any
now unnecessary goto paths.

Signed-off-by: John Ferlan <jferlan@xxxxxxxxxx>
Reviewed-by: Ján Tomko <jtomko@xxxxxxxxxx>
---
 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 ctxt,
                                unsigned int flags,
                                virDomainXMLOptionPtr xmlopt)
 {
-    virStorageSourcePtr backingStore = NULL;
     xmlNodePtr save_ctxt = ctxt->node;
     xmlNodePtr source;
     char *type = NULL;
     char *format = NULL;
     char *idx = NULL;
     int ret = -1;
+    VIR_AUTOPTR(virStorageSource) backingStore = NULL;
 
     if (!(ctxt->node = virXPathNode("./backingStore", ctxt))) {
         ret = 0;
@@ -9132,7 +9132,6 @@ virDomainDiskBackingStoreParse(xmlXPathContextPtr ctxt,
     ret = 0;
 
  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 = ctxt->node;
     qemuDomainDiskPrivatePtr diskPriv = QEMU_DOMAIN_DISK_PRIVATE(disk);
-    virStorageSourcePtr migrSource = NULL;
     char *format = NULL;
     char *type = NULL;
     int ret = -1;
+    VIR_AUTOPTR(virStorageSource) migrSource = NULL;
 
     ctxt->node = node;
 
@@ -2781,7 +2781,6 @@ qemuDomainObjPrivateXMLParseJobNBDSource(xmlNodePtr node,
     ret = 0;
 
  cleanup:
-    virStorageSourceFree(migrSource);
     VIR_FREE(format);
     VIR_FREE(type);
     ctxt->node = 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 = NULL;
     struct stat sb;
     int save_errno = 0;
     int ret = -1;
     int rv;
+    VIR_AUTOPTR(virStorageSource) cpy = NULL;
 
     rv = virStorageFileSupportsSecurityDriver(src);
     if (rv <= 0)
@@ -319,7 +319,6 @@ qemuSecurityChownCallback(const virStorageSource *src,
  cleanup:
     save_errno = errno;
     virStorageFileDeinit(cpy);
-    virStorageSourceFree(cpy);
     errno = save_errno;
 
     return ret;
@@ -17958,7 +17957,7 @@ qemuDomainBlockRebase(virDomainPtr dom, const char *path, const char *base,
     virDomainObjPtr vm;
     int ret = -1;
     unsigned long long speed = bandwidth;
-    virStorageSourcePtr dest = NULL;
+    VIR_AUTOPTR(virStorageSource) dest = NULL;
 
     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,
 
  cleanup:
     virDomainObjEndAPI(&vm);
-    virStorageSourceFree(dest);
     return ret;
 }
 
@@ -18150,10 +18148,10 @@ qemuDomainBlockCommit(virDomainPtr dom,
     char *topPath = NULL;
     char *basePath = NULL;
     char *backingPath = NULL;
-    virStorageSourcePtr mirror = NULL;
     unsigned long long speed = bandwidth;
     qemuBlockJobDataPtr job = NULL;
     qemuBlockJobType jobtype = QEMU_BLOCKJOB_TYPE_COMMIT;
+    VIR_AUTOPTR(virStorageSource) mirror = NULL;
 
     /* 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);
 
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 = NULL;
     qemuDomainDiskPrivatePtr diskPriv = QEMU_DOMAIN_DISK_PRIVATE(disk);
-    virStorageSourcePtr copysrc = NULL;
     int mon_ret = 0;
     int ret = -1;
+    VIR_AUTOPTR(virStorageSource) copysrc = NULL;
 
     VIR_DEBUG("starting blockdev mirror for disk=%s to host=%s", diskAlias, host);
 
@@ -849,7 +849,6 @@ qemuMigrationSrcNBDStorageCopyBlockdev(virQEMUDriverPtr driver,
 
  cleanup:
     qemuBlockStorageSourceAttachDataFree(data);
-    virStorageSourceFree(copysrc);
     return ret;
 }
 
diff --git a/src/storage/storage_backend_gluster.c b/src/storage/storage_backend_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(virStorageBackendGlusterStatePtr state,
 {
     int ret = -1;
     glfs_fd_t *fd = NULL;
-    virStorageSourcePtr meta = NULL;
     ssize_t len;
     int backingFormat;
     VIR_AUTOPTR(virStorageVolDef) vol = NULL;
+    VIR_AUTOPTR(virStorageSource) meta = NULL;
     VIR_AUTOFREE(char *) header = NULL;
 
     *volptr = NULL;
@@ -323,7 +323,6 @@ virStorageBackendGlusterRefreshVol(virStorageBackendGlusterStatePtr state,
     VIR_STEAL_PTR(*volptr, vol);
     ret = 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 = -1;
     int rc;
-    virStorageSourcePtr meta = NULL;
     struct stat sb;
+    VIR_AUTOPTR(virStorageSource) meta = NULL;
     VIR_AUTOCLOSE fd = -1;
 
     if (encryption)
@@ -3372,17 +3371,16 @@ storageBackendProbeTarget(virStorageSourcePtr target,
     fd = rc;
 
     if (virStorageBackendUpdateVolTargetInfoFD(target, fd, &sb) < 0)
-        goto cleanup;
+        return -1;
 
     if (S_ISDIR(sb.st_mode)) {
         if (storageBackendIsPloopDir(target->path)) {
             if (storageBackendRedoPloopUpdate(target, &sb, &fd,
                                               VIR_STORAGE_VOL_FS_PROBE_FLAGS) < 0)
-                goto cleanup;
+                return -1;
         } else {
             target->format = VIR_STORAGE_FILE_DIR;
-            ret = 0;
-            goto cleanup;
+            return 0;
         }
     }
 
@@ -3390,11 +3388,11 @@ storageBackendProbeTarget(virStorageSourcePtr target,
                                                  fd,
                                                  VIR_STORAGE_FILE_AUTO,
                                                  &backingStoreFormat)))
-        goto cleanup;
+        return -1;
 
     if (meta->backingStoreRaw) {
         if (!(target->backingStore = virStorageSourceNewFromBacking(meta)))
-            goto cleanup;
+            return -1;
 
         target->backingStore->format = backingStoreFormat;
 
@@ -3405,7 +3403,7 @@ storageBackendProbeTarget(virStorageSourcePtr target,
             virStorageSourceFree(target->backingStore);
 
             if (VIR_ALLOC(target->backingStore) < 0)
-                goto cleanup;
+                return -1;
 
             target->backingStore->type = VIR_STORAGE_TYPE_NETWORK;
             target->backingStore->path = meta->backingStoreRaw;
@@ -3434,8 +3432,6 @@ storageBackendProbeTarget(virStorageSourcePtr target,
     target->format = meta->format;
 
     /* Default to success below this point */
-    ret = 0;
-
     if (meta->capacity)
         target->capacity = meta->capacity;
 
@@ -3461,9 +3457,7 @@ storageBackendProbeTarget(virStorageSourcePtr target,
         VIR_STEAL_PTR(target->compat, meta->compat);
     }
 
- cleanup:
-    virStorageSourceFree(meta);
-    return ret;
+    return 0;
 }
 
 
@@ -3531,11 +3525,11 @@ virStorageBackendRefreshLocal(virStoragePoolObjPtr pool)
     struct dirent *ent;
     struct statvfs sb;
     struct stat statbuf;
-    virStorageSourcePtr target = NULL;
     int direrr;
     int ret = -1;
     VIR_AUTOPTR(virStorageVolDef) vol = NULL;
     VIR_AUTOCLOSE fd = -1;
+    VIR_AUTOPTR(virStorageSource) target = NULL;
 
     if (virDirOpen(&dir, def->target.path) < 0)
         goto cleanup;
@@ -3626,7 +3620,6 @@ virStorageBackendRefreshLocal(virStoragePoolObjPtr pool)
     ret = 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 = NULL;
+    virStorageSourcePtr ret = NULL;
+    VIR_AUTOPTR(virStorageSource) def = NULL;
 
     if (VIR_ALLOC(def) < 0)
         return NULL;
@@ -1126,13 +1127,10 @@ virStorageFileMetadataNew(const char *path,
     def->type = VIR_STORAGE_TYPE_FILE;
 
     if (VIR_STRDUP(def->path, path) < 0)
-        goto error;
-
-    return def;
+        return NULL;
 
- error:
-    virStorageSourceFree(def);
-    return NULL;
+    VIR_STEAL_PTR(ret, def);
+    return ret;
 }
 
 
@@ -1205,11 +1203,11 @@ virStorageFileGetMetadataFromFD(const char *path,
 
 {
     virStorageSourcePtr ret = NULL;
-    virStorageSourcePtr meta = NULL;
     ssize_t len = VIR_STORAGE_MAX_HEADER;
     struct stat sb;
     int dummy;
     VIR_AUTOFREE(char *) buf = NULL;
+    VIR_AUTOPTR(virStorageSource) meta = NULL;
 
     if (!backingFormat)
         backingFormat = &dummy;
@@ -1231,21 +1229,21 @@ virStorageFileGetMetadataFromFD(const char *path,
         meta->type = VIR_STORAGE_TYPE_DIR;
         meta->format = VIR_STORAGE_FILE_DIR;
         VIR_STEAL_PTR(ret, meta);
-        goto cleanup;
+        return ret;
     }
 
     if (lseek(fd, 0, SEEK_SET) == (off_t)-1) {
         virReportSystemError(errno, _("cannot seek to start of '%s'"), meta->path);
-        goto cleanup;
+        return NULL;
     }
 
     if ((len = virFileReadHeaderFD(fd, len, &buf)) < 0) {
         virReportSystemError(errno, _("cannot read header '%s'"), meta->path);
-        goto cleanup;
+        return NULL;
     }
 
     if (virStorageFileGetMetadataInternal(meta, buf, len, backingFormat) < 0)
-        goto cleanup;
+        return NULL;
 
     if (S_ISREG(sb.st_mode))
         meta->type = VIR_STORAGE_TYPE_FILE;
@@ -1253,9 +1251,6 @@ virStorageFileGetMetadataFromFD(const char *path,
         meta->type = VIR_STORAGE_TYPE_BLOCK;
 
     VIR_STEAL_PTR(ret, meta);
-
- cleanup:
-    virStorageSourceFree(meta);
     return ret;
 }
 
@@ -2243,7 +2238,8 @@ virStorageSourcePtr
 virStorageSourceCopy(const virStorageSource *src,
                      bool backingChain)
 {
-    virStorageSourcePtr def = NULL;
+    virStorageSourcePtr ret = NULL;
+    VIR_AUTOPTR(virStorageSource) def = NULL;
 
     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;
 
     if (src->nhosts) {
         if (!(def->hosts = virStorageNetHostDefCopy(src->nhosts, src->hosts)))
-            goto error;
+            return NULL;
 
         def->nhosts = src->nhosts;
     }
 
     if (src->srcpool &&
         !(def->srcpool = virStorageSourcePoolDefCopy(src->srcpool)))
-        goto error;
+        return NULL;
 
     if (src->features &&
         !(def->features = virBitmapNewCopy(src->features)))
-        goto error;
+        return NULL;
 
     if (src->encryption &&
         !(def->encryption = virStorageEncryptionCopy(src->encryption)))
-        goto error;
+        return NULL;
 
     if (src->perms &&
         !(def->perms = virStoragePermsCopy(src->perms)))
-        goto error;
+        return NULL;
 
     if (src->timestamps &&
         !(def->timestamps = virStorageTimestampsCopy(src->timestamps)))
-        goto error;
+        return NULL;
 
     if (virStorageSourceSeclabelsCopy(def, src) < 0)
-        goto error;
+        return NULL;
 
     if (src->auth &&
         !(def->auth = virStorageAuthDefCopy(src->auth)))
-        goto error;
+        return NULL;
 
     if (src->pr &&
         !(def->pr = virStoragePRDefCopy(src->pr)))
-        goto error;
+        return NULL;
 
     if (virStorageSourceInitiatorCopy(&def->initiator, &src->initiator))
-        goto error;
+        return NULL;
 
     if (backingChain && src->backingStore) {
         if (!(def->backingStore = virStorageSourceCopy(src->backingStore,
                                                        true)))
-            goto error;
+            return NULL;
     }
 
-    return def;
-
- error:
-    virStorageSourceFree(def);
-    return NULL;
+    VIR_STEAL_PTR(ret, def);
+    return ret;
 }
 
 
@@ -2577,27 +2570,28 @@ static virStorageSourcePtr
 virStorageSourceNewFromBackingRelative(virStorageSourcePtr parent,
                                        const char *rel)
 {
-    virStorageSourcePtr def;
+    virStorageSourcePtr ret = NULL;
     VIR_AUTOFREE(char *) dirname = NULL;
+    VIR_AUTOPTR(virStorageSource) def = NULL;
 
     if (VIR_ALLOC(def) < 0)
         return NULL;
 
     /* store relative name */
     if (VIR_STRDUP(def->relPath, parent->backingStoreRaw) < 0)
-        goto error;
+        return NULL;
 
     if (!(dirname = mdir_name(parent->path))) {
         virReportOOMError();
-        goto error;
+        return NULL;
     }
 
     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;
     }
 
     if (virStorageSourceGetActualType(parent) == VIR_STORAGE_TYPE_NETWORK) {
@@ -2608,25 +2602,20 @@ virStorageSourceNewFromBackingRelative(virStorageSourcePtr parent,
         if (parent->nhosts) {
             if (!(def->hosts = virStorageNetHostDefCopy(parent->nhosts,
                                                         parent->hosts)))
-                goto error;
+                return NULL;
 
             def->nhosts = parent->nhosts;
         }
 
         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 = VIR_STORAGE_TYPE_FILE;
     }
 
- cleanup:
-    return def;
-
- error:
-    virStorageSourceFree(def);
-    def = NULL;
-    goto cleanup;
+    VIR_STEAL_PTR(ret, def);
+    return ret;
 }
 
 
@@ -3624,8 +3613,9 @@ virStorageSourcePtr
 virStorageSourceNewFromBackingAbsolute(const char *path)
 {
     const char *json;
-    virStorageSourcePtr def;
+    virStorageSourcePtr ret = NULL;
     int rc;
+    VIR_AUTOPTR(virStorageSource) def = NULL;
 
     if (VIR_ALLOC(def) < 0)
         return NULL;
@@ -3634,7 +3624,7 @@ virStorageSourceNewFromBackingAbsolute(const char *path)
         def->type = VIR_STORAGE_TYPE_FILE;
 
         if (VIR_STRDUP(def->path, path) < 0)
-            goto error;
+            return NULL;
     } else {
         def->type = VIR_STORAGE_TYPE_NETWORK;
 
@@ -3649,7 +3639,7 @@ virStorageSourceNewFromBackingAbsolute(const char *path)
             rc = virStorageSourceParseBackingColon(def, path);
 
         if (rc < 0)
-            goto error;
+            return NULL;
 
         virStorageSourceNetworkAssignDefaultPorts(def);
 
@@ -3662,11 +3652,8 @@ virStorageSourceNewFromBackingAbsolute(const char *path)
         }
     }
 
-    return def;
-
- error:
-    virStorageSourceFree(def);
-    return NULL;
+    VIR_STEAL_PTR(ret, def);
+    return ret;
 }
 
 
@@ -3674,7 +3661,8 @@ virStorageSourcePtr
 virStorageSourceNewFromBacking(virStorageSourcePtr parent)
 {
     struct stat st;
-    virStorageSourcePtr def;
+    virStorageSourcePtr ret = NULL;
+    VIR_AUTOPTR(virStorageSource) def = NULL;
 
     if (virStorageIsRelative(parent->backingStoreRaw))
         def = virStorageSourceNewFromBackingRelative(parent,
@@ -3697,17 +3685,14 @@ virStorageSourceNewFromBacking(virStorageSourcePtr parent)
 
         /* copy parent's labelling and other top level stuff */
         if (virStorageSourceInitChainElement(def, parent, true) < 0)
-            goto error;
+            return NULL;
 
         def->readonly = true;
         def->detected = true;
     }
 
-    return def;
-
- error:
-    virStorageSourceFree(def);
-    return NULL;
+    VIR_STEAL_PTR(ret, def);
+    return ret;
 }
 
 
@@ -3848,9 +3833,8 @@ virStorageSourceUpdateCapacity(virStorageSourcePtr src,
                                ssize_t len,
                                bool probe)
 {
-    int ret = -1;
-    virStorageSourcePtr meta = NULL;
     int format = src->format;
+    VIR_AUTOPTR(virStorageSource) meta = NULL;
 
     /* 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 disabled"),
                            src->path);
-            goto cleanup;
+            return -1;
         }
 
         if ((format = virStorageFileProbeFormatFromBuf(src->path,
                                                        buf, len)) < 0)
-            goto cleanup;
+            return -1;
 
         src->format = format;
     }
@@ -3878,17 +3862,13 @@ virStorageSourceUpdateCapacity(virStorageSourcePtr src,
         if (src->encryption && meta->encryption)
             src->encryption->payload_offset = meta->encryption->payload_offset;
     } else {
-        goto cleanup;
+        return -1;
     }
 
     if (src->encryption && src->encryption->payload_offset != -1)
         src->capacity -= src->encryption->payload_offset * 512;
 
-    ret = 0;
-
- cleanup:
-    virStorageSourceFree(meta);
-    return ret;
+    return 0;
 }
 
 
@@ -4825,10 +4805,10 @@ virStorageFileGetMetadataRecurse(virStorageSourcePtr src,
     int ret = -1;
     const char *uniqueName;
     ssize_t headerLen;
-    virStorageSourcePtr backingStore = NULL;
     int backingFormat;
     int rv;
     VIR_AUTOFREE(char *) buf = NULL;
+    VIR_AUTOPTR(virStorageSource) backingStore = NULL;
 
     VIR_DEBUG("path=%s format=%d uid=%u gid=%u",
               src->path, src->format,
@@ -4911,7 +4891,6 @@ virStorageFileGetMetadataRecurse(virStorageSourcePtr src,
     if (virStorageSourceHasBacking(src))
         src->backingStore->id = depth;
     virStorageFileDeinit(src);
-    virStorageSourceFree(backingStore);
     return ret;
 }
 
@@ -4980,11 +4959,10 @@ int
 virStorageFileGetBackingStoreStr(virStorageSourcePtr src,
                                  char **backing)
 {
-    virStorageSourcePtr tmp = NULL;
     ssize_t headerLen;
-    int ret = -1;
     int rv;
     VIR_AUTOFREE(char *) buf = NULL;
+    VIR_AUTOPTR(virStorageSource) tmp = NULL;
 
     *backing = NULL;
 
@@ -5008,17 +4986,12 @@ virStorageFileGetBackingStoreStr(virStorageSourcePtr src,
     }
 
     if (!(tmp = virStorageSourceCopy(src, false)))
-        goto cleanup;
+        return -1;
 
     if (virStorageFileGetMetadataInternal(tmp, buf, headerLen, NULL) < 0)
-        goto cleanup;
+        return -1;
 
     VIR_STEAL_PTR(*backing, tmp->backingStoreRaw);
 
-    ret = 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);
 
 VIR_DEFINE_AUTOPTR_FUNC(virStorageAuthDef, virStorageAuthDefFree);
+VIR_DEFINE_AUTOPTR_FUNC(virStorageSource, virStorageSourceFree);
 
 #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 = NULL;
     xmlXPathContextPtr ctxt = NULL;
     virBuffer buf = VIR_BUFFER_INITIALIZER;
-    virStorageSourcePtr xmlsrc = NULL;
-    virStorageSourcePtr jsonsrc = NULL;
     virJSONValuePtr backendprops = NULL;
     virJSONValuePtr wrapper = NULL;
     char *propsstr = NULL;
     char *protocolwrapper = NULL;
     char *actualxml = NULL;
     int ret = -1;
+    VIR_AUTOPTR(virStorageSource) xmlsrc = NULL;
+    VIR_AUTOPTR(virStorageSource) jsonsrc = NULL;
 
     if (VIR_ALLOC(xmlsrc) < 0)
         return -1;
@@ -104,8 +104,6 @@ testBackingXMLjsonXML(const void *args)
     ret = 0;
 
  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 = NULL;
+    virStorageSourcePtr ret = NULL;
+    VIR_AUTOPTR(virStorageSource) def = NULL;
 
     if (VIR_ALLOC(def) < 0)
         return NULL;
@@ -112,16 +113,13 @@ testStorageFileGetMetadata(const char *path,
     }
 
     if (VIR_STRDUP(def->path, path) < 0)
-        goto error;
+        return NULL;
 
     if (virStorageFileGetMetadata(def, uid, gid, false) < 0)
-        goto error;
-
-    return def;
+        return NULL;
 
- error:
-    virStorageSourceFree(def);
-    return NULL;
+    VIR_STEAL_PTR(ret, def);
+    return ret;
 }
 
 static int
@@ -308,41 +306,40 @@ static int
 testStorageChain(const void *args)
 {
     const struct testChainData *data = args;
-    int ret = -1;
-    virStorageSourcePtr meta;
     virStorageSourcePtr elt;
     size_t i = 0;
+    VIR_AUTOPTR(virStorageSource) meta = NULL;
     VIR_AUTOFREE(char *) broken = NULL;
 
     meta = testStorageFileGetMetadata(data->start, data->format, -1, -1);
     if (!meta) {
         if (data->flags & EXP_FAIL) {
             virResetLastError();
-            ret = 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() == 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;
         }
     }
 
@@ -353,7 +350,7 @@ testStorageChain(const void *args)
 
         if (i == data->nfiles) {
             fprintf(stderr, "probed chain was too long\n");
-            goto cleanup;
+            return -1;
         }
 
         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 = elt->backingStore;
         i++;
     }
     if (i != data->nfiles) {
         fprintf(stderr, "probed chain was too short\n");
-        goto cleanup;
+        return -1;
     }
 
-    ret = 0;
- cleanup:
-    virStorageSourceFree(meta);
-    return ret;
+    return 0;
 }
 
 struct testLookupData
@@ -646,9 +640,9 @@ testBackingParse(const void *args)
 {
     const struct testBackingParseData *data = args;
     virBuffer buf = VIR_BUFFER_INITIALIZER;
-    virStorageSourcePtr src = NULL;
     int ret = -1;
     VIR_AUTOFREE(char *) xml = NULL;
+    VIR_AUTOPTR(virStorageSource) src = NULL;
 
     if (!(src = virStorageSourceNewFromBackingAbsolute(data->backing))) {
         if (!data->expect)
@@ -680,7 +674,6 @@ testBackingParse(const void *args)
     ret = 0;
 
  cleanup:
-    virStorageSourceFree(src);
     virBufferFreeAndReset(&buf);
 
     return ret;
@@ -696,10 +689,10 @@ mymain(void)
     struct testPathCanonicalizeData data3;
     struct testPathRelativeBacking data4;
     struct testBackingParseData data5;
-    virStorageSourcePtr chain = NULL;
     virStorageSourcePtr chain2; /* short for chain->backingStore */
     virStorageSourcePtr chain3; /* short for chain2->backingStore */
     VIR_AUTOPTR(virCommand) cmd = NULL;
+    VIR_AUTOPTR(virStorageSource) chain = NULL;
 
     if (storageRegisterAll() < 0)
        return EXIT_FAILURE;
@@ -1580,7 +1573,6 @@ mymain(void)
 
  cleanup:
     /* Final cleanup */
-    virStorageSourceFree(chain);
     testCleanupImages();
 
     return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
-- 
2.20.1


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux