c480ed
From 85d367abe6063225117cc27c85e01e36a7b70409 Mon Sep 17 00:00:00 2001
c480ed
Message-Id: <85d367abe6063225117cc27c85e01e36a7b70409@dist-git>
c480ed
From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= <berrange@redhat.com>
c480ed
Date: Fri, 21 Jun 2019 09:24:47 +0200
c480ed
Subject: [PATCH] cpu: simplify failure cleanup paths
c480ed
MIME-Version: 1.0
c480ed
Content-Type: text/plain; charset=UTF-8
c480ed
Content-Transfer-Encoding: 8bit
c480ed
c480ed
Get rid of the separate 'error:' label, so all code paths jump straight
c480ed
to the 'cleanup:' label.
c480ed
c480ed
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
c480ed
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
c480ed
(cherry picked from commit 18cab54c3a0bc72390f29300684396690a7ecf51)
c480ed
c480ed
https://bugzilla.redhat.com/show_bug.cgi?id=1686895
c480ed
c480ed
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
c480ed
Message-Id: <40ff4abfea153cf8210286a84967c3ca7850b775.1561068591.git.jdenemar@redhat.com>
c480ed
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c480ed
---
c480ed
 src/cpu/cpu_ppc64.c | 38 ++++++++++++------------
c480ed
 src/cpu/cpu_x86.c   | 71 ++++++++++++++++++++-------------------------
c480ed
 2 files changed, 49 insertions(+), 60 deletions(-)
c480ed
c480ed
diff --git a/src/cpu/cpu_ppc64.c b/src/cpu/cpu_ppc64.c
c480ed
index 75da5b77d8..fcba7e9b37 100644
c480ed
--- a/src/cpu/cpu_ppc64.c
c480ed
+++ b/src/cpu/cpu_ppc64.c
c480ed
@@ -288,27 +288,28 @@ ppc64VendorParse(xmlXPathContextPtr ctxt ATTRIBUTE_UNUSED,
c480ed
 {
c480ed
     struct ppc64_map *map = data;
c480ed
     struct ppc64_vendor *vendor;
c480ed
+    int ret = -1;
c480ed
 
c480ed
     if (VIR_ALLOC(vendor) < 0)
c480ed
         return -1;
c480ed
 
c480ed
     if (VIR_STRDUP(vendor->name, name) < 0)
c480ed
-        goto error;
c480ed
+        goto cleanup;
c480ed
 
c480ed
     if (ppc64VendorFind(map, vendor->name)) {
c480ed
         virReportError(VIR_ERR_INTERNAL_ERROR,
c480ed
                        _("CPU vendor %s already defined"), vendor->name);
c480ed
-        goto error;
c480ed
+        goto cleanup;
c480ed
     }
c480ed
 
c480ed
     if (VIR_APPEND_ELEMENT(map->vendors, map->nvendors, vendor) < 0)
c480ed
-        goto error;
c480ed
+        goto cleanup;
c480ed
 
c480ed
-    return 0;
c480ed
+    ret = 0;
c480ed
 
c480ed
- error:
c480ed
+ cleanup:
c480ed
     ppc64VendorFree(vendor);
c480ed
-    return -1;
c480ed
+    return ret;
c480ed
 }
c480ed
 
c480ed
 
c480ed
@@ -327,15 +328,15 @@ ppc64ModelParse(xmlXPathContextPtr ctxt,
c480ed
     int ret = -1;
c480ed
 
c480ed
     if (VIR_ALLOC(model) < 0)
c480ed
-        goto error;
c480ed
+        goto cleanup;
c480ed
 
c480ed
     if (VIR_STRDUP(model->name, name) < 0)
c480ed
-        goto error;
c480ed
+        goto cleanup;
c480ed
 
c480ed
     if (ppc64ModelFind(map, model->name)) {
c480ed
         virReportError(VIR_ERR_INTERNAL_ERROR,
c480ed
                        _("CPU model %s already defined"), model->name);
c480ed
-        goto error;
c480ed
+        goto cleanup;
c480ed
     }
c480ed
 
c480ed
     if (virXPathBoolean("boolean(./vendor)", ctxt)) {
c480ed
@@ -344,14 +345,14 @@ ppc64ModelParse(xmlXPathContextPtr ctxt,
c480ed
             virReportError(VIR_ERR_INTERNAL_ERROR,
c480ed
                            _("Invalid vendor element in CPU model %s"),
c480ed
                            model->name);
c480ed
-            goto error;
c480ed
+            goto cleanup;
c480ed
         }
c480ed
 
c480ed
         if (!(model->vendor = ppc64VendorFind(map, vendor))) {
c480ed
             virReportError(VIR_ERR_INTERNAL_ERROR,
c480ed
                            _("Unknown vendor %s referenced by CPU model %s"),
c480ed
                            vendor, model->name);
c480ed
-            goto error;
c480ed
+            goto cleanup;
c480ed
         }
c480ed
     }
c480ed
 
c480ed
@@ -359,11 +360,11 @@ ppc64ModelParse(xmlXPathContextPtr ctxt,
c480ed
         virReportError(VIR_ERR_INTERNAL_ERROR,
c480ed
                        _("Missing PVR information for CPU model %s"),
c480ed
                        model->name);
c480ed
-        goto error;
c480ed
+        goto cleanup;
c480ed
     }
c480ed
 
c480ed
     if (VIR_ALLOC_N(model->data.pvr, n) < 0)
c480ed
-        goto error;
c480ed
+        goto cleanup;
c480ed
 
c480ed
     model->data.len = n;
c480ed
 
c480ed
@@ -374,7 +375,7 @@ ppc64ModelParse(xmlXPathContextPtr ctxt,
c480ed
             virReportError(VIR_ERR_INTERNAL_ERROR,
c480ed
                            _("Missing or invalid PVR value in CPU model %s"),
c480ed
                            model->name);
c480ed
-            goto error;
c480ed
+            goto cleanup;
c480ed
         }
c480ed
         model->data.pvr[i].value = pvr;
c480ed
 
c480ed
@@ -382,24 +383,21 @@ ppc64ModelParse(xmlXPathContextPtr ctxt,
c480ed
             virReportError(VIR_ERR_INTERNAL_ERROR,
c480ed
                            _("Missing or invalid PVR mask in CPU model %s"),
c480ed
                            model->name);
c480ed
-            goto error;
c480ed
+            goto cleanup;
c480ed
         }
c480ed
         model->data.pvr[i].mask = pvr;
c480ed
     }
c480ed
 
c480ed
     if (VIR_APPEND_ELEMENT(map->models, map->nmodels, model) < 0)
c480ed
-        goto error;
c480ed
+        goto cleanup;
c480ed
 
c480ed
     ret = 0;
c480ed
 
c480ed
  cleanup:
c480ed
+    ppc64ModelFree(model);
c480ed
     VIR_FREE(vendor);
c480ed
     VIR_FREE(nodes);
c480ed
     return ret;
c480ed
-
c480ed
- error:
c480ed
-    ppc64ModelFree(model);
c480ed
-    goto cleanup;
c480ed
 }
