tools: use g_new0 instead of VIR_ALLOC*

With the exception of vsh*alloc.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
This commit is contained in:
Ján Tomko 2020-09-23 22:06:18 +02:00
parent f67be086a2
commit 157b17f706
6 changed files with 24 additions and 64 deletions

View File

@ -1234,8 +1234,7 @@ cmdDomBlkError(vshControl *ctl, const vshCmd *cmd)
ndisks = count; ndisks = count;
if (ndisks) { if (ndisks) {
if (VIR_ALLOC_N(disks, ndisks) < 0) disks = g_new0(virDomainDiskError, ndisks);
goto cleanup;
if ((count = virDomainGetDiskErrors(dom, disks, ndisks, 0)) == -1) if ((count = virDomainGetDiskErrors(dom, disks, ndisks, 0)) == -1)
goto cleanup; goto cleanup;
@ -1378,10 +1377,7 @@ cmdDominfo(vshControl *ctl, const vshCmd *cmd)
vshPrint(ctl, "%-15s %s\n", _("Security DOI:"), secmodel.doi); vshPrint(ctl, "%-15s %s\n", _("Security DOI:"), secmodel.doi);
/* Security labels are only valid for active domains */ /* Security labels are only valid for active domains */
if (VIR_ALLOC(seclabel) < 0) { seclabel = g_new0(virSecurityLabel, 1);
virshDomainFree(dom);
return false;
}
if (virDomainGetSecurityLabel(dom, seclabel) == -1) { if (virDomainGetSecurityLabel(dom, seclabel) == -1) {
virshDomainFree(dom); virshDomainFree(dom);
@ -2280,8 +2276,7 @@ cmdDomstats(vshControl *ctl, const vshCmd *cmd)
flags |= VIR_CONNECT_GET_ALL_DOMAINS_STATS_NOWAIT; flags |= VIR_CONNECT_GET_ALL_DOMAINS_STATS_NOWAIT;
if (vshCommandOptBool(cmd, "domain")) { if (vshCommandOptBool(cmd, "domain")) {
if (VIR_ALLOC_N(domlist, 1) < 0) domlist = g_new0(virDomainPtr, 1);
goto cleanup;
ndoms = 1; ndoms = 1;
while ((opt = vshCommandOptArgv(ctl, cmd, opt))) { while ((opt = vshCommandOptArgv(ctl, cmd, opt))) {

View File

@ -1762,8 +1762,7 @@ virshBlockJobWaitInit(vshControl *ctl,
virshBlockJobWaitDataPtr ret; virshBlockJobWaitDataPtr ret;
virshControlPtr priv = ctl->privData; virshControlPtr priv = ctl->privData;
if (VIR_ALLOC(ret) < 0) ret = g_new0(virshBlockJobWaitData, 1);
return NULL;
ret->ctl = ctl; ret->ctl = ctl;
ret->dom = dom; ret->dom = dom;
@ -8177,8 +8176,7 @@ cmdCPUStats(vshControl *ctl, const vshCmd *cmd)
goto cleanup; goto cleanup;
} }
if (VIR_ALLOC_N(params, nparams * MIN(show_count, 128)) < 0) params = g_new0(virTypedParameter, nparams * MIN(show_count, 128));
goto cleanup;
while (show_count) { while (show_count) {
int ncpus = MIN(show_count, 128); int ncpus = MIN(show_count, 128);
@ -8215,8 +8213,7 @@ cmdCPUStats(vshControl *ctl, const vshCmd *cmd)
goto cleanup; goto cleanup;
} }
if (VIR_ALLOC_N(params, nparams) < 0) params = g_new0(virTypedParameter, nparams);
goto cleanup;
/* passing start_cpu == -1 gives us domain's total status */ /* passing start_cpu == -1 gives us domain's total status */
if ((stats_per_cpu = virDomainGetCPUStats(dom, params, nparams, if ((stats_per_cpu = virDomainGetCPUStats(dom, params, nparams,
@ -10086,14 +10083,9 @@ cmdLxcEnterNamespace(vshControl *ctl, const vshCmd *cmd)
goto cleanup; goto cleanup;
if (setlabel) { if (setlabel) {
if (VIR_ALLOC(secmodel) < 0) { secmodel = g_new0(virSecurityModel, 1);
vshError(ctl, "%s", _("Failed to allocate security model")); seclabel = g_new0(virSecurityLabel, 1);
goto cleanup;
}
if (VIR_ALLOC(seclabel) < 0) {
vshError(ctl, "%s", _("Failed to allocate security label"));
goto cleanup;
}
if (virNodeGetSecurityModel(priv->conn, secmodel) < 0) if (virNodeGetSecurityModel(priv->conn, secmodel) < 0)
goto cleanup; goto cleanup;
if (virDomainGetSecurityLabel(dom, seclabel) < 0) if (virDomainGetSecurityLabel(dom, seclabel) < 0)
@ -13737,8 +13729,7 @@ cmdEvent(vshControl *ctl, const vshCmd *cmd)
} }
if (all) { if (all) {
if (VIR_ALLOC_N(data, VIR_DOMAIN_EVENT_ID_LAST) < 0) data = g_new0(virshDomEventData, VIR_DOMAIN_EVENT_ID_LAST);
goto cleanup;
for (i = 0; i < VIR_DOMAIN_EVENT_ID_LAST; i++) { for (i = 0; i < VIR_DOMAIN_EVENT_ID_LAST; i++) {
data[i].ctl = ctl; data[i].ctl = ctl;
data[i].loop = loop; data[i].loop = loop;
@ -13748,8 +13739,7 @@ cmdEvent(vshControl *ctl, const vshCmd *cmd)
data[i].id = -1; data[i].id = -1;
} }
} else { } else {
if (VIR_ALLOC_N(data, 1) < 0) data = g_new0(virshDomEventData, 1);
goto cleanup;
data[0].ctl = ctl; data[0].ctl = ctl;
data[0].loop = vshCommandOptBool(cmd, "loop"); data[0].loop = vshCommandOptBool(cmd, "loop");
data[0].count = &count; data[0].count = &count;

View File

@ -46,8 +46,7 @@ vshAdmServerCompleter(vshControl *ctl,
if ((nsrvs = virAdmConnectListServers(priv->conn, &srvs, 0)) < 0) if ((nsrvs = virAdmConnectListServers(priv->conn, &srvs, 0)) < 0)
return NULL; return NULL;
if (VIR_ALLOC_N(ret, nsrvs + 1) < 0) ret = g_new0(char *, nsrvs + 1);
goto error;
for (i = 0; i < nsrvs; i++) { for (i = 0; i < nsrvs; i++) {
const char *name = virAdmServerGetName(srvs[i]); const char *name = virAdmServerGetName(srvs[i]);
@ -59,13 +58,4 @@ vshAdmServerCompleter(vshControl *ctl,
VIR_FREE(srvs); VIR_FREE(srvs);
return ret; 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;
} }

View File

@ -99,7 +99,7 @@ static int virLoginShellGetShellArgv(virConfPtr conf,
return -1; return -1;
if (rv == 0) { if (rv == 0) {
if (VIR_ALLOC_N(*shargv, 2) < 0) *shargv = g_new0(char *, 2);
return -1; return -1;
(*shargv)[0] = g_strdup("/bin/sh"); (*shargv)[0] = g_strdup("/bin/sh");
*shargvlen = 1; *shargvlen = 1;
@ -302,10 +302,8 @@ main(int argc, char **argv)
if ((nfdlist = virDomainLxcOpenNamespace(dom, &fdlist, 0)) < 0) if ((nfdlist = virDomainLxcOpenNamespace(dom, &fdlist, 0)) < 0)
goto cleanup; goto cleanup;
if (VIR_ALLOC(secmodel) < 0) secmodel = g_new0(virSecurityModel, 1);
goto cleanup; seclabel = g_new0(virSecurityLabel, 1);
if (VIR_ALLOC(seclabel) < 0)
goto cleanup;
if (virNodeGetSecurityModel(conn, secmodel) < 0) if (virNodeGetSecurityModel(conn, secmodel) < 0)
goto cleanup; goto cleanup;
if (virDomainGetSecurityLabel(dom, seclabel) < 0) if (virDomainGetSecurityLabel(dom, seclabel) < 0)
@ -331,10 +329,7 @@ main(int argc, char **argv)
if (tmp) { if (tmp) {
g_strfreev(shargv); g_strfreev(shargv);
shargvlen = 1; shargvlen = 1;
if (VIR_ALLOC_N(shargv[0], shargvlen + 1) < 0) { shargv = g_new0(char *, shargvlen + 1);
VIR_FREE(tmp);
goto cleanup;
}
shargv[0] = tmp; shargv[0] = tmp;
shargv[1] = NULL; shargv[1] = NULL;
} }

View File

@ -95,8 +95,7 @@ vshTableRowNew(const char *arg, va_list ap)
goto error; goto error;
} }
if (VIR_ALLOC(row) < 0) row = g_new0(vshTableRow, 1);
goto error;
while (arg) { while (arg) {
char *tmp = NULL; char *tmp = NULL;
@ -134,8 +133,7 @@ vshTableNew(const char *arg, ...)
vshTableRowPtr header = NULL; vshTableRowPtr header = NULL;
va_list ap; va_list ap;
if (VIR_ALLOC(table) < 0) table = g_new0(vshTable, 1);
goto error;
va_start(ap, arg); va_start(ap, arg);
header = vshTableRowNew(arg, ap); header = vshTableRowNew(arg, ap);
@ -215,8 +213,7 @@ vshTableSafeEncode(const char *s, size_t *width)
memset(&st, 0, sizeof(st)); memset(&st, 0, sizeof(st));
if (VIR_ALLOC_N(buf, (sz * HEX_ENCODE_LENGTH) + 1) < 0) buf = g_new0(char, (sz * HEX_ENCODE_LENGTH) + 1);
return NULL;
ret = buf; ret = buf;
*width = 0; *width = 0;
@ -369,17 +366,13 @@ vshTablePrint(vshTablePtr table, bool header)
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
char *ret = NULL; char *ret = NULL;
if (VIR_ALLOC_N(maxwidths, table->rows[0]->ncells)) maxwidths = g_new0(size_t, table->rows[0]->ncells);
goto cleanup;
if (VIR_ALLOC_N(widths, table->nrows)) widths = g_new0(size_t *, table->nrows);
goto cleanup;
/* retrieve widths of columns */ /* retrieve widths of columns */
for (i = 0; i < table->nrows; i++) { for (i = 0; i < table->nrows; i++)
if (VIR_ALLOC_N(widths[i], table->rows[0]->ncells)) widths[i] = g_new0(size_t, table->rows[0]->ncells);
goto cleanup;
}
if (vshTableGetColumnsWidths(table, maxwidths, widths, header) < 0) if (vshTableGetColumnsWidths(table, maxwidths, widths, header) < 0)
goto cleanup; goto cleanup;

View File

@ -162,10 +162,7 @@ vshStringToArray(const char *str,
} }
/* reserve the NULL element at the end */ /* reserve the NULL element at the end */
if (VIR_ALLOC_N(arr, nstr_tokens + 1) < 0) { arr = g_new0(char *, nstr_tokens + 1);
VIR_FREE(str_copied);
return -1;
}
/* tokenize the input string, while treating ,, as a literal comma */ /* tokenize the input string, while treating ,, as a literal comma */
nstr_tokens = 0; nstr_tokens = 0;