[libvirt] [PATCH 05/15] qemu: Extract parsing of qemu namespace env vars into separate function

Peter Krempa pkrempa at redhat.com
Tue Jun 18 13:10:14 UTC 2019


Simplify the main function by splitting out how we parse the extra
passthrough environment variables.

Note that the validation function checks that the first letter must be a
character or underscore which makes the check whether the name is
redundant.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/qemu/qemu_domain.c | 111 ++++++++++++++++++++++-------------------
 1 file changed, 59 insertions(+), 52 deletions(-)

diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index 3dfe4fc544..5c4bca1c6a 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -3143,6 +3143,62 @@ qemuDomainDefNamespaceParseCommandlineArgs(qemuDomainXmlNsDefPtr nsdef,
 }


+static int
+qemuDomainDefNamespaceParseCommandlineEnvNameValidate(const char *envname)
+{
+    if (!c_isalpha(envname[0]) && envname[0] != '_') {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("Invalid environment name, it must begin with a letter or underscore"));
+        return -1;
+    }
+
+    if (strspn(envname, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_") != strlen(envname)) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("Invalid environment name, it must contain only alphanumerics and underscore"));
+        return -1;
+    }
+
+    return 0;
+}
+
+
+static int
+qemuDomainDefNamespaceParseCommandlineEnv(qemuDomainXmlNsDefPtr nsdef,
+                                          xmlXPathContextPtr ctxt)
+{
+    VIR_AUTOFREE(xmlNodePtr *) nodes = NULL;
+    ssize_t nnodes;
+    size_t i;
+
+    if ((nnodes = virXPathNodeSet("./qemu:commandline/qemu:env", ctxt, &nodes)) < 0)
+        return -1;
+
+    if (nnodes == 0)
+        return 0;
+
+    if (VIR_ALLOC_N(nsdef->env_name, nnodes) < 0 ||
+        VIR_ALLOC_N(nsdef->env_value, nnodes) < 0)
+        return -1;
+
+    for (i = 0; i < nnodes; i++) {
+        if (!(nsdef->env_name[nsdef->num_env] = virXMLPropString(nodes[i], "name"))) {
+            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                           _("No qemu environment name specified"));
+            return -1;
+        }
+
+        if (qemuDomainDefNamespaceParseCommandlineEnvNameValidate(nsdef->env_name[nsdef->num_env]) < 0)
+            return -1;
+
+        nsdef->env_value[nsdef->num_env] = virXMLPropString(nodes[i], "value");
+        /* a NULL value for command is allowed, since it might be empty */
+        nsdef->num_env++;
+    }
+
+    return 0;
+}
+
+
 static int
 qemuDomainDefNamespaceParse(xmlDocPtr xml ATTRIBUTE_UNUSED,
                             xmlNodePtr root ATTRIBUTE_UNUSED,
@@ -3151,9 +3207,6 @@ qemuDomainDefNamespaceParse(xmlDocPtr xml ATTRIBUTE_UNUSED,
 {
     qemuDomainXmlNsDefPtr cmd = NULL;
     bool uses_qemu_ns = false;
-    xmlNodePtr *nodes = NULL;
-    int n;
-    size_t i;

     if (xmlXPathRegisterNs(ctxt, BAD_CAST "qemu", BAD_CAST QEMU_NAMESPACE_HREF) < 0) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -3165,58 +3218,13 @@ qemuDomainDefNamespaceParse(xmlDocPtr xml ATTRIBUTE_UNUSED,
     if (VIR_ALLOC(cmd) < 0)
         return -1;

-    if (qemuDomainDefNamespaceParseCommandlineArgs(cmd, ctxt) < 0)
+    if (qemuDomainDefNamespaceParseCommandlineArgs(cmd, ctxt) < 0 ||
+        qemuDomainDefNamespaceParseCommandlineEnv(cmd, ctxt) < 0)
         goto error;

-    if (cmd->num_args > 0)
+    if (cmd->num_args > 0 || cmd->num_env > 0)
         uses_qemu_ns = true;

-    /* now handle the extra environment variables */
-    n = virXPathNodeSet("./qemu:commandline/qemu:env", ctxt, &nodes);
-    if (n < 0)
-        goto error;
-    uses_qemu_ns |= n > 0;
-
-    if (n && VIR_ALLOC_N(cmd->env_name, n) < 0)
-        goto error;
-
-    if (n && VIR_ALLOC_N(cmd->env_value, n) < 0)
-        goto error;
-
-    for (i = 0; i < n; i++) {
-        char *tmp;
-
-        tmp = virXMLPropString(nodes[i], "name");
-        if (tmp == NULL) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           "%s", _("No qemu environment name specified"));
-            goto error;
-        }
-        if (tmp[0] == '\0') {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           "%s", _("Empty qemu environment name specified"));
-            goto error;
-        }
-        if (!c_isalpha(tmp[0]) && tmp[0] != '_') {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           "%s", _("Invalid environment name, it must begin with a letter or underscore"));
-            goto error;
-        }
-        if (strspn(tmp, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_") != strlen(tmp)) {
-            virReportError(VIR_ERR_INTERNAL_ERROR,
-                           "%s", _("Invalid environment name, it must contain only alphanumerics and underscore"));
-            goto error;
-        }
-
-        cmd->env_name[cmd->num_env] = tmp;
-
-        cmd->env_value[cmd->num_env] = virXMLPropString(nodes[i], "value");
-        /* a NULL value for command is allowed, since it might be empty */
-        cmd->num_env++;
-    }
-
-    VIR_FREE(nodes);
-
     if (uses_qemu_ns)
         *data = cmd;
     else
@@ -3225,7 +3233,6 @@ qemuDomainDefNamespaceParse(xmlDocPtr xml ATTRIBUTE_UNUSED,
     return 0;

  error:
-    VIR_FREE(nodes);
     qemuDomainDefNamespaceFree(cmd);
     return -1;
 }
-- 
2.21.0




More information about the libvir-list mailing list