Blob Blame History Raw
From 85d367abe6063225117cc27c85e01e36a7b70409 Mon Sep 17 00:00:00 2001
Message-Id: <85d367abe6063225117cc27c85e01e36a7b70409@dist-git>
From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= <berrange@redhat.com>
Date: Fri, 21 Jun 2019 09:24:47 +0200
Subject: [PATCH] cpu: simplify failure cleanup paths
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Get rid of the separate 'error:' label, so all code paths jump straight
to the 'cleanup:' label.

Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
(cherry picked from commit 18cab54c3a0bc72390f29300684396690a7ecf51)

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

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
Message-Id: <40ff4abfea153cf8210286a84967c3ca7850b775.1561068591.git.jdenemar@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
---
 src/cpu/cpu_ppc64.c | 38 ++++++++++++------------
 src/cpu/cpu_x86.c   | 71 ++++++++++++++++++++-------------------------
 2 files changed, 49 insertions(+), 60 deletions(-)

diff --git a/src/cpu/cpu_ppc64.c b/src/cpu/cpu_ppc64.c
index 75da5b77d8..fcba7e9b37 100644
--- a/src/cpu/cpu_ppc64.c
+++ b/src/cpu/cpu_ppc64.c
@@ -288,27 +288,28 @@ ppc64VendorParse(xmlXPathContextPtr ctxt ATTRIBUTE_UNUSED,
 {
     struct ppc64_map *map = data;
     struct ppc64_vendor *vendor;
+    int ret = -1;
 
     if (VIR_ALLOC(vendor) < 0)
         return -1;
 
     if (VIR_STRDUP(vendor->name, name) < 0)
-        goto error;
+        goto cleanup;
 
     if (ppc64VendorFind(map, vendor->name)) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("CPU vendor %s already defined"), vendor->name);
-        goto error;
+        goto cleanup;
     }
 
     if (VIR_APPEND_ELEMENT(map->vendors, map->nvendors, vendor) < 0)
-        goto error;
+        goto cleanup;
 
-    return 0;
+    ret = 0;
 
- error:
+ cleanup:
     ppc64VendorFree(vendor);
-    return -1;
+    return ret;
 }
 
 
@@ -327,15 +328,15 @@ ppc64ModelParse(xmlXPathContextPtr ctxt,
     int ret = -1;
 
     if (VIR_ALLOC(model) < 0)
-        goto error;
+        goto cleanup;
 
     if (VIR_STRDUP(model->name, name) < 0)
-        goto error;
+        goto cleanup;
 
     if (ppc64ModelFind(map, model->name)) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("CPU model %s already defined"), model->name);
-        goto error;
+        goto cleanup;
     }
 
     if (virXPathBoolean("boolean(./vendor)", ctxt)) {
@@ -344,14 +345,14 @@ ppc64ModelParse(xmlXPathContextPtr ctxt,
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            _("Invalid vendor element in CPU model %s"),
                            model->name);
-            goto error;
+            goto cleanup;
         }
 
         if (!(model->vendor = ppc64VendorFind(map, vendor))) {
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            _("Unknown vendor %s referenced by CPU model %s"),
                            vendor, model->name);
-            goto error;
+            goto cleanup;
         }
     }
 
@@ -359,11 +360,11 @@ ppc64ModelParse(xmlXPathContextPtr ctxt,
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Missing PVR information for CPU model %s"),
                        model->name);
-        goto error;
+        goto cleanup;
     }
 
     if (VIR_ALLOC_N(model->data.pvr, n) < 0)
-        goto error;
+        goto cleanup;
 
     model->data.len = n;
 
@@ -374,7 +375,7 @@ ppc64ModelParse(xmlXPathContextPtr ctxt,
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            _("Missing or invalid PVR value in CPU model %s"),
                            model->name);
-            goto error;
+            goto cleanup;
         }
         model->data.pvr[i].value = pvr;
 
@@ -382,24 +383,21 @@ ppc64ModelParse(xmlXPathContextPtr ctxt,
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            _("Missing or invalid PVR mask in CPU model %s"),
                            model->name);
