]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: storageencryption: Refactor cleanup section in virStorageEncryptionParseXML
authorPeter Krempa <pkrempa@redhat.com>
Tue, 6 Mar 2018 13:57:17 +0000 (14:57 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Thu, 8 Mar 2018 13:29:50 +0000 (14:29 +0100)
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@redhat.com>
src/util/virstorageencryption.c

index 116a2358ae5c6f399ca250dbff02efd7437b31df..f3de5ff7a78cf268b102ab7d11d03e6c7ac6bd12 100644 (file)
@@ -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