[PATCH 3/4] lxc: Rework lxcDomainDefNamespaceParse()

Michal Privoznik mprivozn at redhat.com
Wed Dec 16 21:13:00 UTC 2020


While fixing our schema for <lxc:namespace/> I've looked into the
parser and realized it could use some treating.

Signed-off-by: Michal Privoznik <mprivozn at redhat.com>
---
 src/lxc/lxc_domain.c | 40 ++++++++++++++++++++++------------------
 1 file changed, 22 insertions(+), 18 deletions(-)

diff --git a/src/lxc/lxc_domain.c b/src/lxc/lxc_domain.c
index 707262336b..255bfab495 100644
--- a/src/lxc/lxc_domain.c
+++ b/src/lxc/lxc_domain.c
@@ -208,26 +208,31 @@ static int
 lxcDomainDefNamespaceParse(xmlXPathContextPtr ctxt,
                            void **data)
 {
-    lxcDomainDefPtr lxcDef = g_new0(lxcDomainDef, 1);
+    lxcDomainDefPtr lxcDef = NULL;
     g_autofree xmlNodePtr *nodes = NULL;
-    bool uses_lxc_ns = false;
     VIR_XPATH_NODE_AUTORESTORE(ctxt)
-    int feature;
     int n;
     size_t i;
+    int ret = -1;
 
     if ((n = virXPathNodeSet("./lxc:namespace/*", ctxt, &nodes)) < 0)
-        goto error;
-    uses_lxc_ns |= n > 0;
+        return -1;
+
+    if (n == 0)
+        return 0;
+
+    lxcDef = g_new0(lxcDomainDef, 1);
 
     for (i = 0; i < n; i++) {
         g_autofree char *tmp = NULL;
+        int feature;
+
         if ((feature = virLXCDomainNamespaceTypeFromString(
                  (const char *)nodes[i]->name)) < 0) {
             virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                            _("unsupported Namespace feature: %s"),
-                            nodes[i]->name);
-            goto error;
+                           _("unsupported Namespace feature: %s"),
+                           nodes[i]->name);
+            goto cleanup;
         }
 
         ctxt->node = nodes[i];
@@ -235,31 +240,30 @@ lxcDomainDefNamespaceParse(xmlXPathContextPtr ctxt,
         if (!(tmp = virXMLPropString(nodes[i], "type"))) {
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            "%s", _("No lxc environment type specified"));
-            goto error;
+            goto cleanup;
         }
         if ((lxcDef->ns_source[feature] =
              virLXCDomainNamespaceSourceTypeFromString(tmp)) < 0) {
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            _("Unknown LXC namespace source '%s'"),
                            tmp);
-            goto error;
+            goto cleanup;
         }
 
         if (!(lxcDef->ns_val[feature] =
               virXMLPropString(nodes[i], "value"))) {
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            "%s", _("No lxc environment type specified"));
-            goto error;
+            goto cleanup;
         }
     }
-    if (uses_lxc_ns)
-        *data = lxcDef;
-    else
-        g_free(lxcDef);
-    return 0;
- error:
+
+    *data = g_steal_pointer(&lxcDef);
+    ret = 0;
+
+ cleanup:
     lxcDomainDefNamespaceFree(lxcDef);
-    return -1;
+    return ret;
 }
 
 
-- 
2.26.2




More information about the libvir-list mailing list