-            goto error;
+            goto cleanup;
         }
         model->data.pvr[i].mask = pvr;
     }
 
     if (VIR_APPEND_ELEMENT(map->models, map->nmodels, model) < 0)
-        goto error;
+        goto cleanup;
 
     ret = 0;
 
  cleanup:
+    ppc64ModelFree(model);
     VIR_FREE(vendor);
     VIR_FREE(nodes);
     return ret;
-
- error:
-    ppc64ModelFree(model);
-    goto cleanup;
 }
 
 
diff --git a/src/cpu/cpu_x86.c b/src/cpu/cpu_x86.c
index 11dbd1e757..ce48ca6867 100644
--- a/src/cpu/cpu_x86.c
+++ b/src/cpu/cpu_x86.c
@@ -731,15 +731,15 @@ x86VendorParse(xmlXPathContextPtr ctxt,
     int ret = -1;
 
     if (VIR_ALLOC(vendor) < 0)
-        goto error;
+        goto cleanup;
 
     if (VIR_STRDUP(vendor->name, name) < 0)
-        goto error;
+        goto cleanup;
 
     if (x86VendorFind(map, vendor->name)) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("CPU vendor %s already defined"), vendor->name);
-        goto error;
+        goto cleanup;
     }
 
     string = virXPathString("string(@string)", ctxt);
@@ -747,24 +747,21 @@ x86VendorParse(xmlXPathContextPtr ctxt,
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Missing vendor string for CPU vendor %s"),
                        vendor->name);
-        goto error;
+        goto cleanup;
     }
 
     if (virCPUx86VendorToCPUID(string, &vendor->cpuid) < 0)
-        goto error;
+        goto cleanup;
 
     if (VIR_APPEND_ELEMENT(map->vendors, map->nvendors, vendor) < 0)
-        goto error;
+        goto cleanup;
 
     ret = 0;
 
  cleanup:
+    x86VendorFree(vendor);
     VIR_FREE(string);
     return ret;
-
- error:
-    x86VendorFree(vendor);
-    goto cleanup;
 }
 
 
@@ -904,17 +901,17 @@ x86FeatureParse(xmlXPathContextPtr ctxt,
     int ret = -1;
 
     if (!(feature = x86FeatureNew()))
-        goto error;
+        goto cleanup;
 
     feature->migratable = true;
 
     if (VIR_STRDUP(feature->name, name) < 0)
-        goto error;
+        goto cleanup;
 
     if (x86FeatureFind(map, feature->name)) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("CPU feature %s already defined"), feature->name);
-        goto error;
+        goto cleanup;
     }
 
     str = virXPathString("string(@migratable)", ctxt);
@@ -923,7 +920,7 @@ x86FeatureParse(xmlXPathContextPtr ctxt,
 
     n = virXPathNodeSet("./cpuid", ctxt, &nodes);
     if (n < 0)
-        goto error;
+        goto cleanup;
 
     for (i = 0; i < n; i++) {
         ctxt->node = nodes[i];
@@ -931,31 +928,28 @@ x86FeatureParse(xmlXPathContextPtr ctxt,
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            _("Invalid cpuid[%zu] in %s feature"),
                            i, feature->name);
-            goto error;
+            goto cleanup;
         }
         if (virCPUx86DataAddCPUIDInt(&feature->data, &cpuid))
-            goto error;
+            goto cleanup;
     }
 
     if (!feature->migratable &&
         VIR_APPEND_ELEMENT_COPY(map->migrate_blockers,
                                 map->nblockers,
                                 feature) < 0)
-        goto error;
+        goto cleanup;
 
     if (VIR_APPEND_ELEMENT(map->features, map->nfeatures, feature) < 0)
-        goto error;
+        goto cleanup;
 
     ret = 0;
 
  cleanup:
+    x86FeatureFree(feature);
     VIR_FREE(nodes);
     VIR_FREE(str);
     return ret;
-
- error:
-    x86FeatureFree(feature);
-    goto cleanup;
 }
 
 