c480ed
 
c480ed
 
c480ed
diff --git a/src/cpu/cpu_x86.c b/src/cpu/cpu_x86.c
c480ed
index 11dbd1e757..ce48ca6867 100644
c480ed
--- a/src/cpu/cpu_x86.c
c480ed
+++ b/src/cpu/cpu_x86.c
c480ed
@@ -731,15 +731,15 @@ x86VendorParse(xmlXPathContextPtr ctxt,
c480ed
     int ret = -1;
c480ed
 
c480ed
     if (VIR_ALLOC(vendor) < 0)
c480ed
-        goto error;
c480ed
+        goto cleanup;
c480ed
 
c480ed
     if (VIR_STRDUP(vendor->name, name) < 0)
c480ed
-        goto error;
c480ed
+        goto cleanup;
c480ed
 
c480ed
     if (x86VendorFind(map, vendor->name)) {
c480ed
         virReportError(VIR_ERR_INTERNAL_ERROR,
c480ed
                        _("CPU vendor %s already defined"), vendor->name);
c480ed
-        goto error;
c480ed
+        goto cleanup;
c480ed
     }
c480ed
 
c480ed
     string = virXPathString("string(@string)", ctxt);
c480ed
@@ -747,24 +747,21 @@ x86VendorParse(xmlXPathContextPtr ctxt,
c480ed
         virReportError(VIR_ERR_INTERNAL_ERROR,
c480ed
                        _("Missing vendor string for CPU vendor %s"),
c480ed
                        vendor->name);
c480ed
-        goto error;
c480ed
+        goto cleanup;
c480ed
     }
c480ed
 
c480ed
     if (virCPUx86VendorToCPUID(string, &vendor->cpuid) < 0)
c480ed
-        goto error;
c480ed
+        goto cleanup;
c480ed
 
c480ed
     if (VIR_APPEND_ELEMENT(map->vendors, map->nvendors, vendor) < 0)
c480ed
-        goto error;
c480ed
+        goto cleanup;
c480ed
 
c480ed
     ret = 0;
c480ed
 
c480ed
  cleanup:
c480ed
+    x86VendorFree(vendor);
c480ed
     VIR_FREE(string);
c480ed
     return ret;
c480ed
-
c480ed
- error:
c480ed
-    x86VendorFree(vendor);
c480ed
-    goto cleanup;
c480ed
 }
