Adapt to VIR_STRDUP and VIR_STRNDUP in src/storage/*

This commit is contained in:
Michal Privoznik 2013-05-03 14:49:08 +02:00
parent 854b91cd9f
commit aaf8114d56
11 changed files with 60 additions and 137 deletions

View File

@ -44,6 +44,7 @@
#include "virutil.h" #include "virutil.h"
#include "c-ctype.h" #include "c-ctype.h"
#include "configmake.h" #include "configmake.h"
#include "virstring.h"
/* we don't need to include the full internal.h just for this */ /* we don't need to include the full internal.h just for this */
#define STREQ(a,b) (strcmp(a,b) == 0) #define STREQ(a,b) (strcmp(a,b) == 0)
@ -86,10 +87,8 @@ int main(int argc, char **argv)
path = argv[1]; path = argv[1];
if (virIsDevMapperDevice(path)) { if (virIsDevMapperDevice(path)) {
partsep = "p"; partsep = "p";
canonical_path = strdup(path); if (VIR_STRDUP_QUIET(canonical_path, path) < 0)
if (canonical_path == NULL) {
return 2; return 2;
}
} else { } else {
if (virFileResolveLink(path, &canonical_path) != 0) { if (virFileResolveLink(path, &canonical_path) != 0) {
return 2; return 2;

View File

@ -486,11 +486,8 @@ virStorageGenerateQcowEncryption(virConnectPtr conn,
goto cleanup; goto cleanup;
def->usage_type = VIR_SECRET_USAGE_TYPE_VOLUME; def->usage_type = VIR_SECRET_USAGE_TYPE_VOLUME;
def->usage.volume = strdup(vol->target.path); if (VIR_STRDUP(def->usage.volume, vol->target.path) < 0)
if (def->usage.volume == NULL) {
virReportOOMError();
goto cleanup; goto cleanup;
}
xml = virSecretDefFormat(def); xml = virSecretDefFormat(def);
virSecretDefFree(def); virSecretDefFree(def);
def = NULL; def = NULL;
@ -1261,12 +1258,11 @@ virStorageBackendUpdateVolTargetInfoFD(virStorageVolTargetPtr target,
target->perms.label = NULL; target->perms.label = NULL;
} }
} else { } else {
target->perms.label = strdup(filecon); if (VIR_STRDUP(target->perms.label, filecon) < 0) {
freecon(filecon); freecon(filecon);
if (target->perms.label == NULL) {
virReportOOMError();
return -1; return -1;
} }
freecon(filecon);
} }
#else #else
target->perms.label = NULL; target->perms.label = NULL;
@ -1451,10 +1447,7 @@ virStorageBackendStablePath(virStoragePoolObjPtr pool,
* the original non-stable dev path * the original non-stable dev path
*/ */
stablepath = strdup(devpath); ignore_value(VIR_STRDUP(stablepath, devpath));
if (stablepath == NULL)
virReportOOMError();
return stablepath; return stablepath;
} }
@ -1556,11 +1549,8 @@ virStorageBackendRunProgRegex(virStoragePoolObjPtr pool,
for (j = 0 ; j < nvars[i] ; j++) { for (j = 0 ; j < nvars[i] ; j++) {
/* NB vars[0] is the full pattern, so we offset j by 1 */ /* NB vars[0] is the full pattern, so we offset j by 1 */
p[vars[j+1].rm_eo] = '\0'; p[vars[j+1].rm_eo] = '\0';
if ((groups[ngroup++] = if (VIR_STRDUP(groups[ngroup++], p + vars[j+1].rm_so) < 0)
strdup(p + vars[j+1].rm_so)) == NULL) {
virReportOOMError();
goto cleanup; goto cleanup;
}
} }
/* We're matching on the last regex, so callback time */ /* We're matching on the last regex, so callback time */

View File

@ -66,17 +66,13 @@ virStorageBackendDiskMakeDataVol(virStoragePoolObjPtr pool,
* strip the path to form a reasonable pool-unique name * strip the path to form a reasonable pool-unique name
*/ */
tmp = strrchr(groups[0], '/'); tmp = strrchr(groups[0], '/');
if ((vol->name = strdup(tmp ? tmp + 1 : groups[0])) == NULL) { if (VIR_STRDUP(vol->name, tmp ? tmp + 1 : groups[0]) < 0)
virReportOOMError();
return -1; return -1;
}
} }
if (vol->target.path == NULL) { if (vol->target.path == NULL) {
if ((devpath = strdup(groups[0])) == NULL) { if (VIR_STRDUP(devpath, groups[0]) < 0)
virReportOOMError();
return -1; return -1;
}
/* Now figure out the stable path /* Now figure out the stable path
* *
@ -92,10 +88,8 @@ virStorageBackendDiskMakeDataVol(virStoragePoolObjPtr pool,
if (vol->key == NULL) { if (vol->key == NULL) {
/* XXX base off a unique key of the underlying disk */ /* XXX base off a unique key of the underlying disk */
if ((vol->key = strdup(vol->target.path)) == NULL) { if (VIR_STRDUP(vol->key, vol->target.path) < 0)
virReportOOMError();
return -1; return -1;
}
} }
if (vol->source.extents == NULL) { if (vol->source.extents == NULL) {
@ -119,11 +113,9 @@ virStorageBackendDiskMakeDataVol(virStoragePoolObjPtr pool,
return -1; return -1;
} }
if ((vol->source.extents[0].path = if (VIR_STRDUP(vol->source.extents[0].path,
strdup(pool->def->source.devices[0].path)) == NULL) { pool->def->source.devices[0].path) < 0)
virReportOOMError();
return -1; return -1;
}
} }
/* Refresh allocation/capacity/perms */ /* Refresh allocation/capacity/perms */
@ -485,10 +477,8 @@ virStorageBackendDiskPartFormat(virStoragePoolObjPtr pool,
return -1; return -1;
} }
} }
if ((*partFormat = strdup(partedFormat)) == NULL) { if (VIR_STRDUP(*partFormat, partedFormat) < 0)
virReportOOMError();
return -1; return -1;
}
} else { } else {
/* create primary partition as long as it is possible /* create primary partition as long as it is possible
and after that check if an extended partition exists and after that check if an extended partition exists
@ -527,10 +517,8 @@ virStorageBackendDiskPartFormat(virStoragePoolObjPtr pool,
} }
} }
} else { } else {
if ((*partFormat = strdup("primary")) == NULL) { if (VIR_STRDUP(*partFormat, "primary") < 0)
virReportOOMError();
return -1; return -1;
}
} }
return 0; return 0;
} }

View File

@ -207,11 +207,9 @@ virStorageBackendFileSystemNetFindPoolSourcesFunc(virStoragePoolObjPtr pool ATTR
} }
src->nhost = 1; src->nhost = 1;
if (!(src->hosts[0].name = strdup(state->host)) || if (VIR_STRDUP(src->hosts[0].name, state->host) < 0 ||
!(src->dir = strdup(path))) { VIR_STRDUP(src->dir, path) < 0)
virReportOOMError();
goto cleanup; goto cleanup;
}
src->format = VIR_STORAGE_POOL_NETFS_NFS; src->format = VIR_STORAGE_POOL_NETFS_NFS;
ret = 0; ret = 0;
@ -395,10 +393,8 @@ virStorageBackendFileSystemMount(virStoragePoolObjPtr pool) {
} }
} else { } else {
if ((src = strdup(pool->def->source.devices[0].path)) == NULL) { if (VIR_STRDUP(src, pool->def->source.devices[0].path) < 0)
virReportOOMError();
return -1; return -1;
}
} }
if (netauto) if (netauto)
@ -571,10 +567,8 @@ virStorageBackendFileSystemProbe(const char *device,
goto error; goto error;
} }
if ((libblkid_format = strdup(format)) == NULL) { if (VIR_STRDUP(libblkid_format, format) < 0)
virReportOOMError();
goto error; goto error;
}
names[0] = libblkid_format; names[0] = libblkid_format;
names[1] = NULL; names[1] = NULL;
@ -749,10 +743,8 @@ virStorageBackendFileSystemBuild(virConnectPtr conn ATTRIBUTE_UNUSED,
goto error; goto error;
} }
if ((parent = strdup(pool->def->target.path)) == NULL) { if (VIR_STRDUP(parent, pool->def->target.path) < 0)
virReportOOMError();
goto error; goto error;
}
if (!(p = strrchr(parent, '/'))) { if (!(p = strrchr(parent, '/'))) {
virReportError(VIR_ERR_INVALID_ARG, virReportError(VIR_ERR_INVALID_ARG,
_("path '%s' is not absolute"), _("path '%s' is not absolute"),
@ -833,8 +825,8 @@ virStorageBackendFileSystemRefresh(virConnectPtr conn ATTRIBUTE_UNUSED,
if (VIR_ALLOC(vol) < 0) if (VIR_ALLOC(vol) < 0)
goto no_memory; goto no_memory;
if ((vol->name = strdup(ent->d_name)) == NULL) if (VIR_STRDUP(vol->name, ent->d_name) < 0)
goto no_memory; goto cleanup;
vol->type = VIR_STORAGE_VOL_FILE; vol->type = VIR_STORAGE_VOL_FILE;
vol->target.format = VIR_STORAGE_FILE_RAW; /* Real value is filled in during probe */ vol->target.format = VIR_STORAGE_FILE_RAW; /* Real value is filled in during probe */
@ -843,8 +835,8 @@ virStorageBackendFileSystemRefresh(virConnectPtr conn ATTRIBUTE_UNUSED,
vol->name) == -1) vol->name) == -1)
goto no_memory; goto no_memory;
if ((vol->key = strdup(vol->target.path)) == NULL) if (VIR_STRDUP(vol->key, vol->target.path) < 0)
goto no_memory; goto cleanup;
if ((ret = virStorageBackendProbeTarget(&vol->target, if ((ret = virStorageBackendProbeTarget(&vol->target,
&backingStore, &backingStore,
@ -1011,13 +1003,7 @@ virStorageBackendFileSystemVolCreate(virConnectPtr conn ATTRIBUTE_UNUSED,
} }
VIR_FREE(vol->key); VIR_FREE(vol->key);
vol->key = strdup(vol->target.path); return VIR_STRDUP(vol->key, vol->target.path);
if (vol->key == NULL) {
virReportOOMError();
return -1;
}
return 0;
} }
static int createFileDir(virConnectPtr conn ATTRIBUTE_UNUSED, static int createFileDir(virConnectPtr conn ATTRIBUTE_UNUSED,

View File

@ -125,13 +125,8 @@ virStorageBackendISCSIExtractSession(virStoragePoolObjPtr pool,
{ {
char **session = data; char **session = data;
if (STREQ(groups[1], pool->def->source.devices[0].path)) { if (STREQ(groups[1], pool->def->source.devices[0].path))
if ((*session = strdup(groups[0])) == NULL) { return VIR_STRDUP(*session, groups[0]);
virReportOOMError();
return -1;
}
}
return 0; return 0;
} }
@ -247,10 +242,8 @@ virStorageBackendIQNFound(const char *initiatoriqn,
"of '%s'"), ISCSIADM); "of '%s'"), ISCSIADM);
goto out; goto out;
} }
*ifacename = strndup(line, token - line); if (VIR_STRNDUP(*ifacename, line, token - line) < 0) {
if (*ifacename == NULL) {
ret = IQN_ERROR; ret = IQN_ERROR;
virReportOOMError();
goto out; goto out;
} }
VIR_DEBUG("Found interface '%s' with IQN '%s'", *ifacename, iqn); VIR_DEBUG("Found interface '%s' with IQN '%s'", *ifacename, iqn);
@ -499,10 +492,8 @@ virStorageBackendISCSIGetTargets(virStoragePoolObjPtr pool ATTRIBUTE_UNUSED,
struct virStorageBackendISCSITargetList *list = data; struct virStorageBackendISCSITargetList *list = data;
char *target; char *target;
if (!(target = strdup(groups[1]))) { if (VIR_STRDUP(target, groups[1]) < 0)
virReportOOMError();
return -1; return -1;
}
if (VIR_REALLOC_N(list->targets, list->ntargets + 1) < 0) { if (VIR_REALLOC_N(list->targets, list->ntargets + 1) < 0) {
VIR_FREE(target); VIR_FREE(target);

View File

@ -105,10 +105,8 @@ virStorageBackendLogicalMakeVol(virStoragePoolObjPtr pool,
is_new_vol = true; is_new_vol = true;
vol->type = VIR_STORAGE_VOL_BLOCK; vol->type = VIR_STORAGE_VOL_BLOCK;
if ((vol->name = strdup(groups[0])) == NULL) { if (VIR_STRDUP(vol->name, groups[0]) < 0)
virReportOOMError();
goto cleanup; goto cleanup;
}
if (VIR_REALLOC_N(pool->volumes.objs, if (VIR_REALLOC_N(pool->volumes.objs,
pool->volumes.count + 1)) { pool->volumes.count + 1)) {
@ -142,11 +140,8 @@ virStorageBackendLogicalMakeVol(virStoragePoolObjPtr pool,
vol->backingStore.format = VIR_STORAGE_POOL_LOGICAL_LVM2; vol->backingStore.format = VIR_STORAGE_POOL_LOGICAL_LVM2;
} }
if (vol->key == NULL && if (!vol->key && VIR_STRDUP(vol->key, groups[2]) < 0)
(vol->key = strdup(groups[2])) == NULL) {
virReportOOMError();
goto cleanup; goto cleanup;
}
if (virStorageBackendUpdateVolInfo(vol, 1) < 0) if (virStorageBackendUpdateVolInfo(vol, 1) < 0)
goto cleanup; goto cleanup;
@ -184,7 +179,8 @@ virStorageBackendLogicalMakeVol(virStoragePoolObjPtr pool,
} }
/* Now parse the "devices" field separately */ /* Now parse the "devices" field separately */
regex = strdup(regex_unit); if (VIR_STRDUP(regex, regex_unit) < 0)
goto cleanup;
for (i = 1; i < nextents; i++) { for (i = 1; i < nextents; i++) {
if (VIR_REALLOC_N(regex, strlen(regex) + strlen(regex_unit) + 2) < 0) { if (VIR_REALLOC_N(regex, strlen(regex) + strlen(regex_unit) + 2) < 0) {
@ -234,23 +230,19 @@ virStorageBackendLogicalMakeVol(virStoragePoolObjPtr pool,
/* vars[0] is skipped */ /* vars[0] is skipped */
for (i = 0; i < nextents; i++) { for (i = 0; i < nextents; i++) {
int j, len; int j, len;
const char *offset_str = NULL; char *offset_str = NULL;
j = (i * 2) + 1; j = (i * 2) + 1;
len = vars[j].rm_eo - vars[j].rm_so; len = vars[j].rm_eo - vars[j].rm_so;
p[vars[j].rm_eo] = '\0'; p[vars[j].rm_eo] = '\0';
if ((vol->source.extents[vol->source.nextent].path = if (VIR_STRNDUP(vol->source.extents[vol->source.nextent].path,
strndup(p + vars[j].rm_so, len)) == NULL) { p + vars[j].rm_so, len) < 0)
virReportOOMError();
goto cleanup; goto cleanup;
}
len = vars[j + 1].rm_eo - vars[j + 1].rm_so; len = vars[j + 1].rm_eo - vars[j + 1].rm_so;
if (!(offset_str = strndup(p + vars[j + 1].rm_so, len))) { if (VIR_STRNDUP(offset_str, p + vars[j + 1].rm_so, len) < 0)
virReportOOMError();
goto cleanup; goto cleanup;
}
if (virStrToLong_ull(offset_str, NULL, 10, &offset) < 0) { if (virStrToLong_ull(offset_str, NULL, 10, &offset) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@ -367,13 +359,9 @@ virStorageBackendLogicalFindPoolSourcesFunc(virStoragePoolObjPtr pool ATTRIBUTE_
virStoragePoolSourceDevicePtr dev; virStoragePoolSourceDevicePtr dev;
virStoragePoolSource *thisSource; virStoragePoolSource *thisSource;
pvname = strdup(groups[0]); if (VIR_STRDUP(pvname, groups[0]) < 0 ||
vgname = strdup(groups[1]); VIR_STRDUP(vgname, groups[1]) < 0)
goto error;
if (pvname == NULL || vgname == NULL) {
virReportOOMError();
goto err_no_memory;
}
thisSource = NULL; thisSource = NULL;
for (i = 0 ; i < sourceList->nsources; i++) { for (i = 0 ; i < sourceList->nsources; i++) {
@ -385,7 +373,7 @@ virStorageBackendLogicalFindPoolSourcesFunc(virStoragePoolObjPtr pool ATTRIBUTE_
if (thisSource == NULL) { if (thisSource == NULL) {
if (!(thisSource = virStoragePoolSourceListNewSource(sourceList))) if (!(thisSource = virStoragePoolSourceListNewSource(sourceList)))
goto err_no_memory; goto error;
thisSource->name = vgname; thisSource->name = vgname;
} }
@ -394,7 +382,7 @@ virStorageBackendLogicalFindPoolSourcesFunc(virStoragePoolObjPtr pool ATTRIBUTE_
if (VIR_REALLOC_N(thisSource->devices, thisSource->ndevice + 1) != 0) { if (VIR_REALLOC_N(thisSource->devices, thisSource->ndevice + 1) != 0) {
virReportOOMError(); virReportOOMError();
goto err_no_memory; goto error;
} }
dev = &thisSource->devices[thisSource->ndevice]; dev = &thisSource->devices[thisSource->ndevice];
@ -406,7 +394,7 @@ virStorageBackendLogicalFindPoolSourcesFunc(virStoragePoolObjPtr pool ATTRIBUTE_
return 0; return 0;
err_no_memory: error:
VIR_FREE(pvname); VIR_FREE(pvname);
VIR_FREE(vgname); VIR_FREE(vgname);

View File

@ -99,11 +99,8 @@ virStorageBackendMpathNewVol(virStoragePoolObjPtr pool,
} }
/* XXX should use logical unit's UUID instead */ /* XXX should use logical unit's UUID instead */
vol->key = strdup(vol->target.path); if (VIR_STRDUP(vol->key, vol->target.path) < 0)
if (vol->key == NULL) {
virReportOOMError();
goto cleanup; goto cleanup;
}
if (VIR_REALLOC_N(pool->volumes.objs, if (VIR_REALLOC_N(pool->volumes.objs,
pool->volumes.count + 1) < 0) { pool->volumes.count + 1) < 0) {

View File

@ -334,10 +334,9 @@ static int virStorageBackendRBDRefreshPool(virConnectPtr conn ATTRIBUTE_UNUSED,
if (VIR_ALLOC(vol) < 0) if (VIR_ALLOC(vol) < 0)
goto out_of_memory; goto out_of_memory;
vol->name = strdup(name); if (VIR_STRDUP(vol->name, name) < 0) {
if (vol->name == NULL) {
VIR_FREE(vol); VIR_FREE(vol);
goto out_of_memory; goto cleanup;
} }
name += strlen(name) + 1; name += strlen(name) + 1;

View File

@ -188,8 +188,7 @@ virStorageBackendSCSISerial(const char *dev)
*nl = '\0'; *nl = '\0';
} else { } else {
VIR_FREE(serial); VIR_FREE(serial);
if (!(serial = strdup(dev))) ignore_value(VIR_STRDUP(serial, dev));
virReportOOMError();
} }
#ifdef WITH_UDEV #ifdef WITH_UDEV
@ -333,10 +332,7 @@ getNewStyleBlockDevice(const char *lun_path,
continue; continue;
} }
*block_device = strdup(block_dirent->d_name); if (VIR_STRDUP(*block_device, block_dirent->d_name) < 0) {
if (*block_device == NULL) {
virReportOOMError();
closedir(block_dir); closedir(block_dir);
retval = -1; retval = -1;
goto out; goto out;
@ -373,10 +369,7 @@ getOldStyleBlockDevice(const char *lun_path ATTRIBUTE_UNUSED,
retval = -1; retval = -1;
} else { } else {
blockp++; blockp++;
*block_device = strdup(blockp); if (VIR_STRDUP(*block_device, blockp) < 0) {
if (*block_device == NULL) {
virReportOOMError();
retval = -1; retval = -1;
goto out; goto out;
} }
@ -630,8 +623,10 @@ getAdapterName(virStoragePoolSourceAdapter adapter)
{ {
char *name = NULL; char *name = NULL;
if (adapter.type != VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_FC_HOST) if (adapter.type != VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_FC_HOST) {
return strdup(adapter.data.name); ignore_value(VIR_STRDUP(name, adapter.data.name));
return name;
}
if (!(name = virGetFCHostNameByWWN(NULL, if (!(name = virGetFCHostNameByWWN(NULL,
adapter.data.fchost.wwnn, adapter.data.fchost.wwnn,

View File

@ -269,11 +269,7 @@ virStorageBackendSheepdogRefreshVol(virConnectPtr conn ATTRIBUTE_UNUSED,
} }
VIR_FREE(vol->target.path); VIR_FREE(vol->target.path);
if (!(vol->target.path = strdup(vol->name))) { ignore_value(VIR_STRDUP(vol->target.path, vol->name));
virReportOOMError();
goto cleanup;
}
cleanup: cleanup:
virCommandFree(cmd); virCommandFree(cmd);
return ret; return ret;

View File

@ -144,8 +144,8 @@ storageStateInitialize(bool privileged,
storageDriverLock(driverState); storageDriverLock(driverState);
if (privileged) { if (privileged) {
if ((base = strdup(SYSCONFDIR "/libvirt")) == NULL) if (VIR_STRDUP(base, SYSCONFDIR "/libvirt") < 0)
goto out_of_memory; goto error;
} else { } else {
base = virGetUserConfigDirectory(); base = virGetUserConfigDirectory();
if (!base) if (!base)
@ -336,9 +336,8 @@ storageConnectListStoragePools(virConnectPtr conn,
for (i = 0 ; i < driver->pools.count && got < nnames ; i++) { for (i = 0 ; i < driver->pools.count && got < nnames ; i++) {
virStoragePoolObjLock(driver->pools.objs[i]); virStoragePoolObjLock(driver->pools.objs[i]);
if (virStoragePoolObjIsActive(driver->pools.objs[i])) { if (virStoragePoolObjIsActive(driver->pools.objs[i])) {
if (!(names[got] = strdup(driver->pools.objs[i]->def->name))) { if (VIR_STRDUP(names[got], driver->pools.objs[i]->def->name) < 0) {
virStoragePoolObjUnlock(driver->pools.objs[i]); virStoragePoolObjUnlock(driver->pools.objs[i]);
virReportOOMError();
goto cleanup; goto cleanup;
} }
got++; got++;
@ -384,9 +383,8 @@ storageConnectListDefinedStoragePools(virConnectPtr conn,
for (i = 0 ; i < driver->pools.count && got < nnames ; i++) { for (i = 0 ; i < driver->pools.count && got < nnames ; i++) {
virStoragePoolObjLock(driver->pools.objs[i]); virStoragePoolObjLock(driver->pools.objs[i]);
if (!virStoragePoolObjIsActive(driver->pools.objs[i])) { if (!virStoragePoolObjIsActive(driver->pools.objs[i])) {
if (!(names[got] = strdup(driver->pools.objs[i]->def->name))) { if (VIR_STRDUP(names[got], driver->pools.objs[i]->def->name) < 0) {
virStoragePoolObjUnlock(driver->pools.objs[i]); virStoragePoolObjUnlock(driver->pools.objs[i]);
virReportOOMError();
goto cleanup; goto cleanup;
} }
got++; got++;
@ -1117,10 +1115,8 @@ storagePoolListVolumes(virStoragePoolPtr obj,
} }
for (i = 0 ; i < pool->volumes.count && n < maxnames ; i++) { for (i = 0 ; i < pool->volumes.count && n < maxnames ; i++) {
if ((names[n++] = strdup(pool->volumes.objs[i]->name)) == NULL) { if (VIR_STRDUP(names[n++], pool->volumes.objs[i]->name) < 0)
virReportOOMError();
goto cleanup; goto cleanup;
}
} }
virStoragePoolObjUnlock(pool); virStoragePoolObjUnlock(pool);
@ -2340,9 +2336,7 @@ storageVolGetPath(virStorageVolPtr obj) {
goto cleanup; goto cleanup;
} }
ret = strdup(vol->target.path); ignore_value(VIR_STRDUP(ret, vol->target.path));
if (ret == NULL)
virReportOOMError();
cleanup: cleanup:
if (pool) if (pool)