[libvirt PATCH 0/5] Use g_new0 more (glib chronicles)

Remove VIR_ALLOC* from tools/ (with the exception of vsh*alloc, which will be cleaned up later) and some random subdirs from src/ Ján Tomko (5): node_device: use g_new0 instead of VIR_ALLOC* openvz: use g_new0 instead of VIR_ALLOC* secret: use g_new0 instead of VIR_ALLOC* security: use g_new0 instead of VIR_ALLOC* tools: use g_new0 instead of VIR_ALLOC* src/node_device/node_device_udev.c | 27 +++++++++------------------ src/openvz/openvz_conf.c | 12 ++++-------- src/openvz/openvz_driver.c | 3 +-- src/secret/secret_driver.c | 3 +-- src/security/security_apparmor.c | 3 +-- src/security/security_dac.c | 9 +++------ src/security/security_manager.c | 14 +++++--------- src/security/security_selinux.c | 9 +++------ src/security/security_stack.c | 6 ++---- tools/virsh-domain-monitor.c | 11 +++-------- tools/virsh-domain.c | 26 ++++++++------------------ tools/virt-admin-completer.c | 12 +----------- tools/virt-login-shell-helper.c | 13 ++++--------- tools/vsh-table.c | 21 +++++++-------------- tools/vsh.c | 5 +---- 15 files changed, 53 insertions(+), 121 deletions(-) -- 2.26.2

