mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-03 03:25:20 +00:00
tools: virsh: reduce variable scope to use automatic cleanup
Some variables are used in a loop and only freed in the cleanup section because we need to be able to jump out of the loop. Reduce their scope and free them automatically. Signed-off-by: Ján Tomko <jtomko@redhat.com> Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
This commit is contained in:
parent
4b72960b4e
commit
851e66e05d
@ -586,10 +586,6 @@ cmdDomblklist(vshControl *ctl, const vshCmd *cmd)
|
||||
xmlNodePtr *disks = NULL;
|
||||
size_t i;
|
||||
bool details = false;
|
||||
char *type = NULL;
|
||||
char *device = NULL;
|
||||
char *target = NULL;
|
||||
char *source = NULL;
|
||||
g_autoptr(vshTable) table = NULL;
|
||||
|
||||
if (vshCommandOptBool(cmd, "inactive"))
|
||||
@ -613,6 +609,11 @@ cmdDomblklist(vshControl *ctl, const vshCmd *cmd)
|
||||
goto cleanup;
|
||||
|
||||
for (i = 0; i < ndisks; i++) {
|
||||
g_autofree char *type = NULL;
|
||||
g_autofree char *device = NULL;
|
||||
g_autofree char *target = NULL;
|
||||
g_autofree char *source = NULL;
|
||||
|
||||
ctxt->node = disks[i];
|
||||
|
||||
type = virXPathString("string(./@type)", ctxt);
|
||||
@ -662,11 +663,6 @@ cmdDomblklist(vshControl *ctl, const vshCmd *cmd)
|
||||
NULLSTR_MINUS(source), NULL) < 0)
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
VIR_FREE(source);
|
||||
VIR_FREE(target);
|
||||
VIR_FREE(device);
|
||||
VIR_FREE(type);
|
||||
}
|
||||
|
||||
vshTablePrintToStdout(table, ctl);
|
||||
@ -674,10 +670,6 @@ cmdDomblklist(vshControl *ctl, const vshCmd *cmd)
|
||||
ret = true;
|
||||
|
||||
cleanup:
|
||||
VIR_FREE(source);
|
||||
VIR_FREE(target);
|
||||
VIR_FREE(device);
|
||||
VIR_FREE(type);
|
||||
VIR_FREE(disks);
|
||||
return ret;
|
||||
}
|
||||
@ -987,7 +979,6 @@ cmdDomblkstat(vshControl *ctl, const vshCmd *cmd)
|
||||
virDomainBlockStatsStruct stats;
|
||||
virTypedParameterPtr params = NULL;
|
||||
virTypedParameterPtr par = NULL;
|
||||
char *value = NULL;
|
||||
const char *field = NULL;
|
||||
int rc, nparams = 0;
|
||||
size_t i;
|
||||
@ -1053,6 +1044,8 @@ cmdDomblkstat(vshControl *ctl, const vshCmd *cmd)
|
||||
|
||||
/* at first print all known values in desired order */
|
||||
for (i = 0; domblkstat_output[i].field != NULL; i++) {
|
||||
g_autofree char *value = NULL;
|
||||
|
||||
if (!(par = virTypedParamsGet(params, nparams,
|
||||
domblkstat_output[i].field)))
|
||||
continue;
|
||||
@ -1075,18 +1068,17 @@ cmdDomblkstat(vshControl *ctl, const vshCmd *cmd)
|
||||
|
||||
vshPrint(ctl, "%s %-*s %s\n", device,
|
||||
human ? 31 : 0, field, value);
|
||||
|
||||
VIR_FREE(value);
|
||||
}
|
||||
|
||||
/* go through the fields again, for remaining fields */
|
||||
for (i = 0; i < nparams; i++) {
|
||||
g_autofree char *value = NULL;
|
||||
|
||||
if (!*params[i].field)
|
||||
continue;
|
||||
|
||||
value = vshGetTypedParamValue(ctl, params+i);
|
||||
vshPrint(ctl, "%s %s %s\n", device, params[i].field, value);
|
||||
VIR_FREE(value);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1927,7 +1919,6 @@ cmdList(vshControl *ctl, const vshCmd *cmd)
|
||||
bool optName = vshCommandOptBool(cmd, "name");
|
||||
bool optID = vshCommandOptBool(cmd, "id");
|
||||
size_t i;
|
||||
char *title;
|
||||
char uuid[VIR_UUID_STRING_BUFLEN];
|
||||
int state;
|
||||
bool ret = false;
|
||||
@ -2010,6 +2001,8 @@ cmdList(vshControl *ctl, const vshCmd *cmd)
|
||||
state = -2;
|
||||
|
||||
if (optTitle) {
|
||||
g_autofree char *title = NULL;
|
||||
|
||||
if (!(title = virshGetDomainDescription(ctl, dom, true, 0)))
|
||||
goto cleanup;
|
||||
if (vshTableRowAppend(table, id_buf,
|
||||
@ -2018,7 +2011,6 @@ cmdList(vshControl *ctl, const vshCmd *cmd)
|
||||
: virshDomainStateToString(state),
|
||||
title, NULL) < 0)
|
||||
goto cleanup;
|
||||
VIR_FREE(title);
|
||||
} else {
|
||||
if (vshTableRowAppend(table, id_buf,
|
||||
virDomainGetName(dom),
|
||||
@ -2175,7 +2167,6 @@ virshDomainStatsPrintRecord(vshControl *ctl G_GNUC_UNUSED,
|
||||
virDomainStatsRecordPtr record,
|
||||
bool raw G_GNUC_UNUSED)
|
||||
{
|
||||
char *param;
|
||||
size_t i;
|
||||
|
||||
vshPrint(ctl, "Domain: '%s'\n", virDomainGetName(record->dom));
|
||||
@ -2183,12 +2174,12 @@ virshDomainStatsPrintRecord(vshControl *ctl G_GNUC_UNUSED,
|
||||
/* XXX: Implement pretty-printing */
|
||||
|
||||
for (i = 0; i < record->nparams; i++) {
|
||||
g_autofree char *param = NULL;
|
||||
|
||||
if (!(param = vshGetTypedParamValue(ctl, record->params + i)))
|
||||
return false;
|
||||
|
||||
vshPrint(ctl, " %s=%s\n", record->params[i].field, param);
|
||||
|
||||
VIR_FREE(param);
|
||||
}
|
||||
|
||||
return true;
|
||||
@ -2388,7 +2379,6 @@ cmdDomIfAddr(vshControl *ctl, const vshCmd *cmd)
|
||||
|
||||
for (i = 0; i < ifaces_count; i++) {
|
||||
virDomainInterfacePtr iface = ifaces[i];
|
||||
char *ip_addr_str = NULL;
|
||||
const char *type = NULL;
|
||||
|
||||
if (ifacestr && STRNEQ(ifacestr, iface->name))
|
||||
@ -2404,6 +2394,7 @@ cmdDomIfAddr(vshControl *ctl, const vshCmd *cmd)
|
||||
|
||||
for (j = 0; j < iface->naddrs; j++) {
|
||||
g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
|
||||
g_autofree char *ip_addr_str = NULL;
|
||||
|
||||
switch (iface->addrs[j].type) {
|
||||
case VIR_IP_ADDR_TYPE_IPV4:
|
||||
@ -2431,8 +2422,6 @@ cmdDomIfAddr(vshControl *ctl, const vshCmd *cmd)
|
||||
else
|
||||
vshPrint(ctl, " %-10s %-17s %s\n",
|
||||
"-", "-", ip_addr_str);
|
||||
|
||||
VIR_FREE(ip_addr_str);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3055,7 +3055,6 @@ cmdDomIfSetLink(vshControl *ctl, const vshCmd *cmd)
|
||||
g_autoptr(virshDomain) dom = NULL;
|
||||
const char *iface;
|
||||
const char *state;
|
||||
char *value;
|
||||
virMacAddr macaddr;
|
||||
const char *element;
|
||||
const char *attr;
|
||||
@ -3119,13 +3118,10 @@ cmdDomIfSetLink(vshControl *ctl, const vshCmd *cmd)
|
||||
while (cur) {
|
||||
if (cur->type == XML_ELEMENT_NODE &&
|
||||
virXMLNodeNameEqual(cur, element)) {
|
||||
value = virXMLPropString(cur, attr);
|
||||
g_autofree char *value = virXMLPropString(cur, attr);
|
||||
|
||||
if (STRCASEEQ(value, iface)) {
|
||||
VIR_FREE(value);
|
||||
if (STRCASEEQ(value, iface))
|
||||
goto hit;
|
||||
}
|
||||
VIR_FREE(value);
|
||||
}
|
||||
cur = cur->next;
|
||||
}
|
||||
@ -3639,9 +3635,6 @@ cmdUndefine(vshControl *ctl, const vshCmd *cmd)
|
||||
g_autoptr(xmlXPathContext) ctxt = NULL;
|
||||
xmlNodePtr *vol_nodes = NULL; /* XML nodes of volumes of the guest */
|
||||
int nvol_nodes;
|
||||
char *source = NULL;
|
||||
char *target = NULL;
|
||||
char *pool = NULL;
|
||||
size_t i;
|
||||
size_t j;
|
||||
virshControl *priv = ctl->privData;
|
||||
@ -3759,14 +3752,13 @@ cmdUndefine(vshControl *ctl, const vshCmd *cmd)
|
||||
goto error;
|
||||
|
||||
for (i = 0; i < nvol_nodes; i++) {
|
||||
g_autofree char *source = NULL;
|
||||
g_autofree char *target = NULL;
|
||||
g_autofree char *pool = NULL;
|
||||
virshUndefineVolume vol;
|
||||
|
||||
ctxt->node = vol_nodes[i];
|
||||
|
||||
VIR_FREE(source);
|
||||
VIR_FREE(target);
|
||||
VIR_FREE(pool);
|
||||
|
||||
/* get volume source and target paths */
|
||||
if (!(target = virXPathString("string(./target/@dev)", ctxt)))
|
||||
goto error;
|
||||
@ -3936,9 +3928,6 @@ cmdUndefine(vshControl *ctl, const vshCmd *cmd)
|
||||
}
|
||||
|
||||
cleanup:
|
||||
VIR_FREE(source);
|
||||
VIR_FREE(target);
|
||||
VIR_FREE(pool);
|
||||
for (i = 0; i < nvols; i++) {
|
||||
VIR_FREE(vols[i].source);
|
||||
VIR_FREE(vols[i].target);
|
||||
@ -5096,7 +5085,6 @@ cmdSchedInfoUpdate(vshControl *ctl, const vshCmd *cmd,
|
||||
virTypedParameterPtr src_params, int nsrc_params,
|
||||
virTypedParameterPtr *update_params)
|
||||
{
|
||||
char *set_field = NULL;
|
||||
char *set_val = NULL;
|
||||
const char *val = NULL;
|
||||
const vshCmdOpt *opt = NULL;
|
||||
@ -5107,7 +5095,8 @@ cmdSchedInfoUpdate(vshControl *ctl, const vshCmd *cmd,
|
||||
int rv;
|
||||
|
||||
while ((opt = vshCommandOptArgv(ctl, cmd, opt))) {
|
||||
set_field = g_strdup(opt->data);
|
||||
g_autofree char *set_field = g_strdup(opt->data);
|
||||
|
||||
if (!(set_val = strchr(set_field, '='))) {
|
||||
vshError(ctl, "%s", _("Invalid syntax for --set, "
|
||||
"expecting name=value"));
|
||||
@ -5121,8 +5110,6 @@ cmdSchedInfoUpdate(vshControl *ctl, const vshCmd *cmd,
|
||||
¶ms, &nparams, &maxparams,
|
||||
set_field, set_val) < 0)
|
||||
goto cleanup;
|
||||
|
||||
VIR_FREE(set_field);
|
||||
}
|
||||
|
||||
rv = vshCommandOptStringReq(ctl, cmd, "cap", &val);
|
||||
@ -5145,7 +5132,6 @@ cmdSchedInfoUpdate(vshControl *ctl, const vshCmd *cmd,
|
||||
*update_params = g_steal_pointer(¶ms);
|
||||
|
||||
cleanup:
|
||||
VIR_FREE(set_field);
|
||||
virTypedParamsFree(params, nparams);
|
||||
return ret;
|
||||
}
|
||||
@ -6759,7 +6745,6 @@ virshDomainGetVcpuBitmap(vshControl *ctl,
|
||||
unsigned int curvcpus = 0;
|
||||
unsigned int maxvcpus = 0;
|
||||
unsigned int vcpuid;
|
||||
char *online = NULL;
|
||||
|
||||
if (inactive)
|
||||
flags |= VIR_DOMAIN_XML_INACTIVE;
|
||||
@ -6788,6 +6773,8 @@ virshDomainGetVcpuBitmap(vshControl *ctl,
|
||||
}
|
||||
|
||||
for (i = 0; i < nnodes; i++) {
|
||||
g_autofree char *online = NULL;
|
||||
|
||||
ctxt->node = nodes[i];
|
||||
|
||||
if (virXPathUInt("string(@id)", ctxt, &vcpuid) < 0 ||
|
||||
@ -6796,8 +6783,6 @@ virshDomainGetVcpuBitmap(vshControl *ctl,
|
||||
|
||||
if (STREQ(online, "yes"))
|
||||
ignore_value(virBitmapSetBit(ret, vcpuid));
|
||||
|
||||
VIR_FREE(online);
|
||||
}
|
||||
|
||||
if (virBitmapCountBits(ret) != curvcpus) {
|
||||
@ -6807,7 +6792,6 @@ virshDomainGetVcpuBitmap(vshControl *ctl,
|
||||
}
|
||||
|
||||
cleanup:
|
||||
VIR_FREE(online);
|
||||
VIR_FREE(nodes);
|
||||
return ret;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user