[libvirt PATCH 12/12] util: o-z: use g_new0

Ján Tomko jtomko at redhat.com
Mon Oct 5 17:48:27 UTC 2020


Signed-off-by: Ján Tomko <jtomko at redhat.com>
---
 src/util/virpci.c               | 12 ++++--------
 src/util/virperf.c              |  3 +--
 src/util/virpolkit.c            |  3 +--
 src/util/virportallocator.c     |  3 +--
 src/util/virprocess.c           |  3 +--
 src/util/virrotatingfile.c      | 15 +++++----------
 src/util/virscsi.c              |  6 ++----
 src/util/virscsivhost.c         |  3 +--
 src/util/virseclabel.c          | 13 +++----------
 src/util/virstorageencryption.c | 16 +++++-----------
 src/util/virstring.c            |  3 +--
 src/util/virthreadpool.c        |  9 +++------
 src/util/virtime.c              |  6 ++----
 src/util/virtypedparam.c        | 20 +++++++-------------
 src/util/viruri.c               |  3 +--
 src/util/virusb.c               |  3 +--
 src/util/virutil.c              | 15 +++++----------
 src/util/virxml.c               |  7 ++-----
 18 files changed, 46 insertions(+), 97 deletions(-)

diff --git a/src/util/virpci.c b/src/util/virpci.c
index 47c671daa0..6fa8acd246 100644
--- a/src/util/virpci.c
+++ b/src/util/virpci.c
@@ -1372,8 +1372,7 @@ virPCIDeviceNew(unsigned int domain,
     g_autofree char *vendor = NULL;
     g_autofree char *product = NULL;
 
-    if (VIR_ALLOC(dev) < 0)
-        return NULL;
+    dev = g_new0(virPCIDevice, 1);
 
     dev->address.domain = domain;
     dev->address.bus = bus;
@@ -1422,8 +1421,7 @@ virPCIDeviceCopy(virPCIDevicePtr dev)
 {
     virPCIDevicePtr copy;
 
-    if (VIR_ALLOC(copy) < 0)
-        return NULL;
+    copy = g_new0(virPCIDevice, 1);
 
     /* shallow copy to take care of most attributes */
     *copy = *dev;
@@ -1871,8 +1869,7 @@ virPCIGetIOMMUGroupAddressesAddOne(virPCIDeviceAddressPtr newDevAddr, void *opaq
     virPCIDeviceAddressPtr copyAddr;
 
     /* make a copy to insert onto the list */
-    if (VIR_ALLOC(copyAddr) < 0)
-        goto cleanup;
+    copyAddr = g_new0(virPCIDeviceAddress, 1);
 
     *copyAddr = *newDevAddr;
 
@@ -2204,8 +2201,7 @@ virPCIGetDeviceAddressFromSysfsLink(const char *device_link)
     }
 
     config_address = g_path_get_basename(device_path);
-    if (VIR_ALLOC(bdf) < 0)
-        return NULL;
+    bdf = g_new0(virPCIDeviceAddress, 1);
 
     if (virPCIDeviceAddressParse(config_address, bdf) < 0) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
diff --git a/src/util/virperf.c b/src/util/virperf.c
index 2e95509caf..7f260d2da6 100644
--- a/src/util/virperf.c
+++ b/src/util/virperf.c
@@ -368,8 +368,7 @@ virPerfNew(void)
     size_t i;
     virPerfPtr perf;
 
-    if (VIR_ALLOC(perf) < 0)
-        return NULL;
+    perf = g_new0(virPerf, 1);
 
     for (i = 0; i < VIR_PERF_EVENT_LAST; i++) {
         perf->events[i].fd = -1;
diff --git a/src/util/virpolkit.c b/src/util/virpolkit.c
index aad924a065..1d6be443f7 100644
--- a/src/util/virpolkit.c
+++ b/src/util/virpolkit.c
@@ -178,8 +178,7 @@ virPolkitAgentCreate(void)
     if (virPipe(pipe_fd) < 0)
         goto error;
 
-    if (VIR_ALLOC(agent) < 0)
-        goto error;
+    agent = g_new0(virPolkitAgent, 1);
 
     agent->cmd = virCommandNewArgList(PKTTYAGENT, "--process", NULL);
 
diff --git a/src/util/virportallocator.c b/src/util/virportallocator.c
index f450740318..76c6e43726 100644
--- a/src/util/virportallocator.c
+++ b/src/util/virportallocator.c
@@ -102,8 +102,7 @@ virPortAllocatorRangeNew(const char *name,
         return NULL;
     }
 
-    if (VIR_ALLOC(range) < 0)
-        return NULL;
+    range = g_new0(virPortAllocatorRange, 1);
 
     range->start = start;
     range->end = end;
diff --git a/src/util/virprocess.c b/src/util/virprocess.c
index d379f7446f..9366f0630c 100644
--- a/src/util/virprocess.c
+++ b/src/util/virprocess.c
@@ -1351,8 +1351,7 @@ virProcessNamespaceAvailable(unsigned int ns)
     /* Signal parent as soon as the child dies. RIP. */
     flags |= SIGCHLD;
 
-    if (VIR_ALLOC_N(stack, stacksize) < 0)
-        return -1;
+    stack = g_new0(char, stacksize);
 
     childStack = stack + stacksize;
 
diff --git a/src/util/virrotatingfile.c b/src/util/virrotatingfile.c
index b77e30dba7..a88c332cf4 100644
--- a/src/util/virrotatingfile.c
+++ b/src/util/virrotatingfile.c
@@ -105,8 +105,7 @@ virRotatingFileWriterEntryNew(const char *path,
 
     VIR_DEBUG("Opening %s mode=0%02o", path, mode);
 
-    if (VIR_ALLOC(entry) < 0)
-        return NULL;
+    entry = g_new0(virRotatingFileWriterEntry, 1);
 
     if ((entry->fd = open(path, O_CREAT|O_APPEND|O_WRONLY|O_CLOEXEC, mode)) < 0) {
         virReportSystemError(errno,
@@ -148,8 +147,7 @@ virRotatingFileReaderEntryNew(const char *path)
 
     VIR_DEBUG("Opening %s", path);
 
-    if (VIR_ALLOC(entry) < 0)
-        return NULL;
+    entry = g_new0(virRotatingFileReaderEntry, 1);
 
     if ((entry->fd = open(path, O_RDONLY|O_CLOEXEC)) < 0) {
         if (errno != ENOENT) {
@@ -238,8 +236,7 @@ virRotatingFileWriterNew(const char *path,
 {
     virRotatingFileWriterPtr file;
 
-    if (VIR_ALLOC(file) < 0)
-        goto error;
+    file = g_new0(virRotatingFileWriter, 1);
 
     file->basepath = g_strdup(path);
 
@@ -288,8 +285,7 @@ virRotatingFileReaderNew(const char *path,
     virRotatingFileReaderPtr file;
     size_t i;
 
-    if (VIR_ALLOC(file) < 0)
-        goto error;
+    file = g_new0(virRotatingFileReader, 1);
 
     if (maxbackup > VIR_MAX_MAX_BACKUP) {
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
@@ -299,8 +295,7 @@ virRotatingFileReaderNew(const char *path,
     }
 
     file->nentries = maxbackup + 1;
-    if (VIR_ALLOC_N(file->entries, file->nentries) < 0)
-        goto error;
+    file->entries = g_new0(virRotatingFileReaderEntryPtr, file->nentries);
 
     if (!(file->entries[file->nentries - 1] = virRotatingFileReaderEntryNew(path)))
         goto error;
diff --git a/src/util/virscsi.c b/src/util/virscsi.c
index e3bd81b569..4dcd68d53a 100644
--- a/src/util/virscsi.c
+++ b/src/util/virscsi.c
@@ -186,8 +186,7 @@ virSCSIDeviceNew(const char *sysfs_prefix,
     g_autofree char *model = NULL;
     const char *prefix = sysfs_prefix ? sysfs_prefix : SYSFS_SCSI_DEVICES;
 
-    if (VIR_ALLOC(dev) < 0)
-        return NULL;
+    dev = g_new0(virSCSIDevice, 1);
 
     dev->bus = bus;
     dev->target = target;
@@ -263,8 +262,7 @@ virSCSIDeviceSetUsedBy(virSCSIDevicePtr dev,
 {
     g_autoptr(virUsedByInfo) copy = NULL;
 
-    if (VIR_ALLOC(copy) < 0)
-        return -1;
+    copy = g_new0(virUsedByInfo, 1);
     copy->drvname = g_strdup(drvname);
     copy->domname = g_strdup(domname);
 
diff --git a/src/util/virscsivhost.c b/src/util/virscsivhost.c
index be1f57c6e2..a2bf9a446f 100644
--- a/src/util/virscsivhost.c
+++ b/src/util/virscsivhost.c
@@ -250,8 +250,7 @@ virSCSIVHostDeviceNew(const char *name)
 {
     g_autoptr(virSCSIVHostDevice) dev = NULL;
 
-    if (VIR_ALLOC(dev) < 0)
-        return NULL;
+    dev = g_new0(virSCSIVHostDevice, 1);
 
     dev->name = g_strdup(name);
 
diff --git a/src/util/virseclabel.c b/src/util/virseclabel.c
index 2141d84210..174f344a8b 100644
--- a/src/util/virseclabel.c
+++ b/src/util/virseclabel.c
@@ -58,10 +58,7 @@ virSecurityLabelDefNew(const char *model)
 {
     virSecurityLabelDefPtr seclabel = NULL;
 
-    if (VIR_ALLOC(seclabel) < 0) {
-        virSecurityLabelDefFree(seclabel);
-        return NULL;
-    }
+    seclabel = g_new0(virSecurityLabelDef, 1);
 
     seclabel->model = g_strdup(model);
 
@@ -75,10 +72,7 @@ virSecurityDeviceLabelDefNew(const char *model)
 {
     virSecurityDeviceLabelDefPtr seclabel = NULL;
 
-    if (VIR_ALLOC(seclabel) < 0) {
-        virSecurityDeviceLabelDefFree(seclabel);
-        return NULL;
-    }
+    seclabel = g_new0(virSecurityDeviceLabelDef, 1);
 
     seclabel->model = g_strdup(model);
 
@@ -91,8 +85,7 @@ virSecurityDeviceLabelDefCopy(const virSecurityDeviceLabelDef *src)
 {
     virSecurityDeviceLabelDefPtr ret;
 
-    if (VIR_ALLOC(ret) < 0)
-        return NULL;
+    ret = g_new0(virSecurityDeviceLabelDef, 1);
 
     ret->relabel = src->relabel;
     ret->labelskip = src->labelskip;
diff --git a/src/util/virstorageencryption.c b/src/util/virstorageencryption.c
index 76958b3876..399c6926bd 100644
--- a/src/util/virstorageencryption.c
+++ b/src/util/virstorageencryption.c
@@ -115,12 +115,9 @@ virStorageEncryptionCopy(const virStorageEncryption *src)
     virStorageEncryptionPtr ret;
     size_t i;
 
-    if (VIR_ALLOC(ret) < 0)
-        return NULL;
-
-    if (VIR_ALLOC_N(ret->secrets, src->nsecrets) < 0)
-        goto error;
+    ret = g_new0(virStorageEncryption, 1);
 
+    ret->secrets = g_new0(virStorageEncryptionSecretPtr, src->nsecrets);
     ret->nsecrets = src->nsecrets;
     ret->format = src->format;
 
@@ -147,8 +144,7 @@ virStorageEncryptionSecretParse(xmlXPathContextPtr ctxt,
     virStorageEncryptionSecretPtr ret;
     char *type_str = NULL;
 
-    if (VIR_ALLOC(ret) < 0)
-        return NULL;
+    ret = g_new0(virStorageEncryptionSecret, 1);
 
     ctxt->node = node;
 
@@ -247,8 +243,7 @@ virStorageEncryptionParseNode(xmlNodePtr node,
 
     ctxt->node = node;
 
-    if (VIR_ALLOC(encdef) < 0)
-        goto cleanup;
+    encdef = g_new0(virStorageEncryption, 1);
 
     if (!(format_str = virXPathString("string(./@format)", ctxt))) {
         virReportError(VIR_ERR_XML_ERROR, "%s",
@@ -268,8 +263,7 @@ virStorageEncryptionParseNode(xmlNodePtr node,
         goto cleanup;
 
     if (n > 0) {
-        if (VIR_ALLOC_N(encdef->secrets, n) < 0)
-            goto cleanup;
+        encdef->secrets = g_new0(virStorageEncryptionSecretPtr, n);
         encdef->nsecrets = n;
 
         for (i = 0; i < n; i++) {
diff --git a/src/util/virstring.c b/src/util/virstring.c
index de2ef964f4..5c49b56f75 100644
--- a/src/util/virstring.c
+++ b/src/util/virstring.c
@@ -1222,8 +1222,7 @@ virStringToUpper(char **dst, const char *src)
     if (!src)
         return 0;
 
-    if (VIR_ALLOC_N(cap, strlen(src) + 1) < 0)
-        return -1;
+    cap = g_new0(char, strlen(src) + 1);
 
     for (i = 0; src[i]; i++) {
         cap[i] = g_ascii_toupper(src[i]);
diff --git a/src/util/virthreadpool.c b/src/util/virthreadpool.c
index ca44f55c1b..3942633431 100644
--- a/src/util/virthreadpool.c
+++ b/src/util/virthreadpool.c
@@ -188,9 +188,8 @@ virThreadPoolExpand(virThreadPoolPtr pool, size_t gain, bool priority)
 
     for (i = 0; i < gain; i++) {
         g_autofree char *name = NULL;
-        if (VIR_ALLOC(data) < 0)
-            goto error;
 
+        data = g_new0(struct virThreadPoolWorkerData, 1);
         data->pool = pool;
         data->cond = priority ? &pool->prioCond : &pool->cond;
         data->priority = priority;
@@ -232,8 +231,7 @@ virThreadPoolNewFull(size_t minWorkers,
     if (minWorkers > maxWorkers)
         minWorkers = maxWorkers;
 
-    if (VIR_ALLOC(pool) < 0)
-        return NULL;
+    pool = g_new0(virThreadPool, 1);
 
     pool->jobList.tail = pool->jobList.head = NULL;
 
@@ -403,8 +401,7 @@ int virThreadPoolSendJob(virThreadPoolPtr pool,
         virThreadPoolExpand(pool, 1, false) < 0)
         goto error;
 
-    if (VIR_ALLOC(job) < 0)
-        goto error;
+    job = g_new0(virThreadPoolJob, 1);
 
     job->data = jobData;
     job->priority = priority;
diff --git a/src/util/virtime.c b/src/util/virtime.c
index 88f6f0a551..baf4ce5db7 100644
--- a/src/util/virtime.c
+++ b/src/util/virtime.c
@@ -257,8 +257,7 @@ char *virTimeStringNow(void)
 {
     char *ret;
 
-    if (VIR_ALLOC_N(ret, VIR_TIME_STRING_BUFLEN) < 0)
-        return NULL;
+    ret = g_new0(char, VIR_TIME_STRING_BUFLEN);
 
     if (virTimeStringNowRaw(ret) < 0) {
         virReportSystemError(errno, "%s",
@@ -286,8 +285,7 @@ char *virTimeStringThen(unsigned long long when)
 {
     char *ret;
 
-    if (VIR_ALLOC_N(ret, VIR_TIME_STRING_BUFLEN) < 0)
-        return NULL;
+    ret = g_new0(char, VIR_TIME_STRING_BUFLEN);
 
     if (virTimeStringThenRaw(when, ret) < 0) {
         virReportSystemError(errno, "%s",
diff --git a/src/util/virtypedparam.c b/src/util/virtypedparam.c
index e4e50d837a..43af4a7c56 100644
--- a/src/util/virtypedparam.c
+++ b/src/util/virtypedparam.c
@@ -68,8 +68,7 @@ virTypedParamsValidate(virTypedParameterPtr params, int nparams, ...)
 
     va_start(ap, nparams);
 
-    if (VIR_ALLOC_N(sorted, nparams) < 0)
-        goto cleanup;
+    sorted = g_new0(virTypedParameter, nparams);
 
     /* Here we intentionally don't copy values */
     memcpy(sorted, params, sizeof(*params) * nparams);
@@ -361,8 +360,7 @@ virTypedParamsCopy(virTypedParameterPtr *dst,
     if (!src || nparams <= 0)
         return 0;
 
-    if (VIR_ALLOC_N(*dst, nparams) < 0)
-        return -1;
+    *dst = g_new0(virTypedParameter, nparams);
 
     for (i = 0; i < nparams; i++) {
         ignore_value(virStrcpyStatic((*dst)[i].field, src[i].field));
@@ -399,8 +397,7 @@ virTypedParamsFilter(virTypedParameterPtr params,
 {
     size_t i, n = 0;
 
-    if (VIR_ALLOC_N(*ret, nparams) < 0)
-        return -1;
+    *ret = g_new0(virTypedParameterPtr, nparams);
 
     for (i = 0; i < nparams; i++) {
         if (STREQ(params[i].field, name)) {
@@ -447,9 +444,8 @@ virTypedParamsGetStringList(virTypedParameterPtr params,
     if (nfiltered < 0)
         goto error;
 
-    if (nfiltered &&
-        VIR_ALLOC_N(*values, nfiltered) < 0)
-        goto error;
+    if (nfiltered)
+        *values = g_new0(const char *, nfiltered);
 
     for (n = 0, i = 0; i < nfiltered; i++) {
         if (filtered[i]->type == VIR_TYPED_PARAM_STRING)
@@ -551,8 +547,7 @@ virTypedParamsDeserialize(virTypedParameterRemotePtr remote_params,
             goto cleanup;
         }
     } else {
-        if (VIR_ALLOC_N(*params, remote_params_len) < 0)
-            goto cleanup;
+        *params = g_new0(virTypedParameter, remote_params_len);
     }
     *nparams = remote_params_len;
 
@@ -662,8 +657,7 @@ virTypedParamsSerialize(virTypedParameterPtr params,
         goto cleanup;
     }
 
-    if (VIR_ALLOC_N(params_val, nparams) < 0)
-        goto cleanup;
+    params_val = g_new0(struct _virTypedParameterRemote, nparams);
 
     for (i = 0, j = 0; i < nparams; ++i) {
         virTypedParameterPtr param = params + i;
diff --git a/src/util/viruri.c b/src/util/viruri.c
index dd7559662b..11753a0aef 100644
--- a/src/util/viruri.c
+++ b/src/util/viruri.c
@@ -157,8 +157,7 @@ virURIParse(const char *uri)
         return NULL;
     }
 
-    if (VIR_ALLOC(ret) < 0)
-        goto error;
+    ret = g_new0(virURI, 1);
 
     ret->scheme = g_strdup(xmluri->scheme);
     ret->server = g_strdup(xmluri->server);
diff --git a/src/util/virusb.c b/src/util/virusb.c
index a28604c3f4..3e7e4281a3 100644
--- a/src/util/virusb.c
+++ b/src/util/virusb.c
@@ -312,8 +312,7 @@ virUSBDeviceNew(unsigned int bus,
 {
     virUSBDevicePtr dev;
 
-    if (VIR_ALLOC(dev) < 0)
-        return NULL;
+    dev = g_new0(virUSBDevice, 1);
 
     dev->bus     = bus;
     dev->dev     = devno;
diff --git a/src/util/virutil.c b/src/util/virutil.c
index 48b38c705b..708c3cf9ef 100644
--- a/src/util/virutil.c
+++ b/src/util/virutil.c
@@ -447,8 +447,7 @@ char *virIndexToDiskName(int idx, const char *prefix)
 
     offset = strlen(prefix);
 
-    if (VIR_ALLOC_N(name, offset + i + 1))
-        return NULL;
+    name = g_new0(char, offset + i + 1);
 
     strcpy(name, prefix);
     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)
         strbuflen = 1024;
 
-    if (VIR_ALLOC_N(strbuf, strbuflen) < 0)
-        return -1;
+    strbuf = g_new0(char, strbuflen);
 
     /*
      * From the manpage (terrifying but true):
@@ -688,8 +686,7 @@ static char *virGetGroupEnt(gid_t gid)
     if (val < 0)
         strbuflen = 1024;
 
-    if (VIR_ALLOC_N(strbuf, strbuflen) < 0)
-        return NULL;
+    strbuf = g_new0(char, strbuflen);
 
     /*
      * From the manpage (terrifying but true):
@@ -774,8 +771,7 @@ virGetUserIDByName(const char *name, uid_t *uid, bool missing_ok)
     if (val < 0)
         strbuflen = 1024;
 
-    if (VIR_ALLOC_N(strbuf, strbuflen) < 0)
-        goto cleanup;
+    strbuf = g_new0(char, strbuflen);
 
     while ((rc = getpwnam_r(name, &pwbuf, strbuf, strbuflen, &pw)) == ERANGE) {
         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)
         strbuflen = 1024;
 
-    if (VIR_ALLOC_N(strbuf, strbuflen) < 0)
-        goto cleanup;
+    strbuf = g_new0(char, strbuflen);
 
     while ((rc = getgrnam_r(name, &grbuf, strbuf, strbuflen, &gr)) == ERANGE) {
         if (VIR_RESIZE_N(strbuf, strbuflen, strbuflen, strbuflen) < 0)
diff --git a/src/util/virxml.c b/src/util/virxml.c
index 1927ff490f..b4d6684560 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -672,9 +672,7 @@ virXPathNodeSet(const char *xpath,
 
     ret = obj->nodesetval->nodeNr;
     if (list != NULL && ret) {
-        if (VIR_ALLOC_N(*list, ret) < 0)
-            return -1;
-
+        *list = g_new0(xmlNodePtr, ret);
         memcpy(*list, obj->nodesetval->nodeTab, ret * sizeof(xmlNodePtr));
     }
     xmlXPathFreeObject(obj);
@@ -1237,8 +1235,7 @@ virXMLValidatorInit(const char *schemafile)
 {
     virXMLValidatorPtr validator = NULL;
 
-    if (VIR_ALLOC(validator) < 0)
-        return NULL;
+    validator = g_new0(virXMLValidator, 1);
 
     validator->schemafile = g_strdup(schemafile);
 
-- 
2.26.2




More information about the libvir-list mailing list