Signed-off-by: Ján Tomko <jtomko@redhat.com> --- src/node_device/node_device_udev.c | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/src/node_device/node_device_udev.c b/src/node_device/node_device_udev.c index 12e3f30bad..2d0ca27fc6 100644 --- a/src/node_device/node_device_udev.c +++ b/src/node_device/node_device_udev.c @@ -413,13 +413,11 @@ udevProcessPCI(struct udev_device *device, goto cleanup; if (virPCIDeviceIsPCIExpress(pciDev) > 0) { - if (VIR_ALLOC(pci_express) < 0) - goto cleanup; + pci_express = g_new0(virPCIEDeviceInfo, 1); if (virPCIDeviceHasPCIExpressLink(pciDev) > 0) { - if (VIR_ALLOC(pci_express->link_cap) < 0 || - VIR_ALLOC(pci_express->link_sta) < 0) - goto cleanup; + pci_express->link_cap = g_new0(virPCIELink, 1); + pci_express->link_sta = g_new0(virPCIELink, 1); if (virPCIDeviceGetLinkCapSta(pciDev, &pci_express->link_cap->port, @@ -1159,8 +1157,7 @@ udevGetDeviceNodes(struct udev_device *device, udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(device)) n++; - if (VIR_ALLOC_N(def->devlinks, n + 1) < 0) - return -1; + def->devlinks = g_new0(char *, n + 1); n = 0; udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(device)) { @@ -1371,16 +1368,14 @@ udevAddOneDevice(struct udev_device *device) bool new_device = true; int ret = -1; - if (VIR_ALLOC(def) != 0) - goto cleanup; + def = g_new0(virNodeDeviceDef, 1); def->sysfs_path = g_strdup(udev_device_get_syspath(device)); if (udevGetStringProperty(device, "DRIVER", &def->driver) < 0) goto cleanup; - if (VIR_ALLOC(def->caps) != 0) - goto cleanup; + def->caps = g_new0(virNodeDevCapsDef, 1); if (udevGetDeviceType(device, &def->caps->data.type) != 0) goto cleanup; @@ -1788,13 +1783,10 @@ udevSetupSystemDev(void) virNodeDeviceObjPtr obj = NULL; int ret = -1; - if (VIR_ALLOC(def) < 0) - return -1; + def = g_new0(virNodeDeviceDef, 1); def->name = g_strdup("computer"); - - if (VIR_ALLOC(def->caps) != 0) - goto cleanup; + def->caps = g_new0(virNodeDevCapsDef, 1); #if defined(__x86_64__) || defined(__i386__) || defined(__amd64__) udevGetDMIData(&def->caps->data.system); @@ -1882,8 +1874,7 @@ nodeStateInitialize(bool privileged, return -1; } - if (VIR_ALLOC(driver) < 0) - return VIR_DRV_STATE_INIT_ERROR; + driver = g_new0(virNodeDeviceDriverState, 1); driver->lockFD = -1; if (virMutexInit(&driver->lock) < 0) { -- 2.26.2

On 9/23/20 5:11 PM, Ján Tomko wrote:
Signed-off-by: Ján Tomko <jtomko@redhat.com> ---
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
src/node_device/node_device_udev.c | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-)
diff --git a/src/node_device/node_device_udev.c b/src/node_device/node_device_udev.c index 12e3f30bad..2d0ca27fc6 100644 --- a/src/node_device/node_device_udev.c +++ b/src/node_device/node_device_udev.c @@ -413,13 +413,11 @@ udevProcessPCI(struct udev_device *device, goto cleanup;
if (virPCIDeviceIsPCIExpress(pciDev) > 0) { - if (VIR_ALLOC(pci_express) < 0) - goto cleanup; + pci_express = g_new0(virPCIEDeviceInfo, 1);
if (virPCIDeviceHasPCIExpressLink(pciDev) > 0) { - if (VIR_ALLOC(pci_express->link_cap) < 0 || - VIR_ALLOC(pci_express->link_sta) < 0) - goto cleanup; + pci_express->link_cap = g_new0(virPCIELink, 1); + pci_express->link_sta = g_new0(virPCIELink, 1);
if (virPCIDeviceGetLinkCapSta(pciDev, &pci_express->link_cap->port, @@ -1159,8 +1157,7 @@ udevGetDeviceNodes(struct udev_device *device, udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(device)) n++;
- if (VIR_ALLOC_N(def->devlinks, n + 1) < 0) - return -1; + def->devlinks = g_new0(char *, n + 1);
n = 0; udev_list_entry_foreach(list_entry, udev_device_get_devlinks_list_entry(device)) { @@ -1371,16 +1368,14 @@ udevAddOneDevice(struct udev_device *device) bool new_device = true; int ret = -1;
- if (VIR_ALLOC(def) != 0) - goto cleanup; + def = g_new0(virNodeDeviceDef, 1);
def->sysfs_path = g_strdup(udev_device_get_syspath(device));
if (udevGetStringProperty(device, "DRIVER", &def->driver) < 0) goto cleanup;
- if (VIR_ALLOC(def->caps) != 0) - goto cleanup; + def->caps = g_new0(virNodeDevCapsDef, 1);
if (udevGetDeviceType(device, &def->caps->data.type) != 0) goto cleanup; @@ -1788,13 +1783,10 @@ udevSetupSystemDev(void) virNodeDeviceObjPtr obj = NULL; int ret = -1;
- if (VIR_ALLOC(def) < 0) - return -1; + def = g_new0(virNodeDeviceDef, 1);
def->name = g_strdup("computer"); - - if (VIR_ALLOC(def->caps) != 0) - goto cleanup; + def->caps = g_new0(virNodeDevCapsDef, 1);
#if defined(__x86_64__) || defined(__i386__) || defined(__amd64__) udevGetDMIData(&def->caps->data.system); @@ -1882,8 +1874,7 @@ nodeStateInitialize(bool privileged, return -1; }
- if (VIR_ALLOC(driver) < 0) - return VIR_DRV_STATE_INIT_ERROR; + driver = g_new0(virNodeDeviceDriverState, 1);
driver->lockFD = -1; if (virMutexInit(&driver->lock) < 0) {

Signed-off-by: Ján Tomko <jtomko@redhat.com> --- src/openvz/openvz_conf.c | 12 ++++-------- src/openvz/openvz_driver.c | 3 +-- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/src/openvz/openvz_conf.c b/src/openvz/openvz_conf.c index 6d54123a35..8f1740863c 100644 --- a/src/openvz/openvz_conf.c +++ b/src/openvz/openvz_conf.c @@ -205,8 +205,7 @@ openvzReadNetworkConf(virDomainDefPtr def, } else if (ret > 0) { token = strtok_r(temp, " ", &saveptr); while (token != NULL) { - if (VIR_ALLOC(net) < 0) - goto error; + net = g_new0(virDomainNetDef, 1); net->type = VIR_DOMAIN_NET_TYPE_ETHERNET; if (virDomainNetAppendIPAddress(net, token, AF_UNSPEC, 0) < 0) @@ -238,8 +237,7 @@ openvzReadNetworkConf(virDomainDefPtr def, int len; /* add new device to list */ - if (VIR_ALLOC(net) < 0) - goto error; + net = g_new0(virDomainNetDef, 1); net->type = VIR_DOMAIN_NET_TYPE_BRIDGE; @@ -259,8 +257,7 @@ openvzReadNetworkConf(virDomainDefPtr def, goto error; } - if (VIR_ALLOC_N(net->ifname, len+1) < 0) - goto error; + net->ifname = g_new0(char, len+1); if (virStrncpy(net->ifname, p, len, len+1) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, @@ -276,8 +273,7 @@ openvzReadNetworkConf(virDomainDefPtr def, goto error; } - if (VIR_ALLOC_N(net->data.bridge.brname, len+1) < 0) - goto error; + net->data.bridge.brname = g_new0(char, len+1); if (virStrncpy(net->data.bridge.brname, p, len, len+1) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, diff --git a/src/openvz/openvz_driver.c b/src/openvz/openvz_driver.c index 71e270ea09..f90ae4fe69 100644 --- a/src/openvz/openvz_driver.c +++ b/src/openvz/openvz_driver.c @@ -1287,8 +1287,7 @@ static virDrvOpenStatus openvzConnectOpen(virConnectPtr conn, /* We now know the URI is definitely for this driver, so beyond * here, don't return DECLINED, always use ERROR */ - if (VIR_ALLOC(driver) < 0) - return VIR_DRV_OPEN_ERROR; + driver = g_new0(struct openvz_driver, 1); if (!(driver->domains = virDomainObjListNew())) goto cleanup; -- 2.26.2

On 9/23/20 5:11 PM, Ján Tomko wrote:
Signed-off-by: Ján Tomko <jtomko@redhat.com> ---
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
src/openvz/openvz_conf.c | 12 ++++-------- src/openvz/openvz_driver.c | 3 +-- 2 files changed, 5 insertions(+), 10 deletions(-)
diff --git a/src/openvz/openvz_conf.c b/src/openvz/openvz_conf.c index 6d54123a35..8f1740863c 100644 --- a/src/openvz/openvz_conf.c +++ b/src/openvz/openvz_conf.c @@ -205,8 +205,7 @@ openvzReadNetworkConf(virDomainDefPtr def, } else if (ret > 0) { token = strtok_r(temp, " ", &saveptr); while (token != NULL) { - if (VIR_ALLOC(net) < 0) - goto error; + net = g_new0(virDomainNetDef, 1);
net->type = VIR_DOMAIN_NET_TYPE_ETHERNET; if (virDomainNetAppendIPAddress(net, token, AF_UNSPEC, 0) < 0) @@ -238,8 +237,7 @@ openvzReadNetworkConf(virDomainDefPtr def, int len;
/* add new device to list */ - if (VIR_ALLOC(net) < 0) - goto error; + net = g_new0(virDomainNetDef, 1);
net->type = VIR_DOMAIN_NET_TYPE_BRIDGE;
@@ -259,8 +257,7 @@ openvzReadNetworkConf(virDomainDefPtr def, goto error; }
- if (VIR_ALLOC_N(net->ifname, len+1) < 0) - goto error; + net->ifname = g_new0(char, len+1);
if (virStrncpy(net->ifname, p, len, len+1) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, @@ -276,8 +273,7 @@ openvzReadNetworkConf(virDomainDefPtr def, goto error; }
- if (VIR_ALLOC_N(net->data.bridge.brname, len+1) < 0) - goto error; + net->data.bridge.brname = g_new0(char, len+1);
if (virStrncpy(net->data.bridge.brname, p, len, len+1) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, diff --git a/src/openvz/openvz_driver.c b/src/openvz/openvz_driver.c index 71e270ea09..f90ae4fe69 100644 --- a/src/openvz/openvz_driver.c +++ b/src/openvz/openvz_driver.c @@ -1287,8 +1287,7 @@ static virDrvOpenStatus openvzConnectOpen(virConnectPtr conn, /* We now know the URI is definitely for this driver, so beyond * here, don't return DECLINED, always use ERROR */
- if (VIR_ALLOC(driver) < 0) - return VIR_DRV_OPEN_ERROR; + driver = g_new0(struct openvz_driver, 1);
if (!(driver->domains = virDomainObjListNew())) goto cleanup;

Signed-off-by: Ján Tomko <jtomko@redhat.com> --- src/secret/secret_driver.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/secret/secret_driver.c b/src/secret/secret_driver.c index 1cb342878f..45da09322b 100644 --- a/src/secret/secret_driver.c +++ b/src/secret/secret_driver.c @@ -459,8 +459,7 @@ secretStateInitialize(bool privileged, virStateInhibitCallback callback G_GNUC_UNUSED, void *opaque G_GNUC_UNUSED) { - if (VIR_ALLOC(driver) < 0) - return VIR_DRV_STATE_INIT_ERROR; + driver = g_new0(virSecretDriverState, 1); driver->lockFD = -1; if (virMutexInit(&driver->lock) < 0) { -- 2.26.2

On 9/23/20 5:11 PM, Ján Tomko wrote:
Signed-off-by: Ján Tomko <jtomko@redhat.com> ---
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
src/secret/secret_driver.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/src/secret/secret_driver.c b/src/secret/secret_driver.c index 1cb342878f..45da09322b 100644 --- a/src/secret/secret_driver.c +++ b/src/secret/secret_driver.c @@ -459,8 +459,7 @@ secretStateInitialize(bool privileged, virStateInhibitCallback callback G_GNUC_UNUSED, void *opaque G_GNUC_UNUSED) { - if (VIR_ALLOC(driver) < 0) - return VIR_DRV_STATE_INIT_ERROR; + driver = g_new0(virSecretDriverState, 1);
driver->lockFD = -1; if (virMutexInit(&driver->lock) < 0) {

Signed-off-by: Ján Tomko <jtomko@redhat.com> --- src/security/security_apparmor.c | 3 +-- src/security/security_dac.c | 9 +++------ src/security/security_manager.c | 14 +++++--------- src/security/security_selinux.c | 9 +++------ src/security/security_stack.c | 6 ++---- 5 files changed, 14 insertions(+), 27 deletions(-) diff --git a/src/security/security_apparmor.c b/src/security/security_apparmor.c index eea37dca83..c2d86c6940 100644 --- a/src/security/security_apparmor.c +++ b/src/security/security_apparmor.c @@ -867,8 +867,7 @@ AppArmorSetSecurityHostdevLabel(virSecurityManagerPtr mgr, if (profile_loaded(secdef->imagelabel) < 0) return 0; - if (VIR_ALLOC(ptr) < 0) - return -1; + ptr = g_new0(struct SDPDOP, 1); ptr->mgr = mgr; ptr->def = def; diff --git a/src/security/security_dac.c b/src/security/security_dac.c index d9d4cda159..258d246659 100644 --- a/src/security/security_dac.c +++ b/src/security/security_dac.c @@ -108,8 +108,7 @@ virSecurityDACChownListAppend(virSecurityDACChownListPtr list, char *tmp = NULL; virSecurityDACChownItemPtr item = NULL; - if (VIR_ALLOC(item) < 0) - return -1; + item = g_new0(virSecurityDACChownItem, 1); tmp = g_strdup(path); @@ -227,8 +226,7 @@ virSecurityDACTransactionRun(pid_t pid G_GNUC_UNUSED, int ret = -1; if (list->lock) { - if (VIR_ALLOC_N(paths, list->nItems) < 0) - return -1; + paths = g_new0(const char *, list->nItems); for (i = 0; i < list->nItems; i++) { virSecurityDACChownItemPtr item = list->items[i]; @@ -580,8 +578,7 @@ virSecurityDACTransactionStart(virSecurityManagerPtr mgr) return -1; } - if (VIR_ALLOC(list) < 0) - return -1; + list = g_new0(virSecurityDACChownList, 1); list->manager = virObjectRef(mgr); diff --git a/src/security/security_manager.c b/src/security/security_manager.c index 17b565cc12..be81ee5e44 100644 --- a/src/security/security_manager.c +++ b/src/security/security_manager.c @@ -87,8 +87,7 @@ virSecurityManagerNewDriver(virSecurityDriverPtr drv, virCheckFlags(VIR_SECURITY_MANAGER_NEW_MASK, NULL); - if (VIR_ALLOC_N(privateData, drv->privateDataLen) < 0) - return NULL; + privateData = g_new0(char, drv->privateDataLen); if (!(mgr = virObjectLockableNew(virSecurityManagerClass))) goto error; @@ -1034,8 +1033,7 @@ virSecurityManagerGetNested(virSecurityManagerPtr mgr) if (STREQ("stack", mgr->drv->name)) return virSecurityStackGetNested(mgr); - if (VIR_ALLOC_N(list, 2) < 0) - return NULL; + list = g_new0(virSecurityManagerPtr, 2); list[0] = mgr; list[1] = NULL; @@ -1346,9 +1344,8 @@ virSecurityManagerMetadataLock(virSecurityManagerPtr mgr G_GNUC_UNUSED, const char **locked_paths = NULL; virSecurityManagerMetadataLockStatePtr ret = NULL; - if (VIR_ALLOC_N(fds, npaths) < 0 || - VIR_ALLOC_N(locked_paths, npaths) < 0) - return NULL; + fds = g_new0(int, npaths); + locked_paths = g_new0(const char *, npaths); /* Sort paths to lock in order to avoid deadlocks with other * processes. For instance, if one process wants to lock @@ -1441,8 +1438,7 @@ virSecurityManagerMetadataLock(virSecurityManagerPtr mgr G_GNUC_UNUSED, VIR_APPEND_ELEMENT_COPY_INPLACE(fds, nfds, fd); } - if (VIR_ALLOC(ret) < 0) - goto cleanup; + ret = g_new0(virSecurityManagerMetadataLockState, 1); ret->paths = g_steal_pointer(&locked_paths); ret->fds = g_steal_pointer(&fds); diff --git a/src/security/security_selinux.c b/src/security/security_selinux.c index 87741d6dad..e40d670e97 100644 --- a/src/security/security_selinux.c +++ b/src/security/security_selinux.c @@ -123,8 +123,7 @@ virSecuritySELinuxContextListAppend(virSecuritySELinuxContextListPtr list, int ret = -1; virSecuritySELinuxContextItemPtr item = NULL; - if (VIR_ALLOC(item) < 0) - return -1; + item = g_new0(virSecuritySELinuxContextItem, 1); item->path = g_strdup(path); item->tcon = g_strdup(tcon); @@ -258,8 +257,7 @@ virSecuritySELinuxTransactionRun(pid_t pid G_GNUC_UNUSED, int ret = -1; if (list->lock) { - if (VIR_ALLOC_N(paths, list->nItems) < 0) - return -1; + paths = g_new0(const char *, list->nItems); for (i = 0; i < list->nItems; i++) { virSecuritySELinuxContextItemPtr item = list->items[i]; @@ -1088,8 +1086,7 @@ virSecuritySELinuxTransactionStart(virSecurityManagerPtr mgr) return -1; } - if (VIR_ALLOC(list) < 0) - return -1; + list = g_new0(virSecuritySELinuxContextList, 1); list->manager = virObjectRef(mgr); diff --git a/src/security/security_stack.c b/src/security/security_stack.c index 2480c47f70..3bfcb1e2f7 100644 --- a/src/security/security_stack.c +++ b/src/security/security_stack.c @@ -56,8 +56,7 @@ virSecurityStackAddNested(virSecurityManagerPtr mgr, while (tmp && tmp->next) tmp = tmp->next; - if (VIR_ALLOC(item) < 0) - return -1; + item = g_new0(virSecurityStackItem, 1); item->securityManager = nested; item->prev = tmp; if (tmp) @@ -620,8 +619,7 @@ virSecurityStackGetNested(virSecurityManagerPtr mgr) for (item = priv->itemsHead; item; item = item->next) len++; - if (VIR_ALLOC_N(list, len + 1) < 0) - return NULL; + list = g_new0(virSecurityManagerPtr, len + 1); for (i = 0, item = priv->itemsHead; item; item = item->next, i++) list[i] = item->securityManager; -- 2.26.2

On 9/23/20 5:11 PM, Ján Tomko wrote:
Signed-off-by: Ján Tomko <jtomko@redhat.com> ---
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
src/security/security_apparmor.c | 3 +-- src/security/security_dac.c | 9 +++------ src/security/security_manager.c | 14 +++++--------- src/security/security_selinux.c | 9 +++------ src/security/security_stack.c | 6 ++---- 5 files changed, 14 insertions(+), 27 deletions(-)
diff --git a/src/security/security_apparmor.c b/src/security/security_apparmor.c index eea37dca83..c2d86c6940 100644 --- a/src/security/security_apparmor.c +++ b/src/security/security_apparmor.c @@ -867,8 +867,7 @@ AppArmorSetSecurityHostdevLabel(virSecurityManagerPtr mgr, if (profile_loaded(secdef->imagelabel) < 0) return 0;
- if (VIR_ALLOC(ptr) < 0) - return -1; + ptr = g_new0(struct SDPDOP, 1); ptr->mgr = mgr; ptr->def = def;
diff --git a/src/security/security_dac.c b/src/security/security_dac.c index d9d4cda159..258d246659 100644 --- a/src/security/security_dac.c +++ b/src/security/security_dac.c @@ -108,8 +108,7 @@ virSecurityDACChownListAppend(virSecurityDACChownListPtr list, char *tmp = NULL; virSecurityDACChownItemPtr item = NULL;
- if (VIR_ALLOC(item) < 0) - return -1; + item = g_new0(virSecurityDACChownItem, 1);
tmp = g_strdup(path);
@@ -227,8 +226,7 @@ virSecurityDACTransactionRun(pid_t pid G_GNUC_UNUSED, int ret = -1;
if (list->lock) { - if (VIR_ALLOC_N(paths, list->nItems) < 0) - return -1; + paths = g_new0(const char *, list->nItems);
for (i = 0; i < list->nItems; i++) { virSecurityDACChownItemPtr item = list->items[i]; @@ -580,8 +578,7 @@ virSecurityDACTransactionStart(virSecurityManagerPtr mgr) return -1; }
- if (VIR_ALLOC(list) < 0) - return -1; + list = g_new0(virSecurityDACChownList, 1);
list->manager = virObjectRef(mgr);
diff --git a/src/security/security_manager.c b/src/security/security_manager.c index 17b565cc12..be81ee5e44 100644 --- a/src/security/security_manager.c +++ b/src/security/security_manager.c @@ -87,8 +87,7 @@ virSecurityManagerNewDriver(virSecurityDriverPtr drv,
virCheckFlags(VIR_SECURITY_MANAGER_NEW_MASK, NULL);
- if (VIR_ALLOC_N(privateData, drv->privateDataLen) < 0) - return NULL; + privateData = g_new0(char, drv->privateDataLen);
if (!(mgr = virObjectLockableNew(virSecurityManagerClass))) goto error; @@ -1034,8 +1033,7 @@ virSecurityManagerGetNested(virSecurityManagerPtr mgr) if (STREQ("stack", mgr->drv->name)) return virSecurityStackGetNested(mgr);
- if (VIR_ALLOC_N(list, 2) < 0) - return NULL; + list = g_new0(virSecurityManagerPtr, 2);
list[0] = mgr; list[1] = NULL; @@ -1346,9 +1344,8 @@ virSecurityManagerMetadataLock(virSecurityManagerPtr mgr G_GNUC_UNUSED, const char **locked_paths = NULL; virSecurityManagerMetadataLockStatePtr ret = NULL;
- if (VIR_ALLOC_N(fds, npaths) < 0 || - VIR_ALLOC_N(locked_paths, npaths) < 0) - return NULL; + fds = g_new0(int, npaths); + locked_paths = g_new0(const char *, npaths);
/* Sort paths to lock in order to avoid deadlocks with other * processes. For instance, if one process wants to lock @@ -1441,8 +1438,7 @@ virSecurityManagerMetadataLock(virSecurityManagerPtr mgr G_GNUC_UNUSED, VIR_APPEND_ELEMENT_COPY_INPLACE(fds, nfds, fd); }
- if (VIR_ALLOC(ret) < 0) - goto cleanup; + ret = g_new0(virSecurityManagerMetadataLockState, 1);
ret->paths = g_steal_pointer(&locked_paths); ret->fds = g_steal_pointer(&fds); diff --git a/src/security/security_selinux.c b/src/security/security_selinux.c index 87741d6dad..e40d670e97 100644 --- a/src/security/security_selinux.c +++ b/src/security/security_selinux.c @@ -123,8 +123,7 @@ virSecuritySELinuxContextListAppend(virSecuritySELinuxContextListPtr list, int ret = -1; virSecuritySELinuxContextItemPtr item = NULL;
- if (VIR_ALLOC(item) < 0) - return -1; + item = g_new0(virSecuritySELinuxContextItem, 1);
item->path = g_strdup(path); item->tcon = g_strdup(tcon); @@ -258,8 +257,7 @@ virSecuritySELinuxTransactionRun(pid_t pid G_GNUC_UNUSED, int ret = -1;
if (list->lock) { - if (VIR_ALLOC_N(paths, list->nItems) < 0) - return -1; + paths = g_new0(const char *, list->nItems);
for (i = 0; i < list->nItems; i++) { virSecuritySELinuxContextItemPtr item = list->items[i]; @@ -1088,8 +1086,7 @@ virSecuritySELinuxTransactionStart(virSecurityManagerPtr mgr) return -1; }
- if (VIR_ALLOC(list) < 0) - return -1; + list = g_new0(virSecuritySELinuxContextList, 1);
list->manager = virObjectRef(mgr);
diff --git a/src/security/security_stack.c b/src/security/security_stack.c index 2480c47f70..3bfcb1e2f7 100644 --- a/src/security/security_stack.c +++ b/src/security/security_stack.c @@ -56,8 +56,7 @@ virSecurityStackAddNested(virSecurityManagerPtr mgr, while (tmp && tmp->next) tmp = tmp->next;
- if (VIR_ALLOC(item) < 0) - return -1; + item = g_new0(virSecurityStackItem, 1); item->securityManager = nested; item->prev = tmp; if (tmp) @@ -620,8 +619,7 @@ virSecurityStackGetNested(virSecurityManagerPtr mgr) for (item = priv->itemsHead; item; item = item->next) len++;
- if (VIR_ALLOC_N(list, len + 1) < 0) - return NULL; + list = g_new0(virSecurityManagerPtr, len + 1);
for (i = 0, item = priv->itemsHead; item; item = item->next, i++) list[i] = item->securityManager;

With the exception of vsh*alloc. Signed-off-by: Ján Tomko <jtomko@redhat.com> --- tools/virsh-domain-monitor.c | 11 +++-------- tools/virsh-domain.c | 26 ++++++++------------------ tools/virt-admin-completer.c | 12 +----------- tools/virt-login-shell-helper.c | 13 ++++--------- tools/vsh-table.c | 21 +++++++-------------- tools/vsh.c | 5 +---- 6 files changed, 24 insertions(+), 64 deletions(-) diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c index 848efc8aa3..c8a7c0f1b7 100644 --- a/tools/virsh-domain-monitor.c +++ b/tools/virsh-domain-monitor.c @@ -1234,8 +1234,7 @@ cmdDomBlkError(vshControl *ctl, const vshCmd *cmd) ndisks = count; if (ndisks) { - if (VIR_ALLOC_N(disks, ndisks) < 0) - goto cleanup; + disks = g_new0(virDomainDiskError, ndisks); if ((count = virDomainGetDiskErrors(dom, disks, ndisks, 0)) == -1) goto cleanup; @@ -1378,10 +1377,7 @@ cmdDominfo(vshControl *ctl, const vshCmd *cmd) vshPrint(ctl, "%-15s %s\n", _("Security DOI:"), secmodel.doi); /* Security labels are only valid for active domains */ - if (VIR_ALLOC(seclabel) < 0) { - virshDomainFree(dom); - return false; - } + seclabel = g_new0(virSecurityLabel, 1); if (virDomainGetSecurityLabel(dom, seclabel) == -1) { virshDomainFree(dom); @@ -2280,8 +2276,7 @@ cmdDomstats(vshControl *ctl, const vshCmd *cmd) flags |= VIR_CONNECT_GET_ALL_DOMAINS_STATS_NOWAIT; if (vshCommandOptBool(cmd, "domain")) { - if (VIR_ALLOC_N(domlist, 1) < 0) - goto cleanup; + domlist = g_new0(virDomainPtr, 1); ndoms = 1; while ((opt = vshCommandOptArgv(ctl, cmd, opt))) { diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 32edfc0398..dfcba04682 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -1762,8 +1762,7 @@ virshBlockJobWaitInit(vshControl *ctl, virshBlockJobWaitDataPtr ret; virshControlPtr priv = ctl->privData; - if (VIR_ALLOC(ret) < 0) - return NULL; + ret = g_new0(virshBlockJobWaitData, 1); ret->ctl = ctl; ret->dom = dom; @@ -8177,8 +8176,7 @@ cmdCPUStats(vshControl *ctl, const vshCmd *cmd) goto cleanup; } - if (VIR_ALLOC_N(params, nparams * MIN(show_count, 128)) < 0) - goto cleanup; + params = g_new0(virTypedParameter, nparams * MIN(show_count, 128)); while (show_count) { int ncpus = MIN(show_count, 128); @@ -8215,8 +8213,7 @@ cmdCPUStats(vshControl *ctl, const vshCmd *cmd) goto cleanup; } - if (VIR_ALLOC_N(params, nparams) < 0) - goto cleanup; + params = g_new0(virTypedParameter, nparams); /* passing start_cpu == -1 gives us domain's total status */ if ((stats_per_cpu = virDomainGetCPUStats(dom, params, nparams, @@ -10086,14 +10083,9 @@ cmdLxcEnterNamespace(vshControl *ctl, const vshCmd *cmd) goto cleanup; if (setlabel) { - if (VIR_ALLOC(secmodel) < 0) { - vshError(ctl, "%s", _("Failed to allocate security model")); - goto cleanup; - } - if (VIR_ALLOC(seclabel) < 0) { - vshError(ctl, "%s", _("Failed to allocate security label")); - goto cleanup; - } + secmodel = g_new0(virSecurityModel, 1); + seclabel = g_new0(virSecurityLabel, 1); + if (virNodeGetSecurityModel(priv->conn, secmodel) < 0) goto cleanup; if (virDomainGetSecurityLabel(dom, seclabel) < 0) @@ -13737,8 +13729,7 @@ cmdEvent(vshControl *ctl, const vshCmd *cmd) } if (all) { - if (VIR_ALLOC_N(data, VIR_DOMAIN_EVENT_ID_LAST) < 0) - goto cleanup; + data = g_new0(virshDomEventData, VIR_DOMAIN_EVENT_ID_LAST); for (i = 0; i < VIR_DOMAIN_EVENT_ID_LAST; i++) { data[i].ctl = ctl; data[i].loop = loop; @@ -13748,8 +13739,7 @@ cmdEvent(vshControl *ctl, const vshCmd *cmd) data[i].id = -1; } } else { - if (VIR_ALLOC_N(data, 1) < 0) - goto cleanup; + data = g_new0(virshDomEventData, 1); data[0].ctl = ctl; data[0].loop = vshCommandOptBool(cmd, "loop"); data[0].count = &count; diff --git a/tools/virt-admin-completer.c b/tools/virt-admin-completer.c index 7201bb71eb..61004beeb5 100644 --- a/tools/virt-admin-completer.c +++ b/tools/virt-admin-completer.c @@ -46,8 +46,7 @@ vshAdmServerCompleter(vshControl *ctl, if ((nsrvs = virAdmConnectListServers(priv->conn, &srvs, 0)) < 0) return NULL; - if (VIR_ALLOC_N(ret, nsrvs + 1) < 0) - goto error; + ret = g_new0(char *, nsrvs + 1); for (i = 0; i < nsrvs; i++) { const char *name = virAdmServerGetName(srvs[i]); @@ -59,13 +58,4 @@ vshAdmServerCompleter(vshControl *ctl, VIR_FREE(srvs); return ret; - - error: - for (; i < nsrvs; i++) - virAdmServerFree(srvs[i]); - VIR_FREE(srvs); - for (i = 0; i < nsrvs; i++) - VIR_FREE(ret[i]); - VIR_FREE(ret); - return ret; } diff --git a/tools/virt-login-shell-helper.c b/tools/virt-login-shell-helper.c index cc6836382a..0e7987bf82 100644 --- a/tools/virt-login-shell-helper.c +++ b/tools/virt-login-shell-helper.c @@ -99,7 +99,7 @@ static int virLoginShellGetShellArgv(virConfPtr conf, return -1; if (rv == 0) { - if (VIR_ALLOC_N(*shargv, 2) < 0) + *shargv = g_new0(char *, 2); return -1; (*shargv)[0] = g_strdup("/bin/sh"); *shargvlen = 1; @@ -302,10 +302,8 @@ main(int argc, char **argv) if ((nfdlist = virDomainLxcOpenNamespace(dom, &fdlist, 0)) < 0) goto cleanup; - if (VIR_ALLOC(secmodel) < 0) - goto cleanup; - if (VIR_ALLOC(seclabel) < 0) - goto cleanup; + secmodel = g_new0(virSecurityModel, 1); + seclabel = g_new0(virSecurityLabel, 1); if (virNodeGetSecurityModel(conn, secmodel) < 0) goto cleanup; if (virDomainGetSecurityLabel(dom, seclabel) < 0) @@ -331,10 +329,7 @@ main(int argc, char **argv) if (tmp) { g_strfreev(shargv); shargvlen = 1; - if (VIR_ALLOC_N(shargv[0], shargvlen + 1) < 0) { - VIR_FREE(tmp); - goto cleanup; - } + shargv = g_new0(char *, shargvlen + 1); shargv[0] = tmp; shargv[1] = NULL; } diff --git a/tools/vsh-table.c b/tools/vsh-table.c index a727ac17b5..4471368687 100644 --- a/tools/vsh-table.c +++ b/tools/vsh-table.c @@ -95,8 +95,7 @@ vshTableRowNew(const char *arg, va_list ap) goto error; } - if (VIR_ALLOC(row) < 0) - goto error; + row = g_new0(vshTableRow, 1); while (arg) { char *tmp = NULL; @@ -134,8 +133,7 @@ vshTableNew(const char *arg, ...) vshTableRowPtr header = NULL; va_list ap; - if (VIR_ALLOC(table) < 0) - goto error; + table = g_new0(vshTable, 1); va_start(ap, arg); header = vshTableRowNew(arg, ap); @@ -215,8 +213,7 @@ vshTableSafeEncode(const char *s, size_t *width) memset(&st, 0, sizeof(st)); - if (VIR_ALLOC_N(buf, (sz * HEX_ENCODE_LENGTH) + 1) < 0) - return NULL; + buf = g_new0(char, (sz * HEX_ENCODE_LENGTH) + 1); ret = buf; *width = 0; @@ -369,17 +366,13 @@ vshTablePrint(vshTablePtr table, bool header) g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; char *ret = NULL; - if (VIR_ALLOC_N(maxwidths, table->rows[0]->ncells)) - goto cleanup; + maxwidths = g_new0(size_t, table->rows[0]->ncells); - if (VIR_ALLOC_N(widths, table->nrows)) - goto cleanup; + widths = g_new0(size_t *, table->nrows); /* retrieve widths of columns */ - for (i = 0; i < table->nrows; i++) { - if (VIR_ALLOC_N(widths[i], table->rows[0]->ncells)) - goto cleanup; - } + for (i = 0; i < table->nrows; i++) + widths[i] = g_new0(size_t, table->rows[0]->ncells); if (vshTableGetColumnsWidths(table, maxwidths, widths, header) < 0) goto cleanup; diff --git a/tools/vsh.c b/tools/vsh.c index 87c409e4aa..0eddd5349d 100644 --- a/tools/vsh.c +++ b/tools/vsh.c @@ -162,10 +162,7 @@ vshStringToArray(const char *str, } /* reserve the NULL element at the end */ - if (VIR_ALLOC_N(arr, nstr_tokens + 1) < 0) { - VIR_FREE(str_copied); - return -1; - } + arr = g_new0(char *, nstr_tokens + 1); /* tokenize the input string, while treating ,, as a literal comma */ nstr_tokens = 0; -- 2.26.2

On 9/23/20 5:11 PM, Ján Tomko wrote:
With the exception of vsh*alloc.
Signed-off-by: Ján Tomko <jtomko@redhat.com> ---
The 'error' label removal down there in virt-admin-completer.c was really nice. 2 'for' loops and several VIR_FREE() calls were needed solely because of that VIR_ALLOC_N(). Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
tools/virsh-domain-monitor.c | 11 +++-------- tools/virsh-domain.c | 26 ++++++++------------------ tools/virt-admin-completer.c | 12 +----------- tools/virt-login-shell-helper.c | 13 ++++--------- tools/vsh-table.c | 21 +++++++-------------- tools/vsh.c | 5 +---- 6 files changed, 24 insertions(+), 64 deletions(-)
diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c index 848efc8aa3..c8a7c0f1b7 100644 --- a/tools/virsh-domain-monitor.c +++ b/tools/virsh-domain-monitor.c @@ -1234,8 +1234,7 @@ cmdDomBlkError(vshControl *ctl, const vshCmd *cmd) ndisks = count;
if (ndisks) { - if (VIR_ALLOC_N(disks, ndisks) < 0) - goto cleanup; + disks = g_new0(virDomainDiskError, ndisks);
if ((count = virDomainGetDiskErrors(dom, disks, ndisks, 0)) == -1) goto cleanup; @@ -1378,10 +1377,7 @@ cmdDominfo(vshControl *ctl, const vshCmd *cmd) vshPrint(ctl, "%-15s %s\n", _("Security DOI:"), secmodel.doi);
/* Security labels are only valid for active domains */ - if (VIR_ALLOC(seclabel) < 0) { - virshDomainFree(dom); - return false; - } + seclabel = g_new0(virSecurityLabel, 1);
if (virDomainGetSecurityLabel(dom, seclabel) == -1) { virshDomainFree(dom); @@ -2280,8 +2276,7 @@ cmdDomstats(vshControl *ctl, const vshCmd *cmd) flags |= VIR_CONNECT_GET_ALL_DOMAINS_STATS_NOWAIT;
if (vshCommandOptBool(cmd, "domain")) { - if (VIR_ALLOC_N(domlist, 1) < 0) - goto cleanup; + domlist = g_new0(virDomainPtr, 1); ndoms = 1;
while ((opt = vshCommandOptArgv(ctl, cmd, opt))) { diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 32edfc0398..dfcba04682 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -1762,8 +1762,7 @@ virshBlockJobWaitInit(vshControl *ctl, virshBlockJobWaitDataPtr ret; virshControlPtr priv = ctl->privData;
- if (VIR_ALLOC(ret) < 0) - return NULL; + ret = g_new0(virshBlockJobWaitData, 1);
ret->ctl = ctl; ret->dom = dom; @@ -8177,8 +8176,7 @@ cmdCPUStats(vshControl *ctl, const vshCmd *cmd) goto cleanup; }
- if (VIR_ALLOC_N(params, nparams * MIN(show_count, 128)) < 0) - goto cleanup; + params = g_new0(virTypedParameter, nparams * MIN(show_count, 128));
while (show_count) { int ncpus = MIN(show_count, 128); @@ -8215,8 +8213,7 @@ cmdCPUStats(vshControl *ctl, const vshCmd *cmd) goto cleanup; }
- if (VIR_ALLOC_N(params, nparams) < 0) - goto cleanup; + params = g_new0(virTypedParameter, nparams);
/* passing start_cpu == -1 gives us domain's total status */ if ((stats_per_cpu = virDomainGetCPUStats(dom, params, nparams, @@ -10086,14 +10083,9 @@ cmdLxcEnterNamespace(vshControl *ctl, const vshCmd *cmd) goto cleanup;
if (setlabel) { - if (VIR_ALLOC(secmodel) < 0) { - vshError(ctl, "%s", _("Failed to allocate security model")); - goto cleanup; - } - if (VIR_ALLOC(seclabel) < 0) { - vshError(ctl, "%s", _("Failed to allocate security label")); - goto cleanup; - } + secmodel = g_new0(virSecurityModel, 1); + seclabel = g_new0(virSecurityLabel, 1); + if (virNodeGetSecurityModel(priv->conn, secmodel) < 0) goto cleanup; if (virDomainGetSecurityLabel(dom, seclabel) < 0) @@ -13737,8 +13729,7 @@ cmdEvent(vshControl *ctl, const vshCmd *cmd) }
if (all) { - if (VIR_ALLOC_N(data, VIR_DOMAIN_EVENT_ID_LAST) < 0) - goto cleanup; + data = g_new0(virshDomEventData, VIR_DOMAIN_EVENT_ID_LAST); for (i = 0; i < VIR_DOMAIN_EVENT_ID_LAST; i++) { data[i].ctl = ctl; data[i].loop = loop; @@ -13748,8 +13739,7 @@ cmdEvent(vshControl *ctl, const vshCmd *cmd) data[i].id = -1; } } else { - if (VIR_ALLOC_N(data, 1) < 0) - goto cleanup; + data = g_new0(virshDomEventData, 1); data[0].ctl = ctl; data[0].loop = vshCommandOptBool(cmd, "loop"); data[0].count = &count; diff --git a/tools/virt-admin-completer.c b/tools/virt-admin-completer.c index 7201bb71eb..61004beeb5 100644 --- a/tools/virt-admin-completer.c +++ b/tools/virt-admin-completer.c @@ -46,8 +46,7 @@ vshAdmServerCompleter(vshControl *ctl, if ((nsrvs = virAdmConnectListServers(priv->conn, &srvs, 0)) < 0) return NULL;
- if (VIR_ALLOC_N(ret, nsrvs + 1) < 0) - goto error; + ret = g_new0(char *, nsrvs + 1);
for (i = 0; i < nsrvs; i++) { const char *name = virAdmServerGetName(srvs[i]); @@ -59,13 +58,4 @@ vshAdmServerCompleter(vshControl *ctl, VIR_FREE(srvs);
return ret; - - error: - for (; i < nsrvs; i++) - virAdmServerFree(srvs[i]); - VIR_FREE(srvs); - for (i = 0; i < nsrvs; i++) - VIR_FREE(ret[i]); - VIR_FREE(ret); - return ret; } diff --git a/tools/virt-login-shell-helper.c b/tools/virt-login-shell-helper.c index cc6836382a..0e7987bf82 100644 --- a/tools/virt-login-shell-helper.c +++ b/tools/virt-login-shell-helper.c @@ -99,7 +99,7 @@ static int virLoginShellGetShellArgv(virConfPtr conf, return -1;
if (rv == 0) { - if (VIR_ALLOC_N(*shargv, 2) < 0) + *shargv = g_new0(char *, 2); return -1; (*shargv)[0] = g_strdup("/bin/sh"); *shargvlen = 1; @@ -302,10 +302,8 @@ main(int argc, char **argv)
if ((nfdlist = virDomainLxcOpenNamespace(dom, &fdlist, 0)) < 0) goto cleanup; - if (VIR_ALLOC(secmodel) < 0) - goto cleanup; - if (VIR_ALLOC(seclabel) < 0) - goto cleanup; + secmodel = g_new0(virSecurityModel, 1); + seclabel = g_new0(virSecurityLabel, 1); if (virNodeGetSecurityModel(conn, secmodel) < 0) goto cleanup; if (virDomainGetSecurityLabel(dom, seclabel) < 0) @@ -331,10 +329,7 @@ main(int argc, char **argv) if (tmp) { g_strfreev(shargv); shargvlen = 1; - if (VIR_ALLOC_N(shargv[0], shargvlen + 1) < 0) { - VIR_FREE(tmp); - goto cleanup; - } + shargv = g_new0(char *, shargvlen + 1); shargv[0] = tmp; shargv[1] = NULL; } diff --git a/tools/vsh-table.c b/tools/vsh-table.c index a727ac17b5..4471368687 100644 --- a/tools/vsh-table.c +++ b/tools/vsh-table.c @@ -95,8 +95,7 @@ vshTableRowNew(const char *arg, va_list ap) goto error; }
- if (VIR_ALLOC(row) < 0) - goto error; + row = g_new0(vshTableRow, 1);
while (arg) { char *tmp = NULL; @@ -134,8 +133,7 @@ vshTableNew(const char *arg, ...) vshTableRowPtr header = NULL; va_list ap;
- if (VIR_ALLOC(table) < 0) - goto error; + table = g_new0(vshTable, 1);
va_start(ap, arg); header = vshTableRowNew(arg, ap); @@ -215,8 +213,7 @@ vshTableSafeEncode(const char *s, size_t *width)
memset(&st, 0, sizeof(st));
- if (VIR_ALLOC_N(buf, (sz * HEX_ENCODE_LENGTH) + 1) < 0) - return NULL; + buf = g_new0(char, (sz * HEX_ENCODE_LENGTH) + 1);
ret = buf; *width = 0; @@ -369,17 +366,13 @@ vshTablePrint(vshTablePtr table, bool header) g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; char *ret = NULL;
- if (VIR_ALLOC_N(maxwidths, table->rows[0]->ncells)) - goto cleanup; + maxwidths = g_new0(size_t, table->rows[0]->ncells);
- if (VIR_ALLOC_N(widths, table->nrows)) - goto cleanup; + widths = g_new0(size_t *, table->nrows);
/* retrieve widths of columns */ - for (i = 0; i < table->nrows; i++) { - if (VIR_ALLOC_N(widths[i], table->rows[0]->ncells)) - goto cleanup; - } + for (i = 0; i < table->nrows; i++) + widths[i] = g_new0(size_t, table->rows[0]->ncells);
if (vshTableGetColumnsWidths(table, maxwidths, widths, header) < 0) goto cleanup; diff --git a/tools/vsh.c b/tools/vsh.c index 87c409e4aa..0eddd5349d 100644 --- a/tools/vsh.c +++ b/tools/vsh.c @@ -162,10 +162,7 @@ vshStringToArray(const char *str, }
/* reserve the NULL element at the end */ - if (VIR_ALLOC_N(arr, nstr_tokens + 1) < 0) { - VIR_FREE(str_copied); - return -1; - } + arr = g_new0(char *, nstr_tokens + 1);
/* tokenize the input string, while treating ,, as a literal comma */ nstr_tokens = 0;
participants (2)
-
Daniel Henrique Barboza
-
Ján Tomko