[libvirt] [PATCH 4/6] util: storageencryption: Refactor cleanup section in virStorageEncryptionParseXML

Peter Krempa pkrempa at redhat.com
Tue Mar 6 14:27:13 UTC 2018


The function used the 'cleanup' label only in error cases. This patch
makes the code pass the cleanup label in every case and removes few
unnecessary VIR_FREEs.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/util/virstorageencryption.c | 32 +++++++++++++++-----------------
 1 file changed, 15 insertions(+), 17 deletions(-)

diff --git a/src/util/virstorageencryption.c b/src/util/virstorageencryption.c
index 116a2358ae..f3de5ff7a7 100644
--- a/src/util/virstorageencryption.c
+++ b/src/util/virstorageencryption.c
@@ -246,13 +246,14 @@ static virStorageEncryptionPtr
 virStorageEncryptionParseXML(xmlXPathContextPtr ctxt)
 {
     xmlNodePtr *nodes = NULL;
-    virStorageEncryptionPtr ret;
+    virStorageEncryptionPtr encdef = NULL;
+    virStorageEncryptionPtr ret = NULL;
     char *format_str = NULL;
     int n;
     size_t i;

-    if (VIR_ALLOC(ret) < 0)
-        return NULL;
+    if (VIR_ALLOC(encdef) < 0)
+        goto cleanup;

     if (!(format_str = virXPathString("string(./@format)", ctxt))) {
         virReportError(VIR_ERR_XML_ERROR, "%s",
@@ -260,60 +261,57 @@ virStorageEncryptionParseXML(xmlXPathContextPtr ctxt)
         goto cleanup;
     }

-    if ((ret->format =
+    if ((encdef->format =
          virStorageEncryptionFormatTypeFromString(format_str)) < 0) {
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                        _("unknown volume encryption format type %s"),
                        format_str);
         goto cleanup;
     }
-    VIR_FREE(format_str);

     if ((n = virXPathNodeSet("./secret", ctxt, &nodes)) < 0)
         goto cleanup;

     if (n > 0) {
-        if (VIR_ALLOC_N(ret->secrets, n) < 0)
+        if (VIR_ALLOC_N(encdef->secrets, n) < 0)
             goto cleanup;
-        ret->nsecrets = n;
+        encdef->nsecrets = n;

         for (i = 0; i < n; i++) {
-            if (!(ret->secrets[i] =
+            if (!(encdef->secrets[i] =
                   virStorageEncryptionSecretParse(ctxt, nodes[i])))
                 goto cleanup;
         }
-        VIR_FREE(nodes);
     }

-    if (ret->format == VIR_STORAGE_ENCRYPTION_FORMAT_LUKS) {
+    if (encdef->format == VIR_STORAGE_ENCRYPTION_FORMAT_LUKS) {
         xmlNodePtr tmpnode;

         if ((tmpnode = virXPathNode("./cipher[1]", ctxt))) {
-            if (virStorageEncryptionInfoParseCipher(tmpnode, &ret->encinfo) < 0)
+            if (virStorageEncryptionInfoParseCipher(tmpnode, &encdef->encinfo) < 0)
                 goto cleanup;
         }

         if ((tmpnode = virXPathNode("./ivgen[1]", ctxt))) {
             /* If no cipher node, then fail */
-            if (!ret->encinfo.cipher_name) {
+            if (!encdef->encinfo.cipher_name) {
                 virReportError(VIR_ERR_XML_ERROR, "%s",
                                 _("ivgen element found, but cipher is missing"));
                 goto cleanup;
             }

-            if (virStorageEncryptionInfoParseIvgen(tmpnode, &ret->encinfo) < 0)
+            if (virStorageEncryptionInfoParseIvgen(tmpnode, &encdef->encinfo) < 0)
                 goto cleanup;
         }
     }

-
-    return ret;
+    VIR_STEAL_PTR(ret, encdef);

  cleanup:
     VIR_FREE(format_str);
     VIR_FREE(nodes);
-    virStorageEncryptionFree(ret);
-    return NULL;
+    virStorageEncryptionFree(encdef);
+    return ret;
 }

 virStorageEncryptionPtr
-- 
2.16.2




More information about the libvir-list mailing list