storage: use g_new0 instead of VIR_ALLOC*

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
This commit is contained in:
Ján Tomko 2020-09-23 20:41:28 +02:00
parent ff146d0953
commit 2e4bf24cac
15 changed files with 52 additions and 100 deletions

View File

@ -74,8 +74,7 @@ virStorageBackendDiskMakeDataVol(virStoragePoolObjPtr pool,
* we're discovering the existing partitions for the pool * we're discovering the existing partitions for the pool
*/ */
addVol = true; addVol = true;
if (VIR_ALLOC(vol) < 0) vol = g_new0(virStorageVolDef, 1);
return -1;
vol->name = g_strdup(partname); vol->name = g_strdup(partname);
} }
@ -132,8 +131,7 @@ virStorageBackendDiskMakeDataVol(virStoragePoolObjPtr pool,
} }
if (vol->source.extents == NULL) { if (vol->source.extents == NULL) {
if (VIR_ALLOC(vol->source.extents) < 0) vol->source.extents = g_new0(virStorageVolSourceExtent, 1);
goto error;
vol->source.nextent = 1; vol->source.nextent = 1;
if (virStrToLong_ull(groups[3], NULL, 10, if (virStrToLong_ull(groups[3], NULL, 10,

View File

@ -76,8 +76,7 @@ virStorageBackendFileSystemNetFindPoolSourcesFunc(char **const groups,
if (!(src = virStoragePoolSourceListNewSource(&state->list))) if (!(src = virStoragePoolSourceListNewSource(&state->list)))
return -1; return -1;
if (VIR_ALLOC_N(src->hosts, 1) < 0) src->hosts = g_new0(virStoragePoolSourceHost, 1);
return -1;
src->nhost = 1; src->nhost = 1;
src->hosts[0].name = g_strdup(state->host); src->hosts[0].name = g_strdup(state->host);
@ -576,9 +575,8 @@ virStoragePoolDefFSNamespaceParse(xmlXPathContextPtr ctxt,
if (nnodes == 0) if (nnodes == 0)
return 0; return 0;
if (VIR_ALLOC(cmdopts) < 0 || cmdopts = g_new0(virStoragePoolFSMountOptionsDef, 1);
VIR_ALLOC_N(cmdopts->options, nnodes) < 0) cmdopts->options = g_new0(char *, nnodes);
goto cleanup;
for (i = 0; i < nnodes; i++) { for (i = 0; i < nnodes; i++) {
if (!(cmdopts->options[cmdopts->noptions] = if (!(cmdopts->options[cmdopts->noptions] =

View File

@ -97,16 +97,14 @@ virStorageBackendGlusterOpen(virStoragePoolObjPtr pool)
trailing_slash = false; trailing_slash = false;
} }
if (VIR_ALLOC(ret) < 0) ret = g_new0(virStorageBackendGlusterState, 1);
return NULL;
ret->volname = g_strdup(name); ret->volname = g_strdup(name);
ret->dir = g_strdup_printf("%s%s", dir ? dir : "/", trailing_slash ? "" : "/"); ret->dir = g_strdup_printf("%s%s", dir ? dir : "/", trailing_slash ? "" : "/");
/* FIXME: Currently hard-coded to tcp transport; XML needs to be /* FIXME: Currently hard-coded to tcp transport; XML needs to be
* extended to allow alternate transport */ * extended to allow alternate transport */
if (VIR_ALLOC(ret->uri) < 0) ret->uri = g_new0(virURI, 1);
goto error;
ret->uri->scheme = g_strdup("gluster"); ret->uri->scheme = g_strdup("gluster");
ret->uri->server = g_strdup(def->source.hosts[0].name); ret->uri->server = g_strdup(def->source.hosts[0].name);
ret->uri->path = g_strdup_printf("/%s%s", ret->volname, ret->dir); ret->uri->path = g_strdup_printf("/%s%s", ret->volname, ret->dir);
@ -151,8 +149,7 @@ virStorageBackendGlusterRead(glfs_fd_t *fd,
char *s; char *s;
size_t nread = 0; size_t nread = 0;
if (VIR_ALLOC_N(*buf, len) < 0) *buf = g_new0(char, len);
return -1;
s = *buf; s = *buf;
while (len) { while (len) {
@ -245,8 +242,7 @@ virStorageBackendGlusterRefreshVol(virStorageBackendGlusterStatePtr state,
return ret; return ret;
} }
if (VIR_ALLOC(vol) < 0) vol = g_new0(virStorageVolDef, 1);
goto cleanup;
if (virStorageBackendUpdateVolTargetInfoFD(&vol->target, -1, st) < 0) if (virStorageBackendUpdateVolTargetInfoFD(&vol->target, -1, st) < 0)
goto cleanup; goto cleanup;

View File

@ -189,13 +189,11 @@ virStorageBackendISCSIFindPoolSources(const char *srcSpec,
&ntargets, &targets) < 0) &ntargets, &targets) < 0)
goto cleanup; goto cleanup;
if (VIR_ALLOC_N(list.sources, ntargets) < 0) list.sources = g_new0(virStoragePoolSource, ntargets);
goto cleanup;
for (i = 0; i < ntargets; i++) { for (i = 0; i < ntargets; i++) {
if (VIR_ALLOC_N(list.sources[i].devices, 1) < 0 || list.sources[i].devices = g_new0(virStoragePoolSourceDevice, 1);
VIR_ALLOC_N(list.sources[i].hosts, 1) < 0) list.sources[i].hosts = g_new0(virStoragePoolSourceHost, 1);
goto cleanup;
list.sources[i].nhost = 1; list.sources[i].nhost = 1;
list.sources[i].hosts[0] = source->hosts[0]; list.sources[i].hosts[0] = source->hosts[0];
list.sources[i].initiator = source->initiator; list.sources[i].initiator = source->initiator;

View File

@ -309,8 +309,7 @@ virISCSIDirectRefreshVol(virStoragePoolObjPtr pool,
if (virISCSIDirectTestUnitReady(iscsi, lun) < 0) if (virISCSIDirectTestUnitReady(iscsi, lun) < 0)
return -1; return -1;
if (VIR_ALLOC(vol) < 0) vol = g_new0(virStorageVolDef, 1);
return -1;
vol->type = VIR_STORAGE_VOL_NETWORK; vol->type = VIR_STORAGE_VOL_NETWORK;
@ -518,13 +517,11 @@ virStorageBackendISCSIDirectFindPoolSources(const char *srcSpec,
if (virISCSIDirectScanTargets(source->initiator.iqn, portal, &ntargets, &targets) < 0) if (virISCSIDirectScanTargets(source->initiator.iqn, portal, &ntargets, &targets) < 0)
goto cleanup; goto cleanup;
if (VIR_ALLOC_N(list.sources, ntargets) < 0) list.sources = g_new0(virStoragePoolSource, ntargets);
goto cleanup;
for (i = 0; i < ntargets; i++) { for (i = 0; i < ntargets; i++) {
if (VIR_ALLOC_N(list.sources[i].devices, 1) < 0 || list.sources[i].devices = g_new0(virStoragePoolSourceDevice, 1);
VIR_ALLOC_N(list.sources[i].hosts, 1) < 0) list.sources[i].hosts = g_new0(virStoragePoolSourceHost, 1);
goto cleanup;
list.sources[i].nhost = 1; list.sources[i].nhost = 1;
list.sources[i].hosts[0] = source->hosts[0]; list.sources[i].hosts[0] = source->hosts[0];
list.sources[i].initiator = source->initiator; list.sources[i].initiator = source->initiator;
@ -628,7 +625,7 @@ virStorageBackendISCSIDirectVolWipeZero(virStorageVolDefPtr vol,
return ret; return ret;
if (virISCSIDirectGetVolumeCapacity(iscsi, lun, &block_size, &nb_block)) if (virISCSIDirectGetVolumeCapacity(iscsi, lun, &block_size, &nb_block))
return ret; return ret;
if (VIR_ALLOC_N(data, block_size * BLOCK_PER_PACKET)) data = g_new0(unsigned char, block_size * BLOCK_PER_PACKET);
return ret; return ret;
while (lba < nb_block) { while (lba < nb_block) {

View File

@ -161,8 +161,7 @@ virStorageBackendLogicalParseVolExtents(virStorageVolDefPtr vol,
} }
/* Allocate space for 'nextents' regex_unit strings plus a comma for each */ /* Allocate space for 'nextents' regex_unit strings plus a comma for each */
if (VIR_ALLOC_N(regex, nextents * (strlen(regex_unit) + 1) + 1) < 0) regex = g_new0(char, nextents * (strlen(regex_unit) + 1) + 1);
goto cleanup;
strcat(regex, regex_unit); strcat(regex, regex_unit);
for (i = 1; i < nextents; i++) { for (i = 1; i < nextents; i++) {
/* "," is the separator of "devices" field */ /* "," is the separator of "devices" field */
@ -252,8 +251,7 @@ virStorageBackendLogicalMakeVol(char **const groups,
/* Or a completely new volume */ /* Or a completely new volume */
if (vol == NULL) { if (vol == NULL) {
if (VIR_ALLOC(vol) < 0) vol = g_new0(virStorageVolDef, 1);
return -1;
is_new_vol = true; is_new_vol = true;
vol->type = VIR_STORAGE_VOL_BLOCK; vol->type = VIR_STORAGE_VOL_BLOCK;

View File

@ -49,8 +49,7 @@ virStorageBackendMpathNewVol(virStoragePoolObjPtr pool,
virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool); virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
g_autoptr(virStorageVolDef) vol = NULL; g_autoptr(virStorageVolDef) vol = NULL;
if (VIR_ALLOC(vol) < 0) vol = g_new0(virStorageVolDef, 1);
return -1;
vol->type = VIR_STORAGE_VOL_BLOCK; vol->type = VIR_STORAGE_VOL_BLOCK;

View File

@ -94,12 +94,10 @@ virStoragePoolDefRBDNamespaceParse(xmlXPathContextPtr ctxt,
if (nnodes == 0) if (nnodes == 0)
return 0; return 0;
if (VIR_ALLOC(cmdopts) < 0) cmdopts = g_new0(virStoragePoolRBDConfigOptionsDef, 1);
goto cleanup;
if (VIR_ALLOC_N(cmdopts->names, nnodes) < 0 || cmdopts->names = g_new0(char *, nnodes);
VIR_ALLOC_N(cmdopts->values, nnodes) < 0) cmdopts->values = g_new0(char *, nnodes);
goto cleanup;
for (i = 0; i < nnodes; i++) { for (i = 0; i < nnodes; i++) {
if (!(cmdopts->names[cmdopts->noptions] = if (!(cmdopts->names[cmdopts->noptions] =
@ -384,8 +382,7 @@ virStorageBackendRBDNewState(virStoragePoolObjPtr pool)
virStorageBackendRBDStatePtr ptr; virStorageBackendRBDStatePtr ptr;
virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool); virStoragePoolDefPtr def = virStoragePoolObjGetDef(pool);
if (VIR_ALLOC(ptr) < 0) ptr = g_new0(virStorageBackendRBDState, 1);
return NULL;
if (virStorageBackendRBDOpenRADOSConn(ptr, def) < 0) if (virStorageBackendRBDOpenRADOSConn(ptr, def) < 0)
goto error; goto error;
@ -604,8 +601,7 @@ virStorageBackendRBDGetVolNames(virStorageBackendRBDStatePtr ptr)
} }
} }
if (VIR_ALLOC_N(names, nimages + 1) < 0) names = g_new0(char *, nimages + 1);
goto error;
nnames = nimages; nnames = nimages;
for (i = 0; i < nimages; i++) for (i = 0; i < nimages; i++)
@ -633,8 +629,7 @@ virStorageBackendRBDGetVolNames(virStorageBackendRBDStatePtr ptr)
const char *name; const char *name;
while (true) { while (true) {
if (VIR_ALLOC_N(namebuf, max_size) < 0) namebuf = g_new0(char, max_size);
goto error;
rc = rbd_list(ptr->ioctx, namebuf, &max_size); rc = rbd_list(ptr->ioctx, namebuf, &max_size);
if (rc >= 0) if (rc >= 0)
@ -712,8 +707,7 @@ virStorageBackendRBDRefreshPool(virStoragePoolObjPtr pool)
for (i = 0; names[i] != NULL; i++) { for (i = 0; names[i] != NULL; i++) {
g_autoptr(virStorageVolDef) vol = NULL; g_autoptr(virStorageVolDef) vol = NULL;
if (VIR_ALLOC(vol) < 0) vol = g_new0(virStorageVolDef, 1);
goto cleanup;
vol->name = g_steal_pointer(&names[i]); vol->name = g_steal_pointer(&names[i]);
@ -770,8 +764,7 @@ virStorageBackendRBDCleanupSnapshots(rados_ioctx_t ioctx,
} }
do { do {
if (VIR_ALLOC_N(snaps, max_snaps)) snaps = g_new0(rbd_snap_info_t, max_snaps);
goto cleanup;
snap_count = rbd_snap_list(image, snaps, &max_snaps); snap_count = rbd_snap_list(image, snaps, &max_snaps);
if (snap_count <= 0) if (snap_count <= 0)
@ -1028,8 +1021,7 @@ virStorageBackendRBDSnapshotFindNoDiff(rbd_image_t image,
} }
do { do {
if (VIR_ALLOC_N(snaps, max_snaps)) snaps = g_new0(rbd_snap_info_t, max_snaps);
goto cleanup;
snap_count = rbd_snap_list(image, snaps, &max_snaps); snap_count = rbd_snap_list(image, snaps, &max_snaps);
if (snap_count <= 0) if (snap_count <= 0)
@ -1320,8 +1312,7 @@ virStorageBackendRBDVolWipeZero(rbd_image_t image,
unsigned long long length; unsigned long long length;
g_autofree char *writebuf = NULL; g_autofree char *writebuf = NULL;
if (VIR_ALLOC_N(writebuf, info->obj_size * stripe_count) < 0) writebuf = g_new0(char, info->obj_size * stripe_count);
return -1;
while (offset < info->size) { while (offset < info->size) {
length = MIN((info->size - offset), (info->obj_size * stripe_count)); length = MIN((info->size - offset), (info->obj_size * stripe_count));

View File

@ -329,9 +329,7 @@ createVport(virStoragePoolDefPtr def,
* retry logic set to true. If the thread isn't created, then no big * retry logic set to true. If the thread isn't created, then no big
* deal since it's still possible to refresh the pool later. * deal since it's still possible to refresh the pool later.
*/ */
if (VIR_ALLOC(cbdata) < 0) cbdata = g_new0(virStoragePoolFCRefreshInfo, 1);
return -1;
memcpy(cbdata->pool_uuid, def->uuid, VIR_UUID_BUFLEN); memcpy(cbdata->pool_uuid, def->uuid, VIR_UUID_BUFLEN);
cbdata->fchost_name = g_steal_pointer(&name); cbdata->fchost_name = g_steal_pointer(&name);

View File

@ -118,8 +118,7 @@ virStorageBackendSheepdogAddVolume(virStoragePoolObjPtr pool, const char *diskIn
return -1; return -1;
} }
if (VIR_ALLOC(vol) < 0) vol = g_new0(virStorageVolDef, 1);
return -1;
vol->name = g_strdup(diskInfo); vol->name = g_strdup(diskInfo);

View File

@ -121,8 +121,7 @@ virStorageBackendZFSParseVol(virStoragePoolObjPtr pool,
volume = vol; volume = vol;
if (volume == NULL) { if (volume == NULL) {
if (VIR_ALLOC(volume) < 0) volume = g_new0(virStorageVolDef, 1);
goto cleanup;
is_new_vol = true; is_new_vol = true;
volume->type = VIR_STORAGE_VOL_BLOCK; volume->type = VIR_STORAGE_VOL_BLOCK;

View File

@ -265,8 +265,7 @@ storageStateInitialize(bool privileged,
return -1; return -1;
} }
if (VIR_ALLOC(driver) < 0) driver = g_new0(virStorageDriverState, 1);
return VIR_DRV_STATE_INIT_ERROR;
driver->lockFD = -1; driver->lockFD = -1;
if (virMutexInit(&driver->lock) < 0) { if (virMutexInit(&driver->lock) < 0) {
@ -1956,10 +1955,7 @@ storageVolCreateXML(virStoragePoolPtr pool,
int buildret; int buildret;
virStorageVolDefPtr buildvoldef = NULL; virStorageVolDefPtr buildvoldef = NULL;
if (VIR_ALLOC(buildvoldef) < 0) { buildvoldef = g_new0(virStorageVolDef, 1);
voldef = NULL;
goto cleanup;
}
/* Make a shallow copy of the 'defined' volume definition, since the /* Make a shallow copy of the 'defined' volume definition, since the
* original allocation value will change as the user polls 'info', * original allocation value will change as the user polls 'info',
@ -2143,8 +2139,7 @@ storageVolCreateXMLFrom(virStoragePoolPtr pool,
* original allocation value will change as the user polls 'info', * original allocation value will change as the user polls 'info',
* but we only need the initial requested values * but we only need the initial requested values
*/ */
if (VIR_ALLOC(shadowvol) < 0) shadowvol = g_new0(virStorageVolDef, 1);
goto cleanup;
memcpy(shadowvol, voldef, sizeof(*voldef)); memcpy(shadowvol, voldef, sizeof(*voldef));
@ -2428,8 +2423,7 @@ storageVolUpload(virStorageVolPtr vol,
* interaction and we can just lookup the backend in the callback * interaction and we can just lookup the backend in the callback
* routine in order to call the refresh API. * routine in order to call the refresh API.
*/ */
if (VIR_ALLOC(cbdata) < 0) cbdata = g_new0(virStorageVolStreamInfo, 1);
goto cleanup;
cbdata->pool_name = g_strdup(def->name); cbdata->pool_name = g_strdup(def->name);
if (voldef->type == VIR_STORAGE_VOL_PLOOP) if (voldef->type == VIR_STORAGE_VOL_PLOOP)
cbdata->vol_path = g_strdup(voldef->target.path); cbdata->vol_path = g_strdup(voldef->target.path);

View File

@ -73,8 +73,7 @@ virStorageFileBackendFileInit(virStorageSourcePtr src)
src->path, src->path,
(unsigned int)src->drv->uid, (unsigned int)src->drv->gid); (unsigned int)src->drv->uid, (unsigned int)src->drv->gid);
if (VIR_ALLOC(priv) < 0) priv = g_new0(virStorageFileBackendFsPriv, 1);
return -1;
src->drv->priv = priv; src->drv->priv = priv;

View File

@ -109,8 +109,7 @@ virStorageFileBackendGlusterInit(virStorageSourcePtr src)
return -1; return -1;
} }
if (VIR_ALLOC(priv) < 0) priv = g_new0(virStorageFileBackendGlusterPriv, 1);
return -1;
VIR_DEBUG("initializing gluster storage file %p " VIR_DEBUG("initializing gluster storage file %p "
"(priv='%p' volume='%s' path='%s') as [%u:%u]", "(priv='%p' volume='%s' path='%s') as [%u:%u]",
@ -209,8 +208,7 @@ virStorageFileBackendGlusterRead(virStorageSourcePtr src,
} }
if (VIR_ALLOC_N(*buf, len) < 0) *buf = g_new0(char, len);
return -1;
s = *buf; s = *buf;
while (len) { while (len) {

View File

@ -154,11 +154,9 @@ virStorageBackendCopyToFD(virStorageVolDefPtr vol,
if (wbytes < WRITE_BLOCK_SIZE_DEFAULT) if (wbytes < WRITE_BLOCK_SIZE_DEFAULT)
wbytes = WRITE_BLOCK_SIZE_DEFAULT; wbytes = WRITE_BLOCK_SIZE_DEFAULT;
if (VIR_ALLOC_N(zerobuf, wbytes) < 0) zerobuf = g_new0(char, wbytes);
return -errno;
if (VIR_ALLOC_N(buf, rbytes) < 0) buf = g_new0(char, rbytes);
return -errno;
if (reflink_copy) { if (reflink_copy) {
if (reflinkCloneFile(fd, inputfd) < 0) { if (reflinkCloneFile(fd, inputfd) < 0) {
@ -1839,14 +1837,14 @@ virStorageBackendUpdateVolTargetInfoFD(virStorageSourcePtr target,
if (virStorageSourceUpdateBackingSizes(target, fd, sb) < 0) if (virStorageSourceUpdateBackingSizes(target, fd, sb) < 0)
return -1; return -1;
if (!target->perms && VIR_ALLOC(target->perms) < 0) if (!target->perms)
return -1; target->perms = g_new0(virStoragePerms, 1);
target->perms->mode = sb->st_mode & S_IRWXUGO; target->perms->mode = sb->st_mode & S_IRWXUGO;
target->perms->uid = sb->st_uid; target->perms->uid = sb->st_uid;
target->perms->gid = sb->st_gid; target->perms->gid = sb->st_gid;
if (!target->timestamps && VIR_ALLOC(target->timestamps) < 0) if (!target->timestamps)
return -1; target->timestamps = g_new0(virStorageTimestamps, 1);
#ifdef __APPLE__ #ifdef __APPLE__
target->timestamps->atime = sb->st_atimespec; target->timestamps->atime = sb->st_atimespec;
@ -2214,12 +2212,8 @@ storageBackendLoadDefaultSecrets(virStorageVolDefPtr vol)
if (!sec) if (!sec)
return 0; return 0;
if (VIR_ALLOC_N(vol->target.encryption->secrets, 1) < 0 || vol->target.encryption->secrets = g_new0(virStorageEncryptionSecretPtr, 1);
VIR_ALLOC(encsec) < 0) { encsec = g_new0(virStorageEncryptionSecret, 1);
VIR_FREE(vol->target.encryption->secrets);
virObjectUnref(sec);
return -1;
}
vol->target.encryption->nsecrets = 1; vol->target.encryption->nsecrets = 1;
vol->target.encryption->secrets[0] = encsec; vol->target.encryption->secrets[0] = encsec;
@ -2528,8 +2522,7 @@ storageBackendWipeLocal(const char *path,
off_t size; off_t size;
g_autofree char *writebuf = NULL; g_autofree char *writebuf = NULL;
if (VIR_ALLOC_N(writebuf, writebuf_length) < 0) writebuf = g_new0(char, writebuf_length);
return -1;
if (!zero_end) { if (!zero_end) {
if ((size = lseek(fd, 0, SEEK_SET)) < 0) { if ((size = lseek(fd, 0, SEEK_SET)) < 0) {
@ -2873,8 +2866,7 @@ virStorageUtilGlusterExtractPoolSources(const char *host,
goto cleanup; goto cleanup;
} }
if (VIR_ALLOC_N(src->hosts, 1) < 0) src->hosts = g_new0(virStoragePoolSourceHost, 1);
goto cleanup;
src->nhost = 1; src->nhost = 1;
src->hosts[0].name = g_strdup(host); src->hosts[0].name = g_strdup(host);
@ -3535,8 +3527,7 @@ virStorageBackendRefreshLocal(virStoragePoolObjPtr pool)
continue; continue;
} }
if (VIR_ALLOC(vol) < 0) vol = g_new0(virStorageVolDef, 1);
goto cleanup;
vol->name = g_strdup(ent->d_name); vol->name = g_strdup(ent->d_name);
@ -3671,8 +3662,7 @@ virStorageBackendSCSINewLun(virStoragePoolObjPtr pool,
return -1; return -1;
} }
if (VIR_ALLOC(vol) < 0) vol = g_new0(virStorageVolDef, 1);
return -1;
vol->type = VIR_STORAGE_VOL_BLOCK; vol->type = VIR_STORAGE_VOL_BLOCK;