@@ -1168,10 +1162,10 @@ x86ModelParse(xmlXPathContextPtr ctxt,
     int ret = -1;
 
     if (!(model = x86ModelNew()))
-        goto error;
+        goto cleanup;
 
     if (VIR_STRDUP(model->name, name) < 0)
-        goto error;
+        goto cleanup;
 
     if (virXPathNode("./model", ctxt)) {
         virCPUx86ModelPtr ancestor;
@@ -1182,7 +1176,7 @@ x86ModelParse(xmlXPathContextPtr ctxt,
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            _("Missing ancestor's name in CPU model %s"),
                            model->name);
-            goto error;
+            goto cleanup;
         }
 
         if (!(ancestor = x86ModelFind(map, anname))) {
@@ -1190,7 +1184,7 @@ x86ModelParse(xmlXPathContextPtr ctxt,
                            _("Ancestor model %s not found for CPU model %s"),
                            anname, model->name);
             VIR_FREE(anname);
-            goto error;
+            goto cleanup;
         }
 
         VIR_FREE(anname);
@@ -1198,7 +1192,7 @@ x86ModelParse(xmlXPathContextPtr ctxt,
         model->vendor = ancestor->vendor;
         model->signature = ancestor->signature;
         if (x86DataCopy(&model->data, &ancestor->data) < 0)
-            goto error;
+            goto cleanup;
     }
 
     if (virXPathBoolean("boolean(./signature)", ctxt)) {
@@ -1211,7 +1205,7 @@ x86ModelParse(xmlXPathContextPtr ctxt,
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            _("Invalid CPU signature family in model %s"),
                            model->name);
-            goto error;
+            goto cleanup;
         }
 
         rc = virXPathUInt("string(./signature/@model)", ctxt, &sigModel);
@@ -1219,7 +1213,7 @@ x86ModelParse(xmlXPathContextPtr ctxt,
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            _("Invalid CPU signature model in model %s"),
                            model->name);
-            goto error;
+            goto cleanup;
         }
 
         model->signature = x86MakeSignature(sigFamily, sigModel, 0);
@@ -1231,20 +1225,20 @@ x86ModelParse(xmlXPathContextPtr ctxt,
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            _("Invalid vendor element in CPU model %s"),
                            model->name);
-            goto error;
+            goto cleanup;
         }
 
         if (!(model->vendor = x86VendorFind(map, vendor))) {
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            _("Unknown vendor %s referenced by CPU model %s"),
                            vendor, model->name);
-            goto error;
+            goto cleanup;
         }
     }
 
     n = virXPathNodeSet("./feature", ctxt, &nodes);
     if (n < 0)
-        goto error;
+        goto cleanup;
 
     for (i = 0; i < n; i++) {
         virCPUx86FeaturePtr feature;
@@ -1253,7 +1247,7 @@ x86ModelParse(xmlXPathContextPtr ctxt,
         if (!(ftname = virXMLPropString(nodes[i], "name"))) {
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            _("Missing feature name for CPU model %s"), model->name);
-            goto error;
+            goto cleanup;
         }
 
         if (!(feature = x86FeatureFind(map, ftname))) {
@@ -1261,27 +1255,24 @@ x86ModelParse(xmlXPathContextPtr ctxt,
                            _("Feature %s required by CPU model %s not found"),
                            ftname, model->name);
             VIR_FREE(ftname);
-            goto error;
+            goto cleanup;
         }
         VIR_FREE(ftname);
 
         if (x86DataAdd(&model->data, &feature->data))
-            goto error;
+            goto cleanup;
     }
 
     if (VIR_APPEND_ELEMENT(map->models, map->nmodels, model) < 0)
-        goto error;
+        goto cleanup;
 
     ret = 0;
 
  cleanup:
+    x86ModelFree(model);
     VIR_FREE(vendor);
     VIR_FREE(nodes);
     return ret;
-
- error:
-    x86ModelFree(model);
-    goto cleanup;
 }
 
 
-- 
2.22.0