]> xenbits.xensource.com Git - libvirt.git/commitdiff
virsh-domain: Fix @ret handling in cmdSetmem and cmdSetmaxmem
authorMichal Privoznik <mprivozn@redhat.com>
Wed, 19 May 2021 09:58:21 +0000 (11:58 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Wed, 19 May 2021 13:31:36 +0000 (15:31 +0200)
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 <mprivozn@redhat.com>
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
tools/virsh-domain.c

index 9316aae3fd1cab1d2d1f9949be1e53d552cb6824..5a5215ab4ce6eeaa73de6cd8bc26164432837ca2 100644 (file)
@@ -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;
 }