virsh: refactor cmdDomblkinfo

Use automatic memory cleanup to get rid of the cleanup section,
and of the memory leak that happens inside the loop, because
cap, alloc and phy are only freed once per function.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
This commit is contained in:
Ján Tomko 2021-08-11 16:04:45 +02:00
parent 878cbd0f6a
commit 7dd72490f6

View File

@ -452,22 +452,16 @@ static bool
cmdDomblkinfo(vshControl *ctl, const vshCmd *cmd) cmdDomblkinfo(vshControl *ctl, const vshCmd *cmd)
{ {
virDomainBlockInfo info; virDomainBlockInfo info;
virDomainPtr dom; g_autoptr(virshDomain) dom = NULL;
bool ret = false;
bool human = false; bool human = false;
bool all = false; bool all = false;
const char *device = NULL; const char *device = NULL;
xmlDocPtr xmldoc = NULL; g_autoptr(xmlDoc) xmldoc = NULL;
xmlXPathContextPtr ctxt = NULL; g_autoptr(xmlXPathContext) ctxt = NULL;
int ndisks; int ndisks;
size_t i; size_t i;
xmlNodePtr *disks = NULL; g_autofree xmlNodePtr *disks = NULL;
char *target = NULL; g_autoptr(vshTable) table = NULL;
char *protocol = NULL;
char *cap = NULL;
char *alloc = NULL;
char *phy = NULL;
vshTable *table = NULL;
VSH_EXCLUSIVE_OPTIONS("all", "device"); VSH_EXCLUSIVE_OPTIONS("all", "device");
@ -477,7 +471,7 @@ cmdDomblkinfo(vshControl *ctl, const vshCmd *cmd)
all = vshCommandOptBool(cmd, "all"); all = vshCommandOptBool(cmd, "all");
if (!all && vshCommandOptStringQuiet(ctl, cmd, "device", &device) <= 0) { if (!all && vshCommandOptStringQuiet(ctl, cmd, "device", &device) <= 0) {
vshError(ctl, "command 'domblkinfo' requires <device> option"); vshError(ctl, "command 'domblkinfo' requires <device> option");
goto cleanup; return false;
} }
human = vshCommandOptBool(cmd, "human"); human = vshCommandOptBool(cmd, "human");
@ -487,18 +481,24 @@ cmdDomblkinfo(vshControl *ctl, const vshCmd *cmd)
int rc; int rc;
if (virshDomainGetXML(ctl, cmd, 0, &xmldoc, &ctxt) < 0) if (virshDomainGetXML(ctl, cmd, 0, &xmldoc, &ctxt) < 0)
goto cleanup; return false;
ndisks = virXPathNodeSet("./devices/disk", ctxt, &disks); ndisks = virXPathNodeSet("./devices/disk", ctxt, &disks);
if (ndisks < 0) if (ndisks < 0)
goto cleanup; return false;
/* title */ /* title */
table = vshTableNew(_("Target"), _("Capacity"), _("Allocation"), _("Physical"), NULL); table = vshTableNew(_("Target"), _("Capacity"), _("Allocation"), _("Physical"), NULL);
if (!table) if (!table)
goto cleanup; return false;
for (i = 0; i < ndisks; i++) { for (i = 0; i < ndisks; i++) {
g_autofree char *target = NULL;
g_autofree char *protocol = NULL;
g_autofree char *cap = NULL;
g_autofree char *alloc = NULL;
g_autofree char *phy = NULL;
ctxt->node = disks[i]; ctxt->node = disks[i];
protocol = virXPathString("string(./source/@protocol)", ctxt); protocol = virXPathString("string(./source/@protocol)", ctxt);
target = virXPathString("string(./target/@dev)", ctxt); target = virXPathString("string(./target/@dev)", ctxt);
@ -517,7 +517,7 @@ cmdDomblkinfo(vshControl *ctl, const vshCmd *cmd)
memset(&info, 0, sizeof(info)); memset(&info, 0, sizeof(info));
vshResetLibvirtError(); vshResetLibvirtError();
} else { } else {
goto cleanup; return false;
} }
} }
} else { } else {
@ -527,41 +527,29 @@ cmdDomblkinfo(vshControl *ctl, const vshCmd *cmd)
} }
if (!cmdDomblkinfoGet(&info, &cap, &alloc, &phy, human)) if (!cmdDomblkinfoGet(&info, &cap, &alloc, &phy, human))
goto cleanup; return false;
if (vshTableRowAppend(table, target, cap, alloc, phy, NULL) < 0) if (vshTableRowAppend(table, target, cap, alloc, phy, NULL) < 0)
goto cleanup; return false;
VIR_FREE(target);
VIR_FREE(protocol);
} }
vshTablePrintToStdout(table, ctl); vshTablePrintToStdout(table, ctl);
} else { } else {
g_autofree char *cap = NULL;
g_autofree char *alloc = NULL;
g_autofree char *phy = NULL;
if (virDomainGetBlockInfo(dom, device, &info, 0) < 0) if (virDomainGetBlockInfo(dom, device, &info, 0) < 0)
goto cleanup; return false;
if (!cmdDomblkinfoGet(&info, &cap, &alloc, &phy, human)) if (!cmdDomblkinfoGet(&info, &cap, &alloc, &phy, human))
goto cleanup; return false;
vshPrint(ctl, "%-15s %s\n", _("Capacity:"), cap); vshPrint(ctl, "%-15s %s\n", _("Capacity:"), cap);
vshPrint(ctl, "%-15s %s\n", _("Allocation:"), alloc); vshPrint(ctl, "%-15s %s\n", _("Allocation:"), alloc);
vshPrint(ctl, "%-15s %s\n", _("Physical:"), phy); vshPrint(ctl, "%-15s %s\n", _("Physical:"), phy);
} }
ret = true; return true;
cleanup:
vshTableFree(table);
VIR_FREE(cap);
VIR_FREE(alloc);
VIR_FREE(phy);
virshDomainFree(dom);
VIR_FREE(target);
VIR_FREE(protocol);
VIR_FREE(disks);
xmlXPathFreeContext(ctxt);
xmlFreeDoc(xmldoc);
return ret;
} }
/* /*