virsh: Account for return values in virNodeGetFreePages

The function returns how many array items were filled in, but virsh
never checked for anything other than errors.  Just to make sure this
does not report invalid data, even though the only possibility would be
reporting 0 free pages, check the returned data so that possible errors
are detected.

Signed-off-by: Martin Kletzander <mkletzan@redhat.com>
(cherry picked from commit c35ba64d18235bfe35617cb3d6d6cc778f6d166d)
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Martin Kletzander 2023-09-26 15:20:42 +02:00
parent 95a53a04a4
commit f7b7c17dfa

View File

@ -328,6 +328,8 @@ cmdFreepages(vshControl *ctl, const vshCmd *cmd)
bool cellno = vshCommandOptBool(cmd, "cellno"); bool cellno = vshCommandOptBool(cmd, "cellno");
bool pagesz = vshCommandOptBool(cmd, "pagesize"); bool pagesz = vshCommandOptBool(cmd, "pagesize");
virshControl *priv = ctl->privData; virshControl *priv = ctl->privData;
bool pagesize_missing = false;
int rv = -1;
VSH_EXCLUSIVE_OPTIONS_VAR(all, cellno); VSH_EXCLUSIVE_OPTIONS_VAR(all, cellno);
@ -407,16 +409,22 @@ cmdFreepages(vshControl *ctl, const vshCmd *cmd)
goto cleanup; goto cleanup;
} }
if (virNodeGetFreePages(priv->conn, npages, pagesize, rv = virNodeGetFreePages(priv->conn, npages, pagesize,
cell, 1, counts, 0) < 0) cell, 1, counts, 0);
if (rv < 0)
goto cleanup; goto cleanup;
if (rv < npages) {
pagesize_missing = true;
vshError(ctl, _("Did not get all free page data for node %1$d"), cell);
continue;
}
vshPrint(ctl, _("Node %1$d:\n"), cell); vshPrint(ctl, _("Node %1$d:\n"), cell);
for (j = 0; j < npages; j++) for (j = 0; j < npages; j++)
vshPrint(ctl, "%uKiB: %lld\n", pagesize[j], counts[j]); vshPrint(ctl, "%uKiB: %lld\n", pagesize[j], counts[j]);
vshPrint(ctl, "%c", '\n'); vshPrint(ctl, "%c", '\n');
} }
} else { } else {
if (!cellno) { if (!cellno) {
vshError(ctl, "%s", _("missing cellno argument")); vshError(ctl, "%s", _("missing cellno argument"));
@ -443,14 +451,22 @@ cmdFreepages(vshControl *ctl, const vshCmd *cmd)
counts = g_new0(unsigned long long, 1); counts = g_new0(unsigned long long, 1);
if (virNodeGetFreePages(priv->conn, 1, pagesize, rv = virNodeGetFreePages(priv->conn, 1, pagesize,
cell, 1, counts, 0) < 0) cell, 1, counts, 0);
if (rv < 0)
goto cleanup; goto cleanup;
if (rv == 0) {
vshError(ctl,
"Could not get count of free %uKiB pages, no data returned",
*pagesize);
goto cleanup;
}
vshPrint(ctl, "%uKiB: %lld\n", *pagesize, counts[0]); vshPrint(ctl, "%uKiB: %lld\n", *pagesize, counts[0]);
} }
ret = true; ret = !pagesize_missing;
cleanup: cleanup:
VIR_FREE(nodes); VIR_FREE(nodes);
return ret; return ret;