]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virStorageSourceNew: Abort on failure
authorPeter Krempa <pkrempa@redhat.com>
Tue, 22 Sep 2020 09:04:17 +0000 (11:04 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Wed, 23 Sep 2020 20:37:56 +0000 (22:37 +0200)
Add an abort() on the class/object allocation failures so that
virStorageSourceNew() always returns a virStorageSource and remove
checks from all callers.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
14 files changed:
src/conf/backup_conf.c
src/conf/domain_conf.c
src/conf/snapshot_conf.c
src/conf/storage_conf.c
src/qemu/qemu_domain.c
src/qemu/qemu_driver.c
src/qemu/qemu_migration.c
src/qemu/qemu_snapshot.c
src/storage/storage_backend_gluster.c
src/storage/storage_backend_logical.c
src/storage/storage_util.c
src/util/virstoragefile.c
tests/qemublocktest.c
tests/virstoragetest.c

index 5caba621d869d65b0f9d5d19a6accde58ac598ef..5c475239ad007e154fb39da618a63db86f02ab65 100644 (file)
@@ -169,8 +169,7 @@ virDomainBackupDiskDefParseXML(xmlNodePtr node,
         def->state = tmp;
     }
 
-    if (!(def->store = virStorageSourceNew()))
-        return -1;
+    def->store = virStorageSourceNew();
 
     if ((type = virXMLPropString(node, "type"))) {
         if ((def->store->type = virStorageTypeFromString(type)) <= 0) {
@@ -500,9 +499,7 @@ virDomainBackupDefAssignStore(virDomainBackupDiskDefPtr disk,
         }
     } else if (!disk->store) {
         if (virStorageSourceGetActualType(src) == VIR_STORAGE_TYPE_FILE) {
-            if (!(disk->store = virStorageSourceNew()))
-                return -1;
-
+            disk->store = virStorageSourceNew();
             disk->store->type = VIR_STORAGE_TYPE_FILE;
             disk->store->path = g_strdup_printf("%s.%s", src->path, suffix);
         } else {
index 22c6ba3b0d3ee4166f601bc91b7787adcae8c4b4..8d30557bdcbe148a0206f3855edba4651e491ae4 100644 (file)
@@ -2187,8 +2187,7 @@ virDomainDiskDefNew(virDomainXMLOptionPtr xmlopt)
     if (VIR_ALLOC(ret) < 0)
         return NULL;
 
-    if (!(ret->src = virStorageSourceNew()))
-        goto error;
+    ret->src = virStorageSourceNew();
 
     if (xmlopt &&
         xmlopt->privateData.diskNew &&
@@ -2400,8 +2399,7 @@ virDomainFSDefNew(virDomainXMLOptionPtr xmlopt)
     if (VIR_ALLOC(ret) < 0)
         return NULL;
 
-    if (!(ret->src = virStorageSourceNew()))
-        goto cleanup;
+    ret->src = virStorageSourceNew();
 
     if (xmlopt &&
         xmlopt->privateData.fsNew &&
@@ -8346,9 +8344,8 @@ virDomainHostdevSubsysSCSIHostDefParseXML(xmlNodePtr sourcenode,
     if (flags & VIR_DOMAIN_DEF_PARSE_STATUS &&
         xmlopt && xmlopt->privateData.storageParse) {
         if ((ctxt->node = virXPathNode("./privateData", ctxt))) {
-            if (!scsihostsrc->src &&
-                !(scsihostsrc->src = virStorageSourceNew()))
-                return -1;
+            if (!scsihostsrc->src)
+                scsihostsrc->src = virStorageSourceNew();
             if (xmlopt->privateData.storageParse(ctxt, scsihostsrc->src) < 0)
                 return -1;
         }
@@ -8374,8 +8371,7 @@ virDomainHostdevSubsysSCSIiSCSIDefParseXML(xmlNodePtr sourcenode,
 
     /* For the purposes of command line creation, this needs to look
      * like a disk storage source */
-    if (!(iscsisrc->src = virStorageSourceNew()))
-        return -1;
+    iscsisrc->src = virStorageSourceNew();
     iscsisrc->src->type = VIR_STORAGE_TYPE_NETWORK;
     iscsisrc->src->protocol = VIR_STORAGE_NET_PROTOCOL_ISCSI;
 
@@ -9791,9 +9787,7 @@ virDomainStorageSourceParseBase(const char *type,
 {
     g_autoptr(virStorageSource) src = NULL;
 
-    if (!(src = virStorageSourceNew()))
-        return NULL;
-
+    src = virStorageSourceNew();
     src->type = VIR_STORAGE_TYPE_FILE;
 
     if (type &&
@@ -9981,8 +9975,7 @@ virDomainDiskBackingStoreParse(xmlXPathContextPtr ctxt,
 
     /* terminator does not have a type */
     if (!(type = virXMLPropString(ctxt->node, "type"))) {
-        if (!(src->backingStore = virStorageSourceNew()))
-            return -1;
+        src->backingStore = virStorageSourceNew();
         return 0;
     }
 
index 1ee63b91416d2e898776e15f47f5abadb12a9327..87a00082efedac4bdbc677daab9e2bd3b58939d9 100644 (file)
@@ -148,9 +148,7 @@ virDomainSnapshotDiskDefParseXML(xmlNodePtr node,
 
     ctxt->node = node;
 
-    if (!(def->src = virStorageSourceNew()))
-        goto cleanup;
-
+    def->src = virStorageSourceNew();
     def->name = virXMLPropString(node, "name");
     if (!def->name) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@@ -744,8 +742,7 @@ virDomainSnapshotAlignDisks(virDomainSnapshotDefPtr def,
         if (virBitmapIsBitSet(map, i))
             continue;
         disk = &def->disks[ndisks++];
-        if (!(disk->src = virStorageSourceNew()))
-            goto cleanup;
+        disk->src = virStorageSourceNew();
         disk->name = g_strdup(def->parent.dom->disks[i]->dst);
         disk->idx = i;
 
index 001f4f2bdd0067f429b24de59ef34655b6e55c58..2b00f09d73093400d994c1173b012c2da0d5bc3f 100644 (file)
@@ -1350,9 +1350,7 @@ virStorageVolDefParseXML(virStoragePoolDefPtr pool,
     }
 
     if ((backingStore = virXPathString("string(./backingStore/path)", ctxt))) {
-        if (!(def->target.backingStore = virStorageSourceNew()))
-            return NULL;
-
+        def->target.backingStore = virStorageSourceNew();
         def->target.backingStore->type = VIR_STORAGE_TYPE_FILE;
 
         def->target.backingStore->path = backingStore;
index 9fa6fac99aca456d1e4c7f2f34e89a66fa61e3de..279de2997d5caadbff0ec6d81f0a36918df2ac6d 100644 (file)
@@ -5273,9 +5273,8 @@ qemuDomainDeviceHostdevDefPostParseRestoreBackendAlias(virDomainHostdevDefPtr ho
 
     switch ((virDomainHostdevSCSIProtocolType) scsisrc->protocol) {
     case VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_NONE:
-        if (!scsisrc->u.host.src &&
-            !(scsisrc->u.host.src = virStorageSourceNew()))
-            return -1;
+        if (!scsisrc->u.host.src)
+            scsisrc->u.host.src = virStorageSourceNew();
 
         src = scsisrc->u.host.src;
         break;
@@ -7167,9 +7166,8 @@ qemuDomainDetermineDiskChain(virQEMUDriverPtr driver,
         }
 
         /* terminate the chain for such images as the code below would do */
-        if (!disksrc->backingStore &&
-            !(disksrc->backingStore = virStorageSourceNew()))
-            return -1;
+        if (!disksrc->backingStore)
+            disksrc->backingStore = virStorageSourceNew();
 
         /* host cdrom requires special treatment in qemu, so we need to check
          * whether a block device is a cdrom */
@@ -10418,8 +10416,7 @@ qemuDomainPrepareHostdev(virDomainHostdevDefPtr hostdev,
         switch ((virDomainHostdevSCSIProtocolType) scsisrc->protocol) {
         case VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_NONE:
             virObjectUnref(scsisrc->u.host.src);
-            if (!(scsisrc->u.host.src = virStorageSourceNew()))
-                return -1;
+            scsisrc->u.host.src = virStorageSourceNew();
             src = scsisrc->u.host.src;
             break;
 
@@ -10812,9 +10809,7 @@ qemuDomainInitializePflashStorageSource(virDomainObjPtr vm)
     if (!virDomainDefHasOldStyleUEFI(def))
         return 0;
 
-    if (!(pflash0 = virStorageSourceNew()))
-        return -1;
-
+    pflash0 = virStorageSourceNew();
     pflash0->type = VIR_STORAGE_TYPE_FILE;
     pflash0->format = VIR_STORAGE_FILE_RAW;
     pflash0->path = g_strdup(def->os.loader->path);
@@ -10824,9 +10819,7 @@ qemuDomainInitializePflashStorageSource(virDomainObjPtr vm)
 
 
     if (def->os.loader->nvram) {
-        if (!(pflash1 = virStorageSourceNew()))
-            return -1;
-
+        pflash1 = virStorageSourceNew();
         pflash1->type = VIR_STORAGE_TYPE_FILE;
         pflash1->format = VIR_STORAGE_FILE_RAW;
         pflash1->path = g_strdup(def->os.loader->nvram);
index 1cecef01f71571d5f1c2b70a08db9325b1c6d153..5db5192fadf0d006c705c8230bc6bed53811dfbd 100644 (file)
@@ -14904,10 +14904,8 @@ qemuDomainBlockCopyCommonValidateUserMirrorBackingStore(virStorageSourcePtr mirr
 {
     if (!virStorageSourceHasBacking(mirror)) {
         /* for deep copy there won't be backing chain so we can terminate it */
-        if (!mirror->backingStore &&
-            !shallow &&
-            !(mirror->backingStore = virStorageSourceNew()))
-            return -1;
+        if (!mirror->backingStore && !shallow)
+            mirror->backingStore = virStorageSourceNew();
 
         /* When reusing an external image we document that the user must ensure
          * that the <mirror> image must expose data as the original image did
@@ -15158,9 +15156,6 @@ qemuDomainBlockCopyCommon(virDomainObjPtr vm,
             if (virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_BLOCKDEV_SNAPSHOT_ALLOW_WRITE_ONLY)) {
                 g_autoptr(virStorageSource) terminator = virStorageSourceNew();
 
-                if (!terminator)
-                    goto endjob;
-
                 if (!(data = qemuBuildStorageSourceChainAttachPrepareBlockdevTop(mirror,
                                                                                  terminator,
                                                                                  priv->qemuCaps)))
@@ -15305,8 +15300,7 @@ qemuDomainBlockRebase(virDomainPtr dom, const char *path, const char *base,
         return qemuDomainBlockPullCommon(vm, path, base, bandwidth, flags);
 
     /* If we got here, we are doing a block copy rebase. */
-    if (!(dest = virStorageSourceNew()))
-        goto cleanup;
+    dest = virStorageSourceNew();
     dest->type = (flags & VIR_DOMAIN_BLOCK_REBASE_COPY_DEV) ?
         VIR_STORAGE_TYPE_BLOCK : VIR_STORAGE_TYPE_FILE;
     dest->path = g_strdup(base);
index a530c1758239956b30cf063d3061a8f82f7f0ae5..5708334d2fb1a25f95ca4cf94a8f530955432432 100644 (file)
@@ -837,15 +837,12 @@ qemuMigrationSrcNBDStorageCopyBlockdevPrepareSource(virDomainDiskDefPtr disk,
 {
     g_autoptr(virStorageSource) copysrc = NULL;
 
-    if (!(copysrc = virStorageSourceNew()))
-        return NULL;
-
+    copysrc = virStorageSourceNew();
     copysrc->type = VIR_STORAGE_TYPE_NETWORK;
     copysrc->protocol = VIR_STORAGE_NET_PROTOCOL_NBD;
     copysrc->format = VIR_STORAGE_FILE_RAW;
 
-    if (!(copysrc->backingStore = virStorageSourceNew()))
-        return NULL;
+    copysrc->backingStore = virStorageSourceNew();
 
     if (!(copysrc->path = qemuAliasDiskDriveFromDisk(disk)))
         return NULL;
index 5f49fd17a4c0c64e0d25954d72e273d6581d373e..a6e091dd09e10961504a27e1effdfe4ae2666072 100644 (file)
@@ -894,8 +894,7 @@ qemuSnapshotDiskPrepareOneBlockdev(virQEMUDriverPtr driver,
 
     /* create a terminator for the snapshot disks so that qemu does not try
      * to open them at first */
-    if (!(terminator = virStorageSourceNew()))
-        return -1;
+    terminator = virStorageSourceNew();
 
     if (qemuDomainPrepareStorageSourceBlockdev(dd->disk, dd->src,
                                                priv, cfg) < 0)
index 70e6f2b086a6bac15fe69629f5065e958701ed3f..4763a569e3a2abd7d04a3cccde4562b2ea567175 100644 (file)
@@ -279,11 +279,8 @@ virStorageBackendGlusterRefreshVol(virStorageBackendGlusterStatePtr state,
         goto cleanup;
 
     if (meta->backingStoreRaw) {
-        if (!(vol->target.backingStore = virStorageSourceNew()))
-            goto cleanup;
-
+        vol->target.backingStore = virStorageSourceNew();
         vol->target.backingStore->type = VIR_STORAGE_TYPE_NETWORK;
-
         vol->target.backingStore->path = g_steal_pointer(&meta->backingStoreRaw);
         vol->target.backingStore->format = meta->backingStoreRawFormat;
 
index 43cf3a15985d391b33b969a401f25e86457474af..e3debc390ca43047ab34d386f4ef4428dd016e0f 100644 (file)
@@ -282,9 +282,7 @@ virStorageBackendLogicalMakeVol(char **const groups,
      *  lv is created with "--virtualsize").
      */
     if (groups[1] && STRNEQ(groups[1], "") && (groups[1][0] != '[')) {
-        if (!(vol->target.backingStore = virStorageSourceNew()))
-            goto cleanup;
-
+        vol->target.backingStore = virStorageSourceNew();
         vol->target.backingStore->path = g_strdup_printf("%s/%s",
                                                          def->target.path, groups[1]);
 
index 9171cb084f7b5ebd607cab277a0032d7414ed568..08d9bd4172ba62d6e8d66575bf019ff7d287e241 100644 (file)
@@ -3389,8 +3389,7 @@ storageBackendProbeTarget(virStorageSourcePtr target,
         return -1;
 
     if (meta->backingStoreRaw) {
-        if (virStorageSourceNewFromBacking(meta, &target->backingStore) < 0)
-            return -1;
+        virStorageSourceNewFromBacking(meta, &target->backingStore);
 
         /* XXX: Remote storage doesn't play nicely with volumes backed by
          * remote storage. To avoid trouble, just fake the backing store is RAW
@@ -3398,9 +3397,7 @@ storageBackendProbeTarget(virStorageSourcePtr target,
         if (!virStorageSourceIsLocalStorage(target->backingStore)) {
             virObjectUnref(target->backingStore);
 
-            if (!(target->backingStore = virStorageSourceNew()))
-                return -1;
-
+            target->backingStore = virStorageSourceNew();
             target->backingStore->type = VIR_STORAGE_TYPE_NETWORK;
             target->backingStore->path = meta->backingStoreRaw;
             meta->backingStoreRaw = NULL;
@@ -3568,8 +3565,7 @@ virStorageBackendRefreshLocal(virStoragePoolObjPtr pool)
         goto cleanup;
     VIR_DIR_CLOSE(dir);
 
-    if (!(target = virStorageSourceNew()))
-        goto cleanup;
+    target = virStorageSourceNew();
 
     if ((fd = open(def->target.path, O_RDONLY)) < 0) {
         virReportSystemError(errno,
index 42341150e59239326dc8d187930194ed6be51cd2..229de149351e0a44e914e11f6ec2aeb4f6c433f1 100644 (file)
@@ -1084,10 +1084,7 @@ static virStorageSourcePtr
 virStorageFileMetadataNew(const char *path,
                           int format)
 {
-    g_autoptr(virStorageSource) def = NULL;
-
-    if (!(def = virStorageSourceNew()))
-        return NULL;
+    g_autoptr(virStorageSource) def = virStorageSourceNew();
 
     def->format = format;
     def->type = VIR_STORAGE_TYPE_FILE;
@@ -2368,10 +2365,7 @@ virStorageSourcePtr
 virStorageSourceCopy(const virStorageSource *src,
                      bool backingChain)
 {
-    g_autoptr(virStorageSource) def = NULL;
-
-    if (!(def = virStorageSourceNew()))
-        return NULL;
+    g_autoptr(virStorageSource) def = virStorageSourceNew();
 
     def->id = src->id;
     def->type = src->type;
@@ -2746,10 +2740,15 @@ VIR_ONCE_GLOBAL_INIT(virStorageSource);
 virStorageSourcePtr
 virStorageSourceNew(void)
 {
+    virStorageSourcePtr ret;
+
     if (virStorageSourceInitialize() < 0)
-        return NULL;
+        abort();
+
+    if (!(ret = virObjectNew(virStorageSourceClass)))
+        abort();
 
-    return virObjectNew(virStorageSourceClass);
+    return ret;
 }
 
 
@@ -2758,10 +2757,7 @@ virStorageSourceNewFromBackingRelative(virStorageSourcePtr parent,
                                        const char *rel)
 {
     g_autofree char *dirname = NULL;
-    g_autoptr(virStorageSource) def = NULL;
-
-    if (!(def = virStorageSourceNew()))
-        return NULL;
+    g_autoptr(virStorageSource) def = virStorageSourceNew();
 
     /* store relative name */
     def->relPath = g_strdup(rel);
@@ -3980,13 +3976,10 @@ virStorageSourceNewFromBackingAbsolute(const char *path,
     const char *json;
     const char *dirpath;
     int rc = 0;
-    g_autoptr(virStorageSource) def = NULL;
+    g_autoptr(virStorageSource) def = virStorageSourceNew();
 
     *src = NULL;
 
-    if (!(def = virStorageSourceNew()))
-        return -1;
-
     if (virStorageIsFile(path)) {
         def->type = VIR_STORAGE_TYPE_FILE;
 
@@ -5317,8 +5310,7 @@ virStorageFileGetMetadataRecurse(virStorageSourcePtr src,
         src->backingStore = g_steal_pointer(&backingStore);
     } else {
         /* add terminator */
-        if (!(src->backingStore = virStorageSourceNew()))
-            return -1;
+        src->backingStore = virStorageSourceNew();
     }
 
     return 0;
index 2489b883ff170baf6f26166dba07f16b1351a50d..c39f96716f70a6a510ead60f02d5fbba27b40ae2 100644 (file)
@@ -67,9 +67,7 @@ testBackingXMLjsonXML(const void *args)
     if (data->legacy)
         backendpropsflags |= QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_LEGACY;
 
-    if (!(xmlsrc = virStorageSourceNew()))
-        return -1;
-
+    xmlsrc = virStorageSourceNew();
     xmlsrc->type = data->type;
 
     if (!(xml = virXMLParseStringCtxt(data->xml, "(test storage source XML)", &ctxt)))
@@ -670,10 +668,7 @@ testQemuBitmapListPrint(const char *title,
 static virStorageSourcePtr
 testQemuBackupIncrementalBitmapCalculateGetFakeImage(size_t idx)
 {
-   virStorageSourcePtr ret;
-
-   if (!(ret = virStorageSourceNew()))
-       abort();
+   virStorageSourcePtr ret = virStorageSourceNew();
 
    ret->id = idx;
    ret->type = VIR_STORAGE_TYPE_FILE;
@@ -752,9 +747,7 @@ testQemuBackupIncrementalBitmapCalculate(const void *opaque)
         return -1;
     }
 
-    if (!(target = virStorageSourceNew()))
-        return -1;
-
+    target = virStorageSourceNew();
     target->nodeformat = g_strdup_printf("target_node");
 
     if (qemuBackupDiskPrepareOneBitmapsChain(data->chain,
@@ -886,9 +879,6 @@ testQemuBlockBitmapBlockcopy(const void *opaque)
     g_autoptr(virStorageSource) fakemirror = virStorageSourceNew();
     g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
 
-    if (!fakemirror)
-        return -1;
-
     fakemirror->nodeformat = g_strdup("mirror-format-node");
 
     expectpath = g_strdup_printf("%s/%s%s-out.json", abs_srcdir,
index e0f8b6d6210cd4aa3f2f0fd61d8b2c18310b1b44..2e466ecb992c329358c04cb3ace2a49ff85b4244 100644 (file)
@@ -85,10 +85,7 @@ testStorageFileGetMetadata(const char *path,
                            uid_t uid, gid_t gid)
 {
     struct stat st;
-    g_autoptr(virStorageSource) def = NULL;
-
-    if (!(def = virStorageSourceNew()))
-        return NULL;
+    g_autoptr(virStorageSource) def = virStorageSourceNew();
 
     def->type = VIR_STORAGE_TYPE_FILE;
     def->format = format;