From nobody Mon Feb 9 16:45:06 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1560863495; cv=none; d=zoho.com; s=zohoarc; b=i1d2UU383U8hWf0Gdxum3yUmfuO6r1EFZOIWNoTJ1ZrtJOdUzejr0ziewG2OBSUkn+L4ii2oVYSw4db2nBbP+GxpD9dwzfGjb6lrATmwijOKTj8ZSR+5jZddMs8rJ1RyQdmB/xS7aP2dErqypnDTkNcgniqWGqxLSFtrofT8hvk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1560863495; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=tFGeZcnPCgd9mx3pUGYvJ6gCBGkWk7U/tt5bGN7Ha90=; b=AsV/6VWMU3egF+4o8xS36/dNDAPON0rJ6r7bO4uODd9VsAyTo27oENEXlcFBaLHEjyj5kYjmTZEiXpGrbSOsS2Vnh8nX/QoNFZ88tAaxKJn51WaTs4rn3HucdAB50XWX0LUR0vWcWJBoizCM2bRKayE7HXRE72KEvi23S9J4yXQ= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1560863495338455.8391602184603; Tue, 18 Jun 2019 06:11:35 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 28E5F30C1AFD; Tue, 18 Jun 2019 13:11:21 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0395617AE6; Tue, 18 Jun 2019 13:11:20 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id B9C60206D5; Tue, 18 Jun 2019 13:11:17 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x5IDAWmt018701 for ; Tue, 18 Jun 2019 09:10:32 -0400 Received: by smtp.corp.redhat.com (Postfix) id 09ED91001DD7; Tue, 18 Jun 2019 13:10:32 +0000 (UTC) Received: from angien.brq.redhat.com (unknown [10.43.2.229]) by smtp.corp.redhat.com (Postfix) with ESMTP id 899571001DCB for ; Tue, 18 Jun 2019 13:10:31 +0000 (UTC) From: Peter Krempa To: libvir-list@redhat.com Date: Tue, 18 Jun 2019 15:10:14 +0200 Message-Id: <39e8a22ad59eafc3ae50e44e19ddd655212aa00b.1560863172.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 05/15] qemu: Extract parsing of qemu namespace env vars into separate function X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.40]); Tue, 18 Jun 2019 13:11:26 +0000 (UTC) Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: J=C3=A1n Tomko --- 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(qemuDomai= nXmlNsDefPtr nsdef, } +static int +qemuDomainDefNamespaceParseCommandlineEnvNameValidate(const char *envname) +{ + if (!c_isalpha(envname[0]) && envname[0] !=3D '_') { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Invalid environment name, it must begin with a l= etter or underscore")); + return -1; + } + + if (strspn(envname, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw= xyz0123456789_") !=3D strlen(envname)) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Invalid environment name, it must contain only a= lphanumerics and underscore")); + return -1; + } + + return 0; +} + + +static int +qemuDomainDefNamespaceParseCommandlineEnv(qemuDomainXmlNsDefPtr nsdef, + xmlXPathContextPtr ctxt) +{ + VIR_AUTOFREE(xmlNodePtr *) nodes =3D NULL; + ssize_t nnodes; + size_t i; + + if ((nnodes =3D virXPathNodeSet("./qemu:commandline/qemu:env", ctxt, &= nodes)) < 0) + return -1; + + if (nnodes =3D=3D 0) + return 0; + + if (VIR_ALLOC_N(nsdef->env_name, nnodes) < 0 || + VIR_ALLOC_N(nsdef->env_value, nnodes) < 0) + return -1; + + for (i =3D 0; i < nnodes; i++) { + if (!(nsdef->env_name[nsdef->num_env] =3D virXMLPropString(nodes[i= ], "name"))) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("No qemu environment name specified")); + return -1; + } + + if (qemuDomainDefNamespaceParseCommandlineEnvNameValidate(nsdef->e= nv_name[nsdef->num_env]) < 0) + return -1; + + nsdef->env_value[nsdef->num_env] =3D virXMLPropString(nodes[i], "v= alue"); + /* 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_U= NUSED, { qemuDomainXmlNsDefPtr cmd =3D NULL; bool uses_qemu_ns =3D false; - xmlNodePtr *nodes =3D 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 =3D true; - /* now handle the extra environment variables */ - n =3D virXPathNodeSet("./qemu:commandline/qemu:env", ctxt, &nodes); - if (n < 0) - goto error; - uses_qemu_ns |=3D 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 =3D 0; i < n; i++) { - char *tmp; - - tmp =3D virXMLPropString(nodes[i], "name"); - if (tmp =3D=3D NULL) { - virReportError(VIR_ERR_INTERNAL_ERROR, - "%s", _("No qemu environment name specified")); - goto error; - } - if (tmp[0] =3D=3D '\0') { - virReportError(VIR_ERR_INTERNAL_ERROR, - "%s", _("Empty qemu environment name specified"= )); - goto error; - } - if (!c_isalpha(tmp[0]) && tmp[0] !=3D '_') { - virReportError(VIR_ERR_INTERNAL_ERROR, - "%s", _("Invalid environment name, it must begi= n with a letter or underscore")); - goto error; - } - if (strspn(tmp, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw= xyz0123456789_") !=3D strlen(tmp)) { - virReportError(VIR_ERR_INTERNAL_ERROR, - "%s", _("Invalid environment name, it must cont= ain only alphanumerics and underscore")); - goto error; - } - - cmd->env_name[cmd->num_env] =3D tmp; - - cmd->env_value[cmd->num_env] =3D 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 =3D cmd; else @@ -3225,7 +3233,6 @@ qemuDomainDefNamespaceParse(xmlDocPtr xml ATTRIBUTE_U= NUSED, return 0; error: - VIR_FREE(nodes); qemuDomainDefNamespaceFree(cmd); return -1; } --=20 2.21.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list