From 01a2339e1f24d404634eff059a3cbe5a2d669200 Mon Sep 17 00:00:00 2001 From: Michal Privoznik Date: Wed, 19 May 2021 11:58:21 +0200 Subject: [PATCH] virsh-domain: Fix @ret handling in cmdSetmem and cmdSetmaxmem These functions initialize @ret to true and only after something fails either they call cleanup code (which consists only from virshDomainFree()) and return false, or they set ret = false and carry on (when the failure occurred close to cleanup code). Switch them to the usual pattern in which ret is initialized to failure, goto cleanup is used and ret is set to true only after everything succeeded. Signed-off-by: Michal Privoznik Reviewed-by: Peter Krempa --- tools/virsh-domain.c | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 9316aae3fd..5a5215ab4c 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -8996,7 +8996,7 @@ cmdSetmem(vshControl *ctl, const vshCmd *cmd) unsigned long long bytes = 0; unsigned long long max; unsigned long kibibytes = 0; - bool ret = true; + bool ret = false; bool config = vshCommandOptBool(cmd, "config"); bool live = vshCommandOptBool(cmd, "live"); bool current = vshCommandOptBool(cmd, "current"); @@ -9019,20 +9019,20 @@ cmdSetmem(vshControl *ctl, const vshCmd *cmd) max = 1024ull * ULONG_MAX; else max = ULONG_MAX; - if (vshCommandOptScaledInt(ctl, cmd, "size", &bytes, 1024, max) < 0) { - virshDomainFree(dom); - return false; - } + if (vshCommandOptScaledInt(ctl, cmd, "size", &bytes, 1024, max) < 0) + goto cleanup; kibibytes = VIR_DIV_UP(bytes, 1024); if (!current && !live && !config) { if (virDomainSetMemory(dom, kibibytes) != 0) - ret = false; + goto cleanup; } else { if (virDomainSetMemoryFlags(dom, kibibytes, flags) < 0) - ret = false; + goto cleanup; } + ret = true; + cleanup: virshDomainFree(dom); return ret; } @@ -9074,7 +9074,7 @@ cmdSetmaxmem(vshControl *ctl, const vshCmd *cmd) unsigned long long bytes = 0; unsigned long long max; unsigned long kibibytes = 0; - bool ret = true; + bool ret = false; bool config = vshCommandOptBool(cmd, "config"); bool live = vshCommandOptBool(cmd, "live"); bool current = vshCommandOptBool(cmd, "current"); @@ -9097,24 +9097,24 @@ cmdSetmaxmem(vshControl *ctl, const vshCmd *cmd) max = 1024ull * ULONG_MAX; else max = ULONG_MAX; - if (vshCommandOptScaledInt(ctl, cmd, "size", &bytes, 1024, max) < 0) { - virshDomainFree(dom); - return false; - } + if (vshCommandOptScaledInt(ctl, cmd, "size", &bytes, 1024, max) < 0) + goto cleanup; kibibytes = VIR_DIV_UP(bytes, 1024); if (flags == 0) { if (virDomainSetMaxMemory(dom, kibibytes) != 0) { vshError(ctl, "%s", _("Unable to change MaxMemorySize")); - ret = false; + goto cleanup; } } else { if (virDomainSetMemoryFlags(dom, kibibytes, flags | VIR_DOMAIN_MEM_MAXIMUM) < 0) { vshError(ctl, "%s", _("Unable to change MaxMemorySize")); - ret = false; + goto cleanup; } } + ret = true; + cleanup: virshDomainFree(dom); return ret; }