[libvirt] [PATCH v2 4/8] cpu: simplify failure cleanup paths

Daniel P. Berrangé berrange at redhat.com
Thu Aug 16 12:10:27 UTC 2018


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

Signed-off-by: Daniel P. Berrangé <berrange at 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 73af9d0885..8e4a3d0f77 100644
--- a/src/cpu/cpu_x86.c
+++ b/src/cpu/cpu_x86.c
@@ -723,15 +723,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);
@@ -739,24 +739,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;
 }
 
 
@@ -896,17 +893,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);
@@ -915,7 +912,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];
@@ -923,31 +920,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;
 }
 
 
@@ -1160,10 +1154,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;
@@ -1174,7 +1168,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))) {
@@ -1182,7 +1176,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);
@@ -1190,7 +1184,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)) {
@@ -1203,7 +1197,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);
@@ -1211,7 +1205,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);
@@ -1223,20 +1217,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;
@@ -1245,7 +1239,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))) {
@@ -1253,27 +1247,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.17.1




More information about the libvir-list mailing list