[PATCH 13/21] domain_conf.c: modernize virDomainSmartcardDefParseXML

Daniel Henrique Barboza danielhb413 at gmail.com
Tue Nov 24 19:20:27 UTC 2020


Register a AUTOPTR_CLEANUP_FUNC for virDomainSmartcardDef and use
g_autoptr() to eliminate the 'error' label.

Signed-off-by: Daniel Henrique Barboza <danielhb413 at gmail.com>
---
 src/conf/domain_conf.c | 34 +++++++++++++++-------------------
 src/conf/domain_conf.h |  1 +
 2 files changed, 16 insertions(+), 19 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 09b284733a..566185fe43 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -13617,7 +13617,7 @@ virDomainSmartcardDefParseXML(virDomainXMLOptionPtr xmlopt,
                               unsigned int flags)
 {
     xmlNodePtr cur;
-    virDomainSmartcardDefPtr def;
+    g_autoptr(virDomainSmartcardDef) def = NULL;
     size_t i;
     g_autofree char *mode = NULL;
     g_autofree char *type = NULL;
@@ -13628,13 +13628,13 @@ virDomainSmartcardDefParseXML(virDomainXMLOptionPtr xmlopt,
     if (mode == NULL) {
         virReportError(VIR_ERR_XML_ERROR, "%s",
                        _("missing smartcard device mode"));
-        goto error;
+        return NULL;
     }
     if ((def->type = virDomainSmartcardTypeFromString(mode)) < 0) {
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                        _("unknown smartcard device mode: %s"),
                        mode);
-        goto error;
+        return NULL;
     }
 
     switch (def->type) {
@@ -13651,23 +13651,23 @@ virDomainSmartcardDefParseXML(virDomainXMLOptionPtr xmlopt,
                     virReportError(VIR_ERR_XML_ERROR, "%s",
                                    _("host-certificates mode needs "
                                      "exactly three certificates"));
-                    goto error;
+                    return NULL;
                 }
                 if (!(def->data.cert.file[i] = virXMLNodeContentString(cur)))
-                    goto error;
+                    return NULL;
 
                 i++;
             } else if (cur->type == XML_ELEMENT_NODE &&
                        virXMLNodeNameEqual(cur, "database") &&
                        !def->data.cert.database) {
                 if (!(def->data.cert.database = virXMLNodeContentString(cur)))
-                    goto error;
+                    return NULL;
 
                 if (*def->data.cert.database != '/') {
                     virReportError(VIR_ERR_XML_ERROR,
                                    _("expecting absolute path: %s"),
                                    def->data.cert.database);
-                    goto error;
+                    return NULL;
                 }
             }
             cur = cur->next;
@@ -13676,7 +13676,7 @@ virDomainSmartcardDefParseXML(virDomainXMLOptionPtr xmlopt,
             virReportError(VIR_ERR_XML_ERROR, "%s",
                            _("host-certificates mode needs "
                              "exactly three certificates"));
-            goto error;
+            return NULL;
         }
         break;
 
@@ -13686,23 +13686,23 @@ virDomainSmartcardDefParseXML(virDomainXMLOptionPtr xmlopt,
             virReportError(VIR_ERR_XML_ERROR, "%s",
                            _("passthrough mode requires a character "
                              "device type attribute"));
-            goto error;
+            return NULL;
         }
 
         if (!(def->data.passthru = virDomainChrSourceDefNew(xmlopt)))
-            goto error;
+            return NULL;
 
         if ((def->data.passthru->type = virDomainChrTypeFromString(type)) < 0) {
             virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                            _("unknown type presented to host for "
                              "character device: %s"), type);
-            goto error;
+            return NULL;
         }
 
         cur = node->children;
         if (virDomainChrSourceDefParseXML(def->data.passthru, cur, flags,
                                           NULL, ctxt) < 0)
-            goto error;
+            return NULL;
 
         if (def->data.passthru->type == VIR_DOMAIN_CHR_TYPE_SPICEVMC) {
             def->data.passthru->data.spicevmc
@@ -13714,17 +13714,13 @@ virDomainSmartcardDefParseXML(virDomainXMLOptionPtr xmlopt,
     default:
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("unknown smartcard mode"));
-        goto error;
+        return NULL;
     }
 
     if (virDomainDeviceInfoParseXML(xmlopt, node, &def->info, flags) < 0)
-        goto error;
-
-    return def;
+        return NULL;
 
- error:
-    virDomainSmartcardDefFree(def);
-    return NULL;
+    return g_steal_pointer(&def);
 }
 
 /* Parse the XML definition for a TPM device
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index cef17efe73..694f015011 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -3070,6 +3070,7 @@ void virDomainVsockDefFree(virDomainVsockDefPtr vsock);
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(virDomainVsockDef, virDomainVsockDefFree);
 void virDomainNetDefFree(virDomainNetDefPtr def);
 void virDomainSmartcardDefFree(virDomainSmartcardDefPtr def);
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(virDomainSmartcardDef, virDomainSmartcardDefFree);
 void virDomainChrDefFree(virDomainChrDefPtr def);
 int virDomainChrSourceDefCopy(virDomainChrSourceDefPtr dest,
                               virDomainChrSourceDefPtr src);
-- 
2.26.2




More information about the libvir-list mailing list