1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-03-07 17:28:15 +00:00

util: o-z: use g_new0

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Erik Skultety <eskultet@redhat.com>
This commit is contained in:
Ján Tomko 2020-10-05 19:13:12 +02:00
parent e59b8f96f7
commit b15093d867
18 changed files with 46 additions and 97 deletions

View File

@ -1372,8 +1372,7 @@ virPCIDeviceNew(unsigned int domain,
g_autofree char *vendor = NULL; g_autofree char *vendor = NULL;
g_autofree char *product = NULL; g_autofree char *product = NULL;
if (VIR_ALLOC(dev) < 0) dev = g_new0(virPCIDevice, 1);
return NULL;
dev->address.domain = domain; dev->address.domain = domain;
dev->address.bus = bus; dev->address.bus = bus;
@ -1422,8 +1421,7 @@ virPCIDeviceCopy(virPCIDevicePtr dev)
{ {
virPCIDevicePtr copy; virPCIDevicePtr copy;
if (VIR_ALLOC(copy) < 0) copy = g_new0(virPCIDevice, 1);
return NULL;
/* shallow copy to take care of most attributes */ /* shallow copy to take care of most attributes */
*copy = *dev; *copy = *dev;
@ -1871,8 +1869,7 @@ virPCIGetIOMMUGroupAddressesAddOne(virPCIDeviceAddressPtr newDevAddr, void *opaq
virPCIDeviceAddressPtr copyAddr; virPCIDeviceAddressPtr copyAddr;
/* make a copy to insert onto the list */ /* make a copy to insert onto the list */
if (VIR_ALLOC(copyAddr) < 0) copyAddr = g_new0(virPCIDeviceAddress, 1);
goto cleanup;
*copyAddr = *newDevAddr; *copyAddr = *newDevAddr;
@ -2204,8 +2201,7 @@ virPCIGetDeviceAddressFromSysfsLink(const char *device_link)
} }
config_address = g_path_get_basename(device_path); config_address = g_path_get_basename(device_path);
if (VIR_ALLOC(bdf) < 0) bdf = g_new0(virPCIDeviceAddress, 1);
return NULL;
if (virPCIDeviceAddressParse(config_address, bdf) < 0) { if (virPCIDeviceAddressParse(config_address, bdf) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,

View File

@ -368,8 +368,7 @@ virPerfNew(void)
size_t i; size_t i;
virPerfPtr perf; virPerfPtr perf;
if (VIR_ALLOC(perf) < 0) perf = g_new0(virPerf, 1);
return NULL;
for (i = 0; i < VIR_PERF_EVENT_LAST; i++) { for (i = 0; i < VIR_PERF_EVENT_LAST; i++) {
perf->events[i].fd = -1; perf->events[i].fd = -1;

View File

@ -178,8 +178,7 @@ virPolkitAgentCreate(void)
if (virPipe(pipe_fd) < 0) if (virPipe(pipe_fd) < 0)
goto error; goto error;
if (VIR_ALLOC(agent) < 0) agent = g_new0(virPolkitAgent, 1);
goto error;
agent->cmd = virCommandNewArgList(PKTTYAGENT, "--process", NULL); agent->cmd = virCommandNewArgList(PKTTYAGENT, "--process", NULL);

View File

@ -102,8 +102,7 @@ virPortAllocatorRangeNew(const char *name,
return NULL; return NULL;
} }
if (VIR_ALLOC(range) < 0) range = g_new0(virPortAllocatorRange, 1);
return NULL;
range->start = start; range->start = start;
range->end = end; range->end = end;

View File

@ -1351,8 +1351,7 @@ virProcessNamespaceAvailable(unsigned int ns)
/* Signal parent as soon as the child dies. RIP. */ /* Signal parent as soon as the child dies. RIP. */
flags |= SIGCHLD; flags |= SIGCHLD;
if (VIR_ALLOC_N(stack, stacksize) < 0) stack = g_new0(char, stacksize);
return -1;
childStack = stack + stacksize; childStack = stack + stacksize;

View File

@ -105,8 +105,7 @@ virRotatingFileWriterEntryNew(const char *path,
VIR_DEBUG("Opening %s mode=0%02o", path, mode); VIR_DEBUG("Opening %s mode=0%02o", path, mode);
if (VIR_ALLOC(entry) < 0) entry = g_new0(virRotatingFileWriterEntry, 1);
return NULL;
if ((entry->fd = open(path, O_CREAT|O_APPEND|O_WRONLY|O_CLOEXEC, mode)) < 0) { if ((entry->fd = open(path, O_CREAT|O_APPEND|O_WRONLY|O_CLOEXEC, mode)) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
@ -148,8 +147,7 @@ virRotatingFileReaderEntryNew(const char *path)
VIR_DEBUG("Opening %s", path); VIR_DEBUG("Opening %s", path);
if (VIR_ALLOC(entry) < 0) entry = g_new0(virRotatingFileReaderEntry, 1);
return NULL;
if ((entry->fd = open(path, O_RDONLY|O_CLOEXEC)) < 0) { if ((entry->fd = open(path, O_RDONLY|O_CLOEXEC)) < 0) {
if (errno != ENOENT) { if (errno != ENOENT) {
@ -238,8 +236,7 @@ virRotatingFileWriterNew(const char *path,
{ {
virRotatingFileWriterPtr file; virRotatingFileWriterPtr file;
if (VIR_ALLOC(file) < 0) file = g_new0(virRotatingFileWriter, 1);
goto error;
file->basepath = g_strdup(path); file->basepath = g_strdup(path);
@ -288,8 +285,7 @@ virRotatingFileReaderNew(const char *path,
virRotatingFileReaderPtr file; virRotatingFileReaderPtr file;
size_t i; size_t i;
if (VIR_ALLOC(file) < 0) file = g_new0(virRotatingFileReader, 1);
goto error;
if (maxbackup > VIR_MAX_MAX_BACKUP) { if (maxbackup > VIR_MAX_MAX_BACKUP) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
@ -299,8 +295,7 @@ virRotatingFileReaderNew(const char *path,
} }
file->nentries = maxbackup + 1; file->nentries = maxbackup + 1;
if (VIR_ALLOC_N(file->entries, file->nentries) < 0) file->entries = g_new0(virRotatingFileReaderEntryPtr, file->nentries);
goto error;
if (!(file->entries[file->nentries - 1] = virRotatingFileReaderEntryNew(path))) if (!(file->entries[file->nentries - 1] = virRotatingFileReaderEntryNew(path)))
goto error; goto error;

View File

@ -186,8 +186,7 @@ virSCSIDeviceNew(const char *sysfs_prefix,
g_autofree char *model = NULL; g_autofree char *model = NULL;
const char *prefix = sysfs_prefix ? sysfs_prefix : SYSFS_SCSI_DEVICES; const char *prefix = sysfs_prefix ? sysfs_prefix : SYSFS_SCSI_DEVICES;
if (VIR_ALLOC(dev) < 0) dev = g_new0(virSCSIDevice, 1);
return NULL;
dev->bus = bus; dev->bus = bus;
dev->target = target; dev->target = target;
@ -263,8 +262,7 @@ virSCSIDeviceSetUsedBy(virSCSIDevicePtr dev,
{ {
g_autoptr(virUsedByInfo) copy = NULL; g_autoptr(virUsedByInfo) copy = NULL;
if (VIR_ALLOC(copy) < 0) copy = g_new0(virUsedByInfo, 1);
return -1;
copy->drvname = g_strdup(drvname); copy->drvname = g_strdup(drvname);
copy->domname = g_strdup(domname); copy->domname = g_strdup(domname);

View File

@ -250,8 +250,7 @@ virSCSIVHostDeviceNew(const char *name)
{ {
g_autoptr(virSCSIVHostDevice) dev = NULL; g_autoptr(virSCSIVHostDevice) dev = NULL;
if (VIR_ALLOC(dev) < 0) dev = g_new0(virSCSIVHostDevice, 1);
return NULL;
dev->name = g_strdup(name); dev->name = g_strdup(name);

View File

@ -58,10 +58,7 @@ virSecurityLabelDefNew(const char *model)
{ {
virSecurityLabelDefPtr seclabel = NULL; virSecurityLabelDefPtr seclabel = NULL;
if (VIR_ALLOC(seclabel) < 0) { seclabel = g_new0(virSecurityLabelDef, 1);
virSecurityLabelDefFree(seclabel);
return NULL;
}
seclabel->model = g_strdup(model); seclabel->model = g_strdup(model);
@ -75,10 +72,7 @@ virSecurityDeviceLabelDefNew(const char *model)
{ {
virSecurityDeviceLabelDefPtr seclabel = NULL; virSecurityDeviceLabelDefPtr seclabel = NULL;
if (VIR_ALLOC(seclabel) < 0) { seclabel = g_new0(virSecurityDeviceLabelDef, 1);
virSecurityDeviceLabelDefFree(seclabel);
return NULL;
}
seclabel->model = g_strdup(model); seclabel->model = g_strdup(model);
@ -91,8 +85,7 @@ virSecurityDeviceLabelDefCopy(const virSecurityDeviceLabelDef *src)
{ {
virSecurityDeviceLabelDefPtr ret; virSecurityDeviceLabelDefPtr ret;
if (VIR_ALLOC(ret) < 0) ret = g_new0(virSecurityDeviceLabelDef, 1);
return NULL;
ret->relabel = src->relabel; ret->relabel = src->relabel;
ret->labelskip = src->labelskip; ret->labelskip = src->labelskip;

View File

@ -115,12 +115,9 @@ virStorageEncryptionCopy(const virStorageEncryption *src)
virStorageEncryptionPtr ret; virStorageEncryptionPtr ret;
size_t i; size_t i;
if (VIR_ALLOC(ret) < 0) ret = g_new0(virStorageEncryption, 1);
return NULL;
if (VIR_ALLOC_N(ret->secrets, src->nsecrets) < 0)
goto error;
ret->secrets = g_new0(virStorageEncryptionSecretPtr, src->nsecrets);
ret->nsecrets = src->nsecrets; ret->nsecrets = src->nsecrets;
ret->format = src->format; ret->format = src->format;
@ -147,8 +144,7 @@ virStorageEncryptionSecretParse(xmlXPathContextPtr ctxt,
virStorageEncryptionSecretPtr ret; virStorageEncryptionSecretPtr ret;
char *type_str = NULL; char *type_str = NULL;
if (VIR_ALLOC(ret) < 0) ret = g_new0(virStorageEncryptionSecret, 1);
return NULL;
ctxt->node = node; ctxt->node = node;
@ -247,8 +243,7 @@ virStorageEncryptionParseNode(xmlNodePtr node,
ctxt->node = node; ctxt->node = node;
if (VIR_ALLOC(encdef) < 0) encdef = g_new0(virStorageEncryption, 1);
goto cleanup;
if (!(format_str = virXPathString("string(./@format)", ctxt))) { if (!(format_str = virXPathString("string(./@format)", ctxt))) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
@ -268,8 +263,7 @@ virStorageEncryptionParseNode(xmlNodePtr node,
goto cleanup; goto cleanup;
if (n > 0) { if (n > 0) {
if (VIR_ALLOC_N(encdef->secrets, n) < 0) encdef->secrets = g_new0(virStorageEncryptionSecretPtr, n);
goto cleanup;
encdef->nsecrets = n; encdef->nsecrets = n;
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {

View File

@ -1222,8 +1222,7 @@ virStringToUpper(char **dst, const char *src)
if (!src) if (!src)
return 0; return 0;
if (VIR_ALLOC_N(cap, strlen(src) + 1) < 0) cap = g_new0(char, strlen(src) + 1);
return -1;
for (i = 0; src[i]; i++) { for (i = 0; src[i]; i++) {
cap[i] = g_ascii_toupper(src[i]); cap[i] = g_ascii_toupper(src[i]);

View File

@ -188,9 +188,8 @@ virThreadPoolExpand(virThreadPoolPtr pool, size_t gain, bool priority)
for (i = 0; i < gain; i++) { for (i = 0; i < gain; i++) {
g_autofree char *name = NULL; g_autofree char *name = NULL;
if (VIR_ALLOC(data) < 0)
goto error;
data = g_new0(struct virThreadPoolWorkerData, 1);
data->pool = pool; data->pool = pool;
data->cond = priority ? &pool->prioCond : &pool->cond; data->cond = priority ? &pool->prioCond : &pool->cond;
data->priority = priority; data->priority = priority;
@ -232,8 +231,7 @@ virThreadPoolNewFull(size_t minWorkers,
if (minWorkers > maxWorkers) if (minWorkers > maxWorkers)
minWorkers = maxWorkers; minWorkers = maxWorkers;
if (VIR_ALLOC(pool) < 0) pool = g_new0(virThreadPool, 1);
return NULL;
pool->jobList.tail = pool->jobList.head = NULL; pool->jobList.tail = pool->jobList.head = NULL;
@ -403,8 +401,7 @@ int virThreadPoolSendJob(virThreadPoolPtr pool,
virThreadPoolExpand(pool, 1, false) < 0) virThreadPoolExpand(pool, 1, false) < 0)
goto error; goto error;
if (VIR_ALLOC(job) < 0) job = g_new0(virThreadPoolJob, 1);
goto error;
job->data = jobData; job->data = jobData;
job->priority = priority; job->priority = priority;

View File

@ -257,8 +257,7 @@ char *virTimeStringNow(void)
{ {
char *ret; char *ret;
if (VIR_ALLOC_N(ret, VIR_TIME_STRING_BUFLEN) < 0) ret = g_new0(char, VIR_TIME_STRING_BUFLEN);
return NULL;
if (virTimeStringNowRaw(ret) < 0) { if (virTimeStringNowRaw(ret) < 0) {
virReportSystemError(errno, "%s", virReportSystemError(errno, "%s",
@ -286,8 +285,7 @@ char *virTimeStringThen(unsigned long long when)
{ {
char *ret; char *ret;
if (VIR_ALLOC_N(ret, VIR_TIME_STRING_BUFLEN) < 0) ret = g_new0(char, VIR_TIME_STRING_BUFLEN);
return NULL;
if (virTimeStringThenRaw(when, ret) < 0) { if (virTimeStringThenRaw(when, ret) < 0) {
virReportSystemError(errno, "%s", virReportSystemError(errno, "%s",

View File

@ -68,8 +68,7 @@ virTypedParamsValidate(virTypedParameterPtr params, int nparams, ...)
va_start(ap, nparams); va_start(ap, nparams);
if (VIR_ALLOC_N(sorted, nparams) < 0) sorted = g_new0(virTypedParameter, nparams);
goto cleanup;
/* Here we intentionally don't copy values */ /* Here we intentionally don't copy values */
memcpy(sorted, params, sizeof(*params) * nparams); memcpy(sorted, params, sizeof(*params) * nparams);
@ -361,8 +360,7 @@ virTypedParamsCopy(virTypedParameterPtr *dst,
if (!src || nparams <= 0) if (!src || nparams <= 0)
return 0; return 0;
if (VIR_ALLOC_N(*dst, nparams) < 0) *dst = g_new0(virTypedParameter, nparams);
return -1;
for (i = 0; i < nparams; i++) { for (i = 0; i < nparams; i++) {
ignore_value(virStrcpyStatic((*dst)[i].field, src[i].field)); ignore_value(virStrcpyStatic((*dst)[i].field, src[i].field));
@ -399,8 +397,7 @@ virTypedParamsFilter(virTypedParameterPtr params,
{ {
size_t i, n = 0; size_t i, n = 0;
if (VIR_ALLOC_N(*ret, nparams) < 0) *ret = g_new0(virTypedParameterPtr, nparams);
return -1;
for (i = 0; i < nparams; i++) { for (i = 0; i < nparams; i++) {
if (STREQ(params[i].field, name)) { if (STREQ(params[i].field, name)) {
@ -447,9 +444,8 @@ virTypedParamsGetStringList(virTypedParameterPtr params,
if (nfiltered < 0) if (nfiltered < 0)
goto error; goto error;
if (nfiltered && if (nfiltered)
VIR_ALLOC_N(*values, nfiltered) < 0) *values = g_new0(const char *, nfiltered);
goto error;
for (n = 0, i = 0; i < nfiltered; i++) { for (n = 0, i = 0; i < nfiltered; i++) {
if (filtered[i]->type == VIR_TYPED_PARAM_STRING) if (filtered[i]->type == VIR_TYPED_PARAM_STRING)
@ -551,8 +547,7 @@ virTypedParamsDeserialize(virTypedParameterRemotePtr remote_params,
goto cleanup; goto cleanup;
} }
} else { } else {
if (VIR_ALLOC_N(*params, remote_params_len) < 0) *params = g_new0(virTypedParameter, remote_params_len);
goto cleanup;
} }
*nparams = remote_params_len; *nparams = remote_params_len;
@ -662,8 +657,7 @@ virTypedParamsSerialize(virTypedParameterPtr params,
goto cleanup; goto cleanup;
} }
if (VIR_ALLOC_N(params_val, nparams) < 0) params_val = g_new0(struct _virTypedParameterRemote, nparams);
goto cleanup;
for (i = 0, j = 0; i < nparams; ++i) { for (i = 0, j = 0; i < nparams; ++i) {
virTypedParameterPtr param = params + i; virTypedParameterPtr param = params + i;

View File

@ -157,8 +157,7 @@ virURIParse(const char *uri)
return NULL; return NULL;
} }
if (VIR_ALLOC(ret) < 0) ret = g_new0(virURI, 1);
goto error;
ret->scheme = g_strdup(xmluri->scheme); ret->scheme = g_strdup(xmluri->scheme);
ret->server = g_strdup(xmluri->server); ret->server = g_strdup(xmluri->server);

View File

@ -312,8 +312,7 @@ virUSBDeviceNew(unsigned int bus,
{ {
virUSBDevicePtr dev; virUSBDevicePtr dev;
if (VIR_ALLOC(dev) < 0) dev = g_new0(virUSBDevice, 1);
return NULL;
dev->bus = bus; dev->bus = bus;
dev->dev = devno; dev->dev = devno;

View File

@ -447,8 +447,7 @@ char *virIndexToDiskName(int idx, const char *prefix)
offset = strlen(prefix); offset = strlen(prefix);
if (VIR_ALLOC_N(name, offset + i + 1)) name = g_new0(char, offset + i + 1);
return NULL;
strcpy(name, prefix); strcpy(name, prefix);
name[offset + i] = '\0'; name[offset + i] = '\0';
@ -618,8 +617,7 @@ virGetUserEnt(uid_t uid, char **name, gid_t *group, char **dir, char **shell, bo
if (val < 0) if (val < 0)
strbuflen = 1024; strbuflen = 1024;
if (VIR_ALLOC_N(strbuf, strbuflen) < 0) strbuf = g_new0(char, strbuflen);
return -1;
/* /*
* From the manpage (terrifying but true): * From the manpage (terrifying but true):
@ -688,8 +686,7 @@ static char *virGetGroupEnt(gid_t gid)
if (val < 0) if (val < 0)
strbuflen = 1024; strbuflen = 1024;
if (VIR_ALLOC_N(strbuf, strbuflen) < 0) strbuf = g_new0(char, strbuflen);
return NULL;
/* /*
* From the manpage (terrifying but true): * From the manpage (terrifying but true):
@ -774,8 +771,7 @@ virGetUserIDByName(const char *name, uid_t *uid, bool missing_ok)
if (val < 0) if (val < 0)
strbuflen = 1024; strbuflen = 1024;
if (VIR_ALLOC_N(strbuf, strbuflen) < 0) strbuf = g_new0(char, strbuflen);
goto cleanup;
while ((rc = getpwnam_r(name, &pwbuf, strbuf, strbuflen, &pw)) == ERANGE) { while ((rc = getpwnam_r(name, &pwbuf, strbuf, strbuflen, &pw)) == ERANGE) {
if (VIR_RESIZE_N(strbuf, strbuflen, strbuflen, strbuflen) < 0) if (VIR_RESIZE_N(strbuf, strbuflen, strbuflen, strbuflen) < 0)
@ -856,8 +852,7 @@ virGetGroupIDByName(const char *name, gid_t *gid, bool missing_ok)
if (val < 0) if (val < 0)
strbuflen = 1024; strbuflen = 1024;
if (VIR_ALLOC_N(strbuf, strbuflen) < 0) strbuf = g_new0(char, strbuflen);
goto cleanup;
while ((rc = getgrnam_r(name, &grbuf, strbuf, strbuflen, &gr)) == ERANGE) { while ((rc = getgrnam_r(name, &grbuf, strbuf, strbuflen, &gr)) == ERANGE) {
if (VIR_RESIZE_N(strbuf, strbuflen, strbuflen, strbuflen) < 0) if (VIR_RESIZE_N(strbuf, strbuflen, strbuflen, strbuflen) < 0)

View File

@ -672,9 +672,7 @@ virXPathNodeSet(const char *xpath,
ret = obj->nodesetval->nodeNr; ret = obj->nodesetval->nodeNr;
if (list != NULL && ret) { if (list != NULL && ret) {
if (VIR_ALLOC_N(*list, ret) < 0) *list = g_new0(xmlNodePtr, ret);
return -1;
memcpy(*list, obj->nodesetval->nodeTab, ret * sizeof(xmlNodePtr)); memcpy(*list, obj->nodesetval->nodeTab, ret * sizeof(xmlNodePtr));
} }
xmlXPathFreeObject(obj); xmlXPathFreeObject(obj);
@ -1237,8 +1235,7 @@ virXMLValidatorInit(const char *schemafile)
{ {
virXMLValidatorPtr validator = NULL; virXMLValidatorPtr validator = NULL;
if (VIR_ALLOC(validator) < 0) validator = g_new0(virXMLValidator, 1);
return NULL;
validator->schemafile = g_strdup(schemafile); validator->schemafile = g_strdup(schemafile);