virStorageSourceNew: Abort on failure

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>
This commit is contained in:
Peter Krempa 2020-09-22 11:04:17 +02:00
parent 9c6996124f
commit bc3a78f61a
14 changed files with 47 additions and 109 deletions

View File

@ -169,8 +169,7 @@ virDomainBackupDiskDefParseXML(xmlNodePtr node,
def->state = tmp; def->state = tmp;
} }
if (!(def->store = virStorageSourceNew())) def->store = virStorageSourceNew();
return -1;
if ((type = virXMLPropString(node, "type"))) { if ((type = virXMLPropString(node, "type"))) {
if ((def->store->type = virStorageTypeFromString(type)) <= 0) { if ((def->store->type = virStorageTypeFromString(type)) <= 0) {
@ -500,9 +499,7 @@ virDomainBackupDefAssignStore(virDomainBackupDiskDefPtr disk,
} }
} else if (!disk->store) { } else if (!disk->store) {
if (virStorageSourceGetActualType(src) == VIR_STORAGE_TYPE_FILE) { if (virStorageSourceGetActualType(src) == VIR_STORAGE_TYPE_FILE) {
if (!(disk->store = virStorageSourceNew())) disk->store = virStorageSourceNew();
return -1;
disk->store->type = VIR_STORAGE_TYPE_FILE; disk->store->type = VIR_STORAGE_TYPE_FILE;
disk->store->path = g_strdup_printf("%s.%s", src->path, suffix); disk->store->path = g_strdup_printf("%s.%s", src->path, suffix);
} else { } else {

View File

@ -2187,8 +2187,7 @@ virDomainDiskDefNew(virDomainXMLOptionPtr xmlopt)
if (VIR_ALLOC(ret) < 0) if (VIR_ALLOC(ret) < 0)
return NULL; return NULL;
if (!(ret->src = virStorageSourceNew())) ret->src = virStorageSourceNew();
goto error;
if (xmlopt && if (xmlopt &&
xmlopt->privateData.diskNew && xmlopt->privateData.diskNew &&
@ -2400,8 +2399,7 @@ virDomainFSDefNew(virDomainXMLOptionPtr xmlopt)
if (VIR_ALLOC(ret) < 0) if (VIR_ALLOC(ret) < 0)
return NULL; return NULL;
if (!(ret->src = virStorageSourceNew())) ret->src = virStorageSourceNew();
goto cleanup;
if (xmlopt && if (xmlopt &&
xmlopt->privateData.fsNew && xmlopt->privateData.fsNew &&
@ -8346,9 +8344,8 @@ virDomainHostdevSubsysSCSIHostDefParseXML(xmlNodePtr sourcenode,
if (flags & VIR_DOMAIN_DEF_PARSE_STATUS && if (flags & VIR_DOMAIN_DEF_PARSE_STATUS &&
xmlopt && xmlopt->privateData.storageParse) { xmlopt && xmlopt->privateData.storageParse) {
if ((ctxt->node = virXPathNode("./privateData", ctxt))) { if ((ctxt->node = virXPathNode("./privateData", ctxt))) {
if (!scsihostsrc->src && if (!scsihostsrc->src)
!(scsihostsrc->src = virStorageSourceNew())) scsihostsrc->src = virStorageSourceNew();
return -1;
if (xmlopt->privateData.storageParse(ctxt, scsihostsrc->src) < 0) if (xmlopt->privateData.storageParse(ctxt, scsihostsrc->src) < 0)
return -1; return -1;
} }
@ -8374,8 +8371,7 @@ virDomainHostdevSubsysSCSIiSCSIDefParseXML(xmlNodePtr sourcenode,
/* For the purposes of command line creation, this needs to look /* For the purposes of command line creation, this needs to look
* like a disk storage source */ * like a disk storage source */
if (!(iscsisrc->src = virStorageSourceNew())) iscsisrc->src = virStorageSourceNew();
return -1;
iscsisrc->src->type = VIR_STORAGE_TYPE_NETWORK; iscsisrc->src->type = VIR_STORAGE_TYPE_NETWORK;
iscsisrc->src->protocol = VIR_STORAGE_NET_PROTOCOL_ISCSI; iscsisrc->src->protocol = VIR_STORAGE_NET_PROTOCOL_ISCSI;
@ -9791,9 +9787,7 @@ virDomainStorageSourceParseBase(const char *type,
{ {
g_autoptr(virStorageSource) src = NULL; g_autoptr(virStorageSource) src = NULL;
if (!(src = virStorageSourceNew())) src = virStorageSourceNew();
return NULL;
src->type = VIR_STORAGE_TYPE_FILE; src->type = VIR_STORAGE_TYPE_FILE;
if (type && if (type &&
@ -9981,8 +9975,7 @@ virDomainDiskBackingStoreParse(xmlXPathContextPtr ctxt,
/* terminator does not have a type */ /* terminator does not have a type */
if (!(type = virXMLPropString(ctxt->node, "type"))) { if (!(type = virXMLPropString(ctxt->node, "type"))) {
if (!(src->backingStore = virStorageSourceNew())) src->backingStore = virStorageSourceNew();
return -1;
return 0; return 0;
} }

View File

@ -148,9 +148,7 @@ virDomainSnapshotDiskDefParseXML(xmlNodePtr node,
ctxt->node = node; ctxt->node = node;
if (!(def->src = virStorageSourceNew())) def->src = virStorageSourceNew();
goto cleanup;
def->name = virXMLPropString(node, "name"); def->name = virXMLPropString(node, "name");
if (!def->name) { if (!def->name) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@ -744,8 +742,7 @@ virDomainSnapshotAlignDisks(virDomainSnapshotDefPtr def,
if (virBitmapIsBitSet(map, i)) if (virBitmapIsBitSet(map, i))
continue; continue;
disk = &def->disks[ndisks++]; disk = &def->disks[ndisks++];
if (!(disk->src = virStorageSourceNew())) disk->src = virStorageSourceNew();
goto cleanup;
disk->name = g_strdup(def->parent.dom->disks[i]->dst); disk->name = g_strdup(def->parent.dom->disks[i]->dst);
disk->idx = i; disk->idx = i;

View File

@ -1350,9 +1350,7 @@ virStorageVolDefParseXML(virStoragePoolDefPtr pool,
} }
if ((backingStore = virXPathString("string(./backingStore/path)", ctxt))) { if ((backingStore = virXPathString("string(./backingStore/path)", ctxt))) {
if (!(def->target.backingStore = virStorageSourceNew())) def->target.backingStore = virStorageSourceNew();
return NULL;
def->target.backingStore->type = VIR_STORAGE_TYPE_FILE; def->target.backingStore->type = VIR_STORAGE_TYPE_FILE;
def->target.backingStore->path = backingStore; def->target.backingStore->path = backingStore;

View File

@ -5273,9 +5273,8 @@ qemuDomainDeviceHostdevDefPostParseRestoreBackendAlias(virDomainHostdevDefPtr ho
switch ((virDomainHostdevSCSIProtocolType) scsisrc->protocol) { switch ((virDomainHostdevSCSIProtocolType) scsisrc->protocol) {
case VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_NONE: case VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_NONE:
if (!scsisrc->u.host.src && if (!scsisrc->u.host.src)
!(scsisrc->u.host.src = virStorageSourceNew())) scsisrc->u.host.src = virStorageSourceNew();
return -1;
src = scsisrc->u.host.src; src = scsisrc->u.host.src;
break; break;
@ -7167,9 +7166,8 @@ qemuDomainDetermineDiskChain(virQEMUDriverPtr driver,
} }
/* terminate the chain for such images as the code below would do */ /* terminate the chain for such images as the code below would do */
if (!disksrc->backingStore && if (!disksrc->backingStore)
!(disksrc->backingStore = virStorageSourceNew())) disksrc->backingStore = virStorageSourceNew();
return -1;
/* host cdrom requires special treatment in qemu, so we need to check /* host cdrom requires special treatment in qemu, so we need to check
* whether a block device is a cdrom */ * whether a block device is a cdrom */
@ -10418,8 +10416,7 @@ qemuDomainPrepareHostdev(virDomainHostdevDefPtr hostdev,
switch ((virDomainHostdevSCSIProtocolType) scsisrc->protocol) { switch ((virDomainHostdevSCSIProtocolType) scsisrc->protocol) {
case VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_NONE: case VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_NONE:
virObjectUnref(scsisrc->u.host.src); virObjectUnref(scsisrc->u.host.src);
if (!(scsisrc->u.host.src = virStorageSourceNew())) scsisrc->u.host.src = virStorageSourceNew();
return -1;
src = scsisrc->u.host.src; src = scsisrc->u.host.src;
break; break;
@ -10812,9 +10809,7 @@ qemuDomainInitializePflashStorageSource(virDomainObjPtr vm)
if (!virDomainDefHasOldStyleUEFI(def)) if (!virDomainDefHasOldStyleUEFI(def))
return 0; return 0;
if (!(pflash0 = virStorageSourceNew())) pflash0 = virStorageSourceNew();
return -1;
pflash0->type = VIR_STORAGE_TYPE_FILE; pflash0->type = VIR_STORAGE_TYPE_FILE;
pflash0->format = VIR_STORAGE_FILE_RAW; pflash0->format = VIR_STORAGE_FILE_RAW;
pflash0->path = g_strdup(def->os.loader->path); pflash0->path = g_strdup(def->os.loader->path);
@ -10824,9 +10819,7 @@ qemuDomainInitializePflashStorageSource(virDomainObjPtr vm)
if (def->os.loader->nvram) { if (def->os.loader->nvram) {
if (!(pflash1 = virStorageSourceNew())) pflash1 = virStorageSourceNew();
return -1;
pflash1->type = VIR_STORAGE_TYPE_FILE; pflash1->type = VIR_STORAGE_TYPE_FILE;
pflash1->format = VIR_STORAGE_FILE_RAW; pflash1->format = VIR_STORAGE_FILE_RAW;
pflash1->path = g_strdup(def->os.loader->nvram); pflash1->path = g_strdup(def->os.loader->nvram);

View File

@ -14904,10 +14904,8 @@ qemuDomainBlockCopyCommonValidateUserMirrorBackingStore(virStorageSourcePtr mirr
{ {
if (!virStorageSourceHasBacking(mirror)) { if (!virStorageSourceHasBacking(mirror)) {
/* for deep copy there won't be backing chain so we can terminate it */ /* for deep copy there won't be backing chain so we can terminate it */
if (!mirror->backingStore && if (!mirror->backingStore && !shallow)
!shallow && mirror->backingStore = virStorageSourceNew();
!(mirror->backingStore = virStorageSourceNew()))
return -1;
/* When reusing an external image we document that the user must ensure /* When reusing an external image we document that the user must ensure
* that the <mirror> image must expose data as the original image did * 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)) { if (virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_BLOCKDEV_SNAPSHOT_ALLOW_WRITE_ONLY)) {
g_autoptr(virStorageSource) terminator = virStorageSourceNew(); g_autoptr(virStorageSource) terminator = virStorageSourceNew();
if (!terminator)
goto endjob;
if (!(data = qemuBuildStorageSourceChainAttachPrepareBlockdevTop(mirror, if (!(data = qemuBuildStorageSourceChainAttachPrepareBlockdevTop(mirror,
terminator, terminator,
priv->qemuCaps))) priv->qemuCaps)))
@ -15305,8 +15300,7 @@ qemuDomainBlockRebase(virDomainPtr dom, const char *path, const char *base,
return qemuDomainBlockPullCommon(vm, path, base, bandwidth, flags); return qemuDomainBlockPullCommon(vm, path, base, bandwidth, flags);
/* If we got here, we are doing a block copy rebase. */ /* If we got here, we are doing a block copy rebase. */
if (!(dest = virStorageSourceNew())) dest = virStorageSourceNew();
goto cleanup;
dest->type = (flags & VIR_DOMAIN_BLOCK_REBASE_COPY_DEV) ? dest->type = (flags & VIR_DOMAIN_BLOCK_REBASE_COPY_DEV) ?
VIR_STORAGE_TYPE_BLOCK : VIR_STORAGE_TYPE_FILE; VIR_STORAGE_TYPE_BLOCK : VIR_STORAGE_TYPE_FILE;
dest->path = g_strdup(base); dest->path = g_strdup(base);

View File

@ -837,15 +837,12 @@ qemuMigrationSrcNBDStorageCopyBlockdevPrepareSource(virDomainDiskDefPtr disk,
{ {
g_autoptr(virStorageSource) copysrc = NULL; g_autoptr(virStorageSource) copysrc = NULL;
if (!(copysrc = virStorageSourceNew())) copysrc = virStorageSourceNew();
return NULL;
copysrc->type = VIR_STORAGE_TYPE_NETWORK; copysrc->type = VIR_STORAGE_TYPE_NETWORK;
copysrc->protocol = VIR_STORAGE_NET_PROTOCOL_NBD; copysrc->protocol = VIR_STORAGE_NET_PROTOCOL_NBD;
copysrc->format = VIR_STORAGE_FILE_RAW; copysrc->format = VIR_STORAGE_FILE_RAW;
if (!(copysrc->backingStore = virStorageSourceNew())) copysrc->backingStore = virStorageSourceNew();
return NULL;
if (!(copysrc->path = qemuAliasDiskDriveFromDisk(disk))) if (!(copysrc->path = qemuAliasDiskDriveFromDisk(disk)))
return NULL; return NULL;

View File

@ -894,8 +894,7 @@ qemuSnapshotDiskPrepareOneBlockdev(virQEMUDriverPtr driver,
/* create a terminator for the snapshot disks so that qemu does not try /* create a terminator for the snapshot disks so that qemu does not try
* to open them at first */ * to open them at first */
if (!(terminator = virStorageSourceNew())) terminator = virStorageSourceNew();
return -1;
if (qemuDomainPrepareStorageSourceBlockdev(dd->disk, dd->src, if (qemuDomainPrepareStorageSourceBlockdev(dd->disk, dd->src,
priv, cfg) < 0) priv, cfg) < 0)

View File

@ -279,11 +279,8 @@ virStorageBackendGlusterRefreshVol(virStorageBackendGlusterStatePtr state,
goto cleanup; goto cleanup;
if (meta->backingStoreRaw) { if (meta->backingStoreRaw) {
if (!(vol->target.backingStore = virStorageSourceNew())) vol->target.backingStore = virStorageSourceNew();
goto cleanup;
vol->target.backingStore->type = VIR_STORAGE_TYPE_NETWORK; vol->target.backingStore->type = VIR_STORAGE_TYPE_NETWORK;
vol->target.backingStore->path = g_steal_pointer(&meta->backingStoreRaw); vol->target.backingStore->path = g_steal_pointer(&meta->backingStoreRaw);
vol->target.backingStore->format = meta->backingStoreRawFormat; vol->target.backingStore->format = meta->backingStoreRawFormat;

View File

@ -282,9 +282,7 @@ virStorageBackendLogicalMakeVol(char **const groups,
* lv is created with "--virtualsize"). * lv is created with "--virtualsize").
*/ */
if (groups[1] && STRNEQ(groups[1], "") && (groups[1][0] != '[')) { if (groups[1] && STRNEQ(groups[1], "") && (groups[1][0] != '[')) {
if (!(vol->target.backingStore = virStorageSourceNew())) vol->target.backingStore = virStorageSourceNew();
goto cleanup;
vol->target.backingStore->path = g_strdup_printf("%s/%s", vol->target.backingStore->path = g_strdup_printf("%s/%s",
def->target.path, groups[1]); def->target.path, groups[1]);

View File

@ -3389,8 +3389,7 @@ storageBackendProbeTarget(virStorageSourcePtr target,
return -1; return -1;
if (meta->backingStoreRaw) { if (meta->backingStoreRaw) {
if (virStorageSourceNewFromBacking(meta, &target->backingStore) < 0) virStorageSourceNewFromBacking(meta, &target->backingStore);
return -1;
/* XXX: Remote storage doesn't play nicely with volumes backed by /* XXX: Remote storage doesn't play nicely with volumes backed by
* remote storage. To avoid trouble, just fake the backing store is RAW * remote storage. To avoid trouble, just fake the backing store is RAW
@ -3398,9 +3397,7 @@ storageBackendProbeTarget(virStorageSourcePtr target,
if (!virStorageSourceIsLocalStorage(target->backingStore)) { if (!virStorageSourceIsLocalStorage(target->backingStore)) {
virObjectUnref(target->backingStore); virObjectUnref(target->backingStore);
if (!(target->backingStore = virStorageSourceNew())) target->backingStore = virStorageSourceNew();
return -1;
target->backingStore->type = VIR_STORAGE_TYPE_NETWORK; target->backingStore->type = VIR_STORAGE_TYPE_NETWORK;
target->backingStore->path = meta->backingStoreRaw; target->backingStore->path = meta->backingStoreRaw;
meta->backingStoreRaw = NULL; meta->backingStoreRaw = NULL;
@ -3568,8 +3565,7 @@ virStorageBackendRefreshLocal(virStoragePoolObjPtr pool)
goto cleanup; goto cleanup;
VIR_DIR_CLOSE(dir); VIR_DIR_CLOSE(dir);
if (!(target = virStorageSourceNew())) target = virStorageSourceNew();
goto cleanup;
if ((fd = open(def->target.path, O_RDONLY)) < 0) { if ((fd = open(def->target.path, O_RDONLY)) < 0) {
virReportSystemError(errno, virReportSystemError(errno,

View File

@ -1084,10 +1084,7 @@ static virStorageSourcePtr
virStorageFileMetadataNew(const char *path, virStorageFileMetadataNew(const char *path,
int format) int format)
{ {
g_autoptr(virStorageSource) def = NULL; g_autoptr(virStorageSource) def = virStorageSourceNew();
if (!(def = virStorageSourceNew()))
return NULL;
def->format = format; def->format = format;
def->type = VIR_STORAGE_TYPE_FILE; def->type = VIR_STORAGE_TYPE_FILE;
@ -2368,10 +2365,7 @@ virStorageSourcePtr
virStorageSourceCopy(const virStorageSource *src, virStorageSourceCopy(const virStorageSource *src,
bool backingChain) bool backingChain)
{ {
g_autoptr(virStorageSource) def = NULL; g_autoptr(virStorageSource) def = virStorageSourceNew();
if (!(def = virStorageSourceNew()))
return NULL;
def->id = src->id; def->id = src->id;
def->type = src->type; def->type = src->type;
@ -2746,10 +2740,15 @@ VIR_ONCE_GLOBAL_INIT(virStorageSource);
virStorageSourcePtr virStorageSourcePtr
virStorageSourceNew(void) virStorageSourceNew(void)
{ {
if (virStorageSourceInitialize() < 0) virStorageSourcePtr ret;
return NULL;
return virObjectNew(virStorageSourceClass); if (virStorageSourceInitialize() < 0)
abort();
if (!(ret = virObjectNew(virStorageSourceClass)))
abort();
return ret;
} }
@ -2758,10 +2757,7 @@ virStorageSourceNewFromBackingRelative(virStorageSourcePtr parent,
const char *rel) const char *rel)
{ {
g_autofree char *dirname = NULL; g_autofree char *dirname = NULL;
g_autoptr(virStorageSource) def = NULL; g_autoptr(virStorageSource) def = virStorageSourceNew();
if (!(def = virStorageSourceNew()))
return NULL;
/* store relative name */ /* store relative name */
def->relPath = g_strdup(rel); def->relPath = g_strdup(rel);
@ -3980,13 +3976,10 @@ virStorageSourceNewFromBackingAbsolute(const char *path,
const char *json; const char *json;
const char *dirpath; const char *dirpath;
int rc = 0; int rc = 0;
g_autoptr(virStorageSource) def = NULL; g_autoptr(virStorageSource) def = virStorageSourceNew();
*src = NULL; *src = NULL;
if (!(def = virStorageSourceNew()))
return -1;
if (virStorageIsFile(path)) { if (virStorageIsFile(path)) {
def->type = VIR_STORAGE_TYPE_FILE; def->type = VIR_STORAGE_TYPE_FILE;
@ -5317,8 +5310,7 @@ virStorageFileGetMetadataRecurse(virStorageSourcePtr src,
src->backingStore = g_steal_pointer(&backingStore); src->backingStore = g_steal_pointer(&backingStore);
} else { } else {
/* add terminator */ /* add terminator */
if (!(src->backingStore = virStorageSourceNew())) src->backingStore = virStorageSourceNew();
return -1;
} }
return 0; return 0;

View File

@ -67,9 +67,7 @@ testBackingXMLjsonXML(const void *args)
if (data->legacy) if (data->legacy)
backendpropsflags |= QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_LEGACY; backendpropsflags |= QEMU_BLOCK_STORAGE_SOURCE_BACKEND_PROPS_LEGACY;
if (!(xmlsrc = virStorageSourceNew())) xmlsrc = virStorageSourceNew();
return -1;
xmlsrc->type = data->type; xmlsrc->type = data->type;
if (!(xml = virXMLParseStringCtxt(data->xml, "(test storage source XML)", &ctxt))) if (!(xml = virXMLParseStringCtxt(data->xml, "(test storage source XML)", &ctxt)))
@ -670,10 +668,7 @@ testQemuBitmapListPrint(const char *title,
static virStorageSourcePtr static virStorageSourcePtr
testQemuBackupIncrementalBitmapCalculateGetFakeImage(size_t idx) testQemuBackupIncrementalBitmapCalculateGetFakeImage(size_t idx)
{ {
virStorageSourcePtr ret; virStorageSourcePtr ret = virStorageSourceNew();
if (!(ret = virStorageSourceNew()))
abort();
ret->id = idx; ret->id = idx;
ret->type = VIR_STORAGE_TYPE_FILE; ret->type = VIR_STORAGE_TYPE_FILE;
@ -752,9 +747,7 @@ testQemuBackupIncrementalBitmapCalculate(const void *opaque)
return -1; return -1;
} }
if (!(target = virStorageSourceNew())) target = virStorageSourceNew();
return -1;
target->nodeformat = g_strdup_printf("target_node"); target->nodeformat = g_strdup_printf("target_node");
if (qemuBackupDiskPrepareOneBitmapsChain(data->chain, if (qemuBackupDiskPrepareOneBitmapsChain(data->chain,
@ -886,9 +879,6 @@ testQemuBlockBitmapBlockcopy(const void *opaque)
g_autoptr(virStorageSource) fakemirror = virStorageSourceNew(); g_autoptr(virStorageSource) fakemirror = virStorageSourceNew();
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
if (!fakemirror)
return -1;
fakemirror->nodeformat = g_strdup("mirror-format-node"); fakemirror->nodeformat = g_strdup("mirror-format-node");
expectpath = g_strdup_printf("%s/%s%s-out.json", abs_srcdir, expectpath = g_strdup_printf("%s/%s%s-out.json", abs_srcdir,

View File

@ -85,10 +85,7 @@ testStorageFileGetMetadata(const char *path,
uid_t uid, gid_t gid) uid_t uid, gid_t gid)
{ {
struct stat st; struct stat st;
g_autoptr(virStorageSource) def = NULL; g_autoptr(virStorageSource) def = virStorageSourceNew();
if (!(def = virStorageSourceNew()))
return NULL;
def->type = VIR_STORAGE_TYPE_FILE; def->type = VIR_STORAGE_TYPE_FILE;
def->format = format; def->format = format;