Blob Blame History Raw
From bb29aaf4fd23afd3e51227f415816707d28cfadb Mon Sep 17 00:00:00 2001
Message-Id: <bb29aaf4fd23afd3e51227f415816707d28cfadb@dist-git>
From: Michal Privoznik <mprivozn@redhat.com>
Date: Fri, 5 Aug 2016 14:35:24 +0200
Subject: [PATCH] qemuBuildMachineCommandLine: Follow our pattern

https://bugzilla.redhat.com/show_bug.cgi?id=1304483

We use 'goto cleanup' for a reason. If a function can exit at
many places but doesn't follow the pattern, it has to copy the
free code in multiple places.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
(cherry picked from commit 90b42f0fad5ee3395cf26c0cfc0a8bd1ad1753bb)
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---
 src/qemu/qemu_command.c | 39 ++++++++++++++++++---------------------
 1 file changed, 18 insertions(+), 21 deletions(-)

diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index b6a7798..1a5c6dc 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -6956,7 +6956,9 @@ qemuBuildMachineCommandLine(virCommandPtr cmd,
                             const virDomainDef *def,
                             virQEMUCapsPtr qemuCaps)
 {
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
     bool obsoleteAccel = false;
+    int ret = -1;
 
     /* This should *never* be NULL, since we always provide
      * a machine in the capabilities data for QEMU. So this
@@ -6981,7 +6983,7 @@ qemuBuildMachineCommandLine(virCommandPtr cmd,
             virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                            _("disable shared memory is not available "
                              "with this QEMU binary"));
-             return -1;
+            return -1;
         }
 
         obsoleteAccel = true;
@@ -6993,7 +6995,6 @@ qemuBuildMachineCommandLine(virCommandPtr cmd,
             return -1;
         }
     } else {
-        virBuffer buf = VIR_BUFFER_INITIALIZER;
         virTristateSwitch vmport = def->features[VIR_DOMAIN_FEATURE_VMPORT];
 
         virCommandAddArg(cmd, "-machine");
@@ -7017,8 +7018,7 @@ qemuBuildMachineCommandLine(virCommandPtr cmd,
                 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                                _("vmport is not available "
                                  "with this QEMU binary"));
-                virBufferFreeAndReset(&buf);
-                return -1;
+                goto cleanup;
             }
 
             virBufferAsprintf(&buf, ",vmport=%s",
@@ -7030,8 +7030,7 @@ qemuBuildMachineCommandLine(virCommandPtr cmd,
                 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                                _("dump-guest-core is not available "
                                  "with this QEMU binary"));
-                virBufferFreeAndReset(&buf);
-                return -1;
+                goto cleanup;
             }
 
             virBufferAsprintf(&buf, ",dump-guest-core=%s",
@@ -7039,22 +7038,19 @@ qemuBuildMachineCommandLine(virCommandPtr cmd,
         }
 
         if (def->mem.nosharepages) {
-            if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_MEM_MERGE)) {
-                virBufferAddLit(&buf, ",mem-merge=off");
-            } else {
+            if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_MEM_MERGE)) {
                 virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                                _("disable shared memory is not available "
                                  "with this QEMU binary"));
-                virBufferFreeAndReset(&buf);
-                return -1;
+                goto cleanup;
             }
+
+            virBufferAddLit(&buf, ",mem-merge=off");
         }
 
         if (def->keywrap &&
-            !qemuAppendKeyWrapMachineParms(&buf, qemuCaps, def->keywrap)) {
-            virBufferFreeAndReset(&buf);
-            return -1;
-        }
+            !qemuAppendKeyWrapMachineParms(&buf, qemuCaps, def->keywrap))
+            goto cleanup;
 
         if (def->features[VIR_DOMAIN_FEATURE_GIC] == VIR_TRISTATE_SWITCH_ON) {
             if (def->gic_version != VIR_GIC_VERSION_NONE) {
@@ -7062,8 +7058,7 @@ qemuBuildMachineCommandLine(virCommandPtr cmd,
                     virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                                    _("gic-version option is available "
                                      "only for ARM virt machine"));
-                    virBufferFreeAndReset(&buf);
-                    return -1;
+                    goto cleanup;
                 }
 
                 /* The default GIC version should not be specified on the
@@ -7074,8 +7069,7 @@ qemuBuildMachineCommandLine(virCommandPtr cmd,
                         virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                                        _("gic-version option is not available "
                                          "with this QEMU binary"));
-                        virBufferFreeAndReset(&buf);
-                        return -1;
+                        goto cleanup;
                     }
 
                     virBufferAsprintf(&buf, ",gic-version=%s",
@@ -7089,9 +7083,12 @@ qemuBuildMachineCommandLine(virCommandPtr cmd,
 
     if (obsoleteAccel &&
         qemuBuildObsoleteAccelArg(cmd, def, qemuCaps) < 0)
-        return -1;
+        goto cleanup;
 
-    return 0;
+    ret = 0;
+ cleanup:
+    virBufferFreeAndReset(&buf);
+    return ret;
 }
 
 static int
-- 
2.9.2