[libvirt] [PATCH v2 03/32] util: Introduce VIR_DEFINE_AUTOPTR_FUNC for virStorageAuthDef

John Ferlan jferlan at redhat.com
Fri Feb 8 18:36:57 UTC 2019


Let's make use of the auto __cleanup capabilities cleaning up any
now unnecessary goto paths.

Signed-off-by: John Ferlan <jferlan at redhat.com>
Reviewed-by: Erik Skultety <eskultet at redhat.com>
---
 src/conf/domain_conf.c        | 26 ++++++++++----------------
 src/conf/storage_conf.c       |  3 +--
 src/qemu/qemu_parse_command.c |  3 +--
 src/util/virstoragefile.c     | 16 ++++++----------
 src/util/virstoragefile.h     |  2 ++
 5 files changed, 20 insertions(+), 30 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index a33f18c957..2b1389035e 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -7578,11 +7578,10 @@ virDomainHostdevSubsysSCSIiSCSIDefParseXML(xmlNodePtr sourcenode,
                                            virDomainHostdevSubsysSCSIPtr def,
                                            xmlXPathContextPtr ctxt)
 {
-    int ret = -1;
     int auth_secret_usage = -1;
     xmlNodePtr cur;
-    virStorageAuthDefPtr authdef = NULL;
     virDomainHostdevSubsysSCSIiSCSIPtr iscsisrc = &def->u.iscsi;
+    VIR_AUTOPTR(virStorageAuthDef) authdef = NULL;
 
     /* For the purposes of command line creation, this needs to look
      * like a disk storage source */
@@ -7594,23 +7593,23 @@ virDomainHostdevSubsysSCSIiSCSIDefParseXML(xmlNodePtr sourcenode,
     if (!(iscsisrc->src->path = virXMLPropString(sourcenode, "name"))) {
         virReportError(VIR_ERR_XML_ERROR, "%s",
                        _("missing iSCSI hostdev source path name"));
-        goto cleanup;
+        return -1;
     }
 
     if (virDomainStorageNetworkParseHosts(sourcenode, &iscsisrc->src->hosts,
                                           &iscsisrc->src->nhosts) < 0)
-        goto cleanup;
+        return -1;
 
     if (iscsisrc->src->nhosts < 1) {
         virReportError(VIR_ERR_XML_ERROR, "%s",
                        _("missing the host address for the iSCSI hostdev"));
-        goto cleanup;
+        return -1;
     }
     if (iscsisrc->src->nhosts > 1) {
         virReportError(VIR_ERR_XML_ERROR, "%s",
                        _("only one source host address may be specified "
                          "for the iSCSI hostdev"));
-        goto cleanup;
+        return -1;
     }
 
     cur = sourcenode->children;
@@ -7618,29 +7617,25 @@ virDomainHostdevSubsysSCSIiSCSIDefParseXML(xmlNodePtr sourcenode,
         if (cur->type == XML_ELEMENT_NODE &&
             virXMLNodeNameEqual(cur, "auth")) {
             if (!(authdef = virStorageAuthDefParse(cur, ctxt)))
-                goto cleanup;
+                return -1;
             if ((auth_secret_usage =
                  virSecretUsageTypeFromString(authdef->secrettype)) < 0) {
                 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                                _("invalid secret type %s"),
                                authdef->secrettype);
-                goto cleanup;
+                return -1;
             }
             if (auth_secret_usage != VIR_SECRET_USAGE_TYPE_ISCSI) {
                 virReportError(VIR_ERR_INTERNAL_ERROR,
                                _("hostdev invalid secret type '%s'"),
                                authdef->secrettype);
-                goto cleanup;
+                return -1;
             }
             VIR_STEAL_PTR(iscsisrc->src->auth, authdef);
         }
         cur = cur->next;
     }
-    ret = 0;
-
- cleanup:
-    virStorageAuthDefFree(authdef);
-    return ret;
+    return 0;
 }
 
 static int
