]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
lxc: Rework lxcDomainDefNamespaceParse()
authorMichal Privoznik <mprivozn@redhat.com>
Tue, 15 Dec 2020 19:06:22 +0000 (20:06 +0100)
committerMichal Privoznik <mprivozn@redhat.com>
Thu, 17 Dec 2020 08:42:32 +0000 (09:42 +0100)
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@redhat.com>
Reviewed-by: Laine Stump <laine@redhat.com>
src/lxc/lxc_domain.c

index 707262336bed81db2778f4a1a32112ba8c9e50be..255bfab495741bb94a03044d14309955534216b7 100644 (file)
@@ -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;
 }