c480ed
 
c480ed
 
c480ed
@@ -904,17 +901,17 @@ x86FeatureParse(xmlXPathContextPtr ctxt,
c480ed
     int ret = -1;
c480ed
 
c480ed
     if (!(feature = x86FeatureNew()))
c480ed
-        goto error;
c480ed
+        goto cleanup;
c480ed
 
c480ed
     feature->migratable = true;
c480ed
 
c480ed
     if (VIR_STRDUP(feature->name, name) < 0)
c480ed
-        goto error;
c480ed
+        goto cleanup;
c480ed
 
c480ed
     if (x86FeatureFind(map, feature->name)) {
c480ed
         virReportError(VIR_ERR_INTERNAL_ERROR,
c480ed
                        _("CPU feature %s already defined"), feature->name);
c480ed
-        goto error;
c480ed
+        goto cleanup;
c480ed
     }
c480ed
 
c480ed
     str = virXPathString("string(@migratable)", ctxt);
c480ed
@@ -923,7 +920,7 @@ x86FeatureParse(xmlXPathContextPtr ctxt,
c480ed
 
c480ed
     n = virXPathNodeSet("./cpuid", ctxt, &nodes);
c480ed
     if (n < 0)
c480ed
-        goto error;
c480ed
+        goto cleanup;
c480ed
 
c480ed
     for (i = 0; i < n; i++) {
c480ed
         ctxt->node = nodes[i];
c480ed
@@ -931,31 +928,28 @@ x86FeatureParse(xmlXPathContextPtr ctxt,
c480ed
             virReportError(VIR_ERR_INTERNAL_ERROR,
c480ed
                            _("Invalid cpuid[%zu] in %s feature"),
c480ed
                            i, feature->name);
c480ed
-            goto error;
c480ed
+            goto cleanup;
c480ed
         }
c480ed
         if (virCPUx86DataAddCPUIDInt(&feature->data, &cpuid))
c480ed
-            goto error;
c480ed
+            goto cleanup;
c480ed
     }
c480ed
 
c480ed
     if (!feature->migratable &&
c480ed
         VIR_APPEND_ELEMENT_COPY(map->migrate_blockers,
c480ed
                                 map->nblockers,
c480ed
                                 feature) < 0)
c480ed
-        goto error;
c480ed
+        goto cleanup;
c480ed
 
c480ed
     if (VIR_APPEND_ELEMENT(map->features, map->nfeatures, feature) < 0)
c480ed
-        goto error;
c480ed
+        goto cleanup;
c480ed
 
c480ed
     ret = 0;
c480ed
 
c480ed
  cleanup:
c480ed
+    x86FeatureFree(feature);
c480ed
     VIR_FREE(nodes);
c480ed
     VIR_FREE(str);
c480ed
     return ret;
c480ed
-
c480ed
- error:
c480ed
-    x86FeatureFree(feature);
c480ed
-    goto cleanup;
c480ed
 }
c480ed
 
c480ed
 
c480ed
@@ -1168,10 +1162,10 @@ x86ModelParse(xmlXPathContextPtr ctxt,
c480ed
     int ret = -1;
c480ed
 
c480ed
     if (!(model = x86ModelNew()))
c480ed
-        goto error;
c480ed
+        goto cleanup;
c480ed
 
c480ed
     if (VIR_STRDUP(model->name, name) < 0)
c480ed
-        goto error;
c480ed
+        goto cleanup;
c480ed
 
c480ed
     if (virXPathNode("./model", ctxt)) {
c480ed
         virCPUx86ModelPtr ancestor;
c480ed
@@ -1182,7 +1176,7 @@ x86ModelParse(xmlXPathContextPtr ctxt,
c480ed
             virReportError(VIR_ERR_INTERNAL_ERROR,
c480ed
                            _("Missing ancestor's name in CPU model %s"),
c480ed
                            model->name);
c480ed
-            goto error;
c480ed
+            goto cleanup;
c480ed
         }
c480ed
 
c480ed
         if (!(ancestor = x86ModelFind(map, anname))) {
c480ed
@@ -1190,7 +1184,7 @@ x86ModelParse(xmlXPathContextPtr ctxt,
c480ed
                            _("Ancestor model %s not found for CPU model %s"),
c480ed
                            anname, model->name);
c480ed
             VIR_FREE(anname);
c480ed
-            goto error;
c480ed
+            goto cleanup;
c480ed
         }
c480ed
 
c480ed
         VIR_FREE(anname);
c480ed
@@ -1198,7 +1192,7 @@ x86ModelParse(xmlXPathContextPtr ctxt,
c480ed
         model->vendor = ancestor->vendor;
c480ed
         model->signature = ancestor->signature;
c480ed
         if (x86DataCopy(&model->data, &ancestor->data) < 0)
c480ed
-            goto error;
c480ed
+            goto cleanup;
c480ed
     }
c480ed
 
c480ed
     if (virXPathBoolean("boolean(./signature)", ctxt)) {
c480ed
@@ -1211,7 +1205,7 @@ x86ModelParse(xmlXPathContextPtr ctxt,
c480ed
             virReportError(VIR_ERR_INTERNAL_ERROR,
c480ed
                            _("Invalid CPU signature family in model %s"),
c480ed
                            model->name);
c480ed
-            goto error;
c480ed
+            goto cleanup;
c480ed
         }
c480ed
 
c480ed
         rc = virXPathUInt("string(./signature/@model)", ctxt, &sigModel);
c480ed
@@ -1219,7 +1213,7 @@ x86ModelParse(xmlXPathContextPtr ctxt,
c480ed
             virReportError(VIR_ERR_INTERNAL_ERROR,
c480ed
                            _("Invalid CPU signature model in model %s"),
c480ed
                            model->name);
c480ed
-            goto error;
c480ed
+            goto cleanup;
c480ed
         }
c480ed
 
c480ed
         model->signature = x86MakeSignature(sigFamily, sigModel, 0);
c480ed
@@ -1231,20 +1225,20 @@ x86ModelParse(xmlXPathContextPtr ctxt,
c480ed
             virReportError(VIR_ERR_INTERNAL_ERROR,
c480ed
                            _("Invalid vendor element in CPU model %s"),
c480ed
                            model->name);
c480ed
-            goto error;
c480ed
+            goto cleanup;
c480ed
         }
c480ed
 
c480ed
         if (!(model->vendor = x86VendorFind(map, vendor))) {
c480ed
             virReportError(VIR_ERR_INTERNAL_ERROR,
c480ed
                            _("Unknown vendor %s referenced by CPU model %s"),
c480ed
                            vendor, model->name);
c480ed
-            goto error;
c480ed
+            goto cleanup;
c480ed
         }
c480ed
     }
c480ed
 
c480ed
     n = virXPathNodeSet("./feature", ctxt, &nodes);
c480ed
     if (n < 0)
c480ed
-        goto error;
c480ed
+        goto cleanup;
c480ed
 
c480ed
     for (i = 0; i < n; i++) {
c480ed
         virCPUx86FeaturePtr feature;
c480ed
@@ -1253,7 +1247,7 @@ x86ModelParse(xmlXPathContextPtr ctxt,
c480ed
         if (!(ftname = virXMLPropString(nodes[i], "name"))) {
c480ed
             virReportError(VIR_ERR_INTERNAL_ERROR,
c480ed
                            _("Missing feature name for CPU model %s"), model->name);
c480ed
-            goto error;
c480ed
+            goto cleanup;
c480ed
         }
c480ed
 
c480ed
         if (!(feature = x86FeatureFind(map, ftname))) {
c480ed
@@ -1261,27 +1255,24 @@ x86ModelParse(xmlXPathContextPtr ctxt,
c480ed
                            _("Feature %s required by CPU model %s not found"),
c480ed
                            ftname, model->name);
c480ed
             VIR_FREE(ftname);
c480ed
-            goto error;
c480ed
+            goto cleanup;
c480ed
         }
c480ed
         VIR_FREE(ftname);
c480ed
 
c480ed
         if (x86DataAdd(&model->data, &feature->data))
c480ed
-            goto error;
c480ed
+            goto cleanup;
c480ed
     }
c480ed
 
c480ed
     if (VIR_APPEND_ELEMENT(map->models, map->nmodels, model) < 0)
c480ed
-        goto error;
c480ed
+        goto cleanup;
c480ed
 
c480ed
     ret = 0;
c480ed
 
c480ed
  cleanup:
c480ed
+    x86ModelFree(model);
c480ed
     VIR_FREE(vendor);
c480ed
     VIR_FREE(nodes);
c480ed
     return ret;
c480ed
-
c480ed
- error:
c480ed
-    x86ModelFree(model);
c480ed
-    goto cleanup;
c480ed
 }
c480ed
 
c480ed
 
c480ed
-- 
c480ed
2.22.0
c480ed