@@ -9683,7 +9678,6 @@ virDomainDiskDefParseXML(virDomainXMLOptionPtr xmlopt,
     virStorageEncryptionPtr encryption = NULL;
     char *serial = NULL;
     char *startupPolicy = NULL;
-    virStorageAuthDefPtr authdef = NULL;
     char *tray = NULL;
     char *removable = NULL;
     char *logical_block_size = NULL;
@@ -9692,6 +9686,7 @@ virDomainDiskDefParseXML(virDomainXMLOptionPtr xmlopt,
     char *vendor = NULL;
     char *product = NULL;
     char *domain_name = NULL;
+    VIR_AUTOPTR(virStorageAuthDef) authdef = NULL;
 
     if (!(def = virDomainDiskDefNew(xmlopt)))
         return NULL;
@@ -10093,7 +10088,6 @@ virDomainDiskDefParseXML(virDomainXMLOptionPtr xmlopt,
     VIR_FREE(target);
     VIR_FREE(tray);
     VIR_FREE(removable);
-    virStorageAuthDefFree(authdef);
     VIR_FREE(devaddr);
     VIR_FREE(serial);
     virStorageEncryptionFree(encryption);
diff --git a/src/conf/storage_conf.c b/src/conf/storage_conf.c
index fbd62e1305..ead24816bc 100644
--- a/src/conf/storage_conf.c
+++ b/src/conf/storage_conf.c
@@ -458,11 +458,11 @@ virStoragePoolDefParseSource(xmlXPathContextPtr ctxt,
     int nsource;
     size_t i;
     virStoragePoolOptionsPtr options;
-    virStorageAuthDefPtr authdef = NULL;
     char *name = NULL;
     char *port = NULL;
     char *ver = NULL;
     int n;
+    VIR_AUTOPTR(virStorageAuthDef) authdef = NULL;
 
     relnode = ctxt->node;
     ctxt->node = node;
@@ -614,7 +614,6 @@ virStoragePoolDefParseSource(xmlXPathContextPtr ctxt,
 
     VIR_FREE(port);
     VIR_FREE(nodeset);
-    virStorageAuthDefFree(authdef);
     return ret;
 }
 
diff --git a/src/qemu/qemu_parse_command.c b/src/qemu/qemu_parse_command.c
index 679d49d442..81691cb85e 100644
--- a/src/qemu/qemu_parse_command.c
+++ b/src/qemu/qemu_parse_command.c
@@ -59,7 +59,7 @@ qemuParseDriveURIString(virDomainDiskDefPtr def, virURIPtr uri,
     char *sock = NULL;
     char *volimg = NULL;
     char *secret = NULL;
-    virStorageAuthDefPtr authdef = NULL;
+    VIR_AUTOPTR(virStorageAuthDef) authdef = NULL;
 
     if (VIR_ALLOC(def->src->hosts) < 0)
         goto error;
@@ -151,7 +151,6 @@ qemuParseDriveURIString(virDomainDiskDefPtr def, virURIPtr uri,
  error:
     virStorageNetHostDefClear(def->src->hosts);
     VIR_FREE(def->src->hosts);
-    virStorageAuthDefFree(authdef);
     goto cleanup;
 }
 
diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c
index 7fbeea78eb..1eb1ede59d 100644
--- a/src/util/virstoragefile.c
+++ b/src/util/virstoragefile.c
@@ -1879,25 +1879,23 @@ virStorageAuthDefFree(virStorageAuthDefPtr authdef)
 virStorageAuthDefPtr
 virStorageAuthDefCopy(const virStorageAuthDef *src)
 {
-    virStorageAuthDefPtr authdef;
     virStorageAuthDefPtr ret = NULL;
+    VIR_AUTOPTR(virStorageAuthDef) authdef = NULL;
 
     if (VIR_ALLOC(authdef) < 0)
         return NULL;
 
     if (VIR_STRDUP(authdef->username, src->username) < 0)
-        goto cleanup;
+        return NULL;
     /* Not present for storage pool, but used for disk source */
     if (VIR_STRDUP(authdef->secrettype, src->secrettype) < 0)
-        goto cleanup;
+        return NULL;
     authdef->authType = src->authType;
 
     if (virSecretLookupDefCopy(&authdef->seclookupdef, &src->seclookupdef) < 0)
-        goto cleanup;
+        return NULL;
 
     VIR_STEAL_PTR(ret, authdef);
- cleanup:
-    virStorageAuthDefFree(authdef);
     return ret;
 }
 
@@ -1907,10 +1905,10 @@ virStorageAuthDefParse(xmlNodePtr node,
                        xmlXPathContextPtr ctxt)
 {
     xmlNodePtr saveNode = ctxt->node;
-    virStorageAuthDefPtr authdef = NULL;
     virStorageAuthDefPtr ret = NULL;
     xmlNodePtr secretnode = NULL;
     char *authtype = NULL;
+    VIR_AUTOPTR(virStorageAuthDef) authdef = NULL;
 
     ctxt->node = node;
 
@@ -1958,7 +1956,6 @@ virStorageAuthDefParse(xmlNodePtr node,
 
  cleanup:
     VIR_FREE(authtype);
-    virStorageAuthDefFree(authdef);
     ctxt->node = saveNode;
 
     return ret;
@@ -2832,7 +2829,7 @@ virStorageSourceParseRBDColonString(const char *rbdstr,
 {
     char *options = NULL;
     char *p, *e, *next;
-    virStorageAuthDefPtr authdef = NULL;
+    VIR_AUTOPTR(virStorageAuthDef) authdef = NULL;
 
     /* optionally skip the "rbd:" prefix if provided */
     if (STRPREFIX(rbdstr, "rbd:"))
@@ -2935,7 +2932,6 @@ virStorageSourceParseRBDColonString(const char *rbdstr,
 
  error:
     VIR_FREE(options);
-    virStorageAuthDefFree(authdef);
     return -1;
 }
 
diff --git a/src/util/virstoragefile.h b/src/util/virstoragefile.h
index a98d5103fa..eacc927ea6 100644
--- a/src/util/virstoragefile.h
+++ b/src/util/virstoragefile.h
@@ -543,4 +543,6 @@ void virStorageFileReportBrokenChain(int errcode,
                                      virStorageSourcePtr src,
                                      virStorageSourcePtr parent);
 
+VIR_DEFINE_AUTOPTR_FUNC(virStorageAuthDef, virStorageAuthDefFree);
+
 #endif /* LIBVIRT_VIRSTORAGEFILE_H */
-- 
2.20.1




More information about the libvir-list mailing list