From nobody Mon Feb 9 00:08:09 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 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=1618587443; cv=none; d=zohomail.com; s=zohoarc; b=gM0ATwpcwbmfw4D2Yr+N6XfPfvOeDgM+jI6iuRL1yflnjOfRT6uS9PtYLWom0HYntE6zc3mnoc6rE1ajxQ33DagQiZrjx07ediaFisr5CvpKYlCGry9vyeBCXdNXVQDS3XBprQBoFcvnx53HE9bJA8h3B+7an1RDiX1VMINmO1A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1618587443; 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; bh=ZAgKqjDrtCZGgEMKwb0lL2ZdNcZVlq9usXJ3BhGyd6I=; b=n+3oZO0PTqO5+ZlxQoMNEsYi68POC9LyjwZphNuNGdDi5Z3fOOf+7hRLVcg4qjnvJRm5wB731RYHwv/fWsvTnlXasWzsydQUniEYQl5QZi4zj5pO4PEmUig8ZLvoH7qheuP87MDrg4W269amvuBzvsdoUufDUHUYVwh3eGytTMc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1618587443928799.8753768928016; Fri, 16 Apr 2021 08:37:23 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-349-B1auhCSZPAW0OtgIKEliFA-1; Fri, 16 Apr 2021 11:37:18 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8F812100A60D; Fri, 16 Apr 2021 15:37:07 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0412710023B5; Fri, 16 Apr 2021 15:37:06 +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 92C851806D0F; Fri, 16 Apr 2021 15:37:03 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 13GFYoh2009220 for ; Fri, 16 Apr 2021 11:34:50 -0400 Received: by smtp.corp.redhat.com (Postfix) id 1951E1C4; Fri, 16 Apr 2021 15:34:50 +0000 (UTC) Received: from speedmetal.lan (unknown [10.40.208.33]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5B0745C3DF for ; Fri, 16 Apr 2021 15:34:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1618587442; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=ZAgKqjDrtCZGgEMKwb0lL2ZdNcZVlq9usXJ3BhGyd6I=; b=SqgD1hHu2r2vOwqeoA9Lnc/qGU87hPhh67sqHCV9TbWk6Q6RBOtVliKlZ5FsX2fxyCv/bo 8cgNTpZRaI4xMRyQQyJa6HKurWJeiaukJvhERNl2AmLbZNvCQu0BH6wzL6Z1rimNfSw9G1 sMcjQMI7DtmGBE5w6kZ17XIkPufS4PU= X-MC-Unique: B1auhCSZPAW0OtgIKEliFA-1 From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 01/25] virXMLParseHelper: Add root XML node name validation capability Date: Fri, 16 Apr 2021 17:34:19 +0200 Message-Id: <88f9f97ca927340a1ed6baedfc63efdac9de7227.1618587222.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Some callers want to validate the root XML node name. Add the capability to the parser helper to prevent open-coding. Signed-off-by: Peter Krempa --- src/util/virxml.c | 18 ++++++++++++++++-- src/util/virxml.h | 13 +++++++------ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/util/virxml.c b/src/util/virxml.c index 83644af8ce..d0d9494009 100644 --- a/src/util/virxml.c +++ b/src/util/virxml.c @@ -1020,11 +1020,15 @@ catchXMLError(void *ctx, const char *msg G_GNUC_UNU= SED, ...) * @filename: file to be parsed or NULL if string parsing is requested * @xmlStr: XML string to be parsed in case filename is NULL * @url: URL of XML document for string parser + * @rootelement: Optional name of the expected root element * @ctxt: optional pointer to populate with new context pointer * * Parse XML document provided either as a file or a string. The function * guarantees that the XML document contains a root element. * + * If @rootelement is not NULL, the name of the root element of the parsed= XML + * is vaidated against + * * Returns parsed XML document. */ xmlDocPtr @@ -1032,11 +1036,13 @@ virXMLParseHelper(int domcode, const char *filename, const char *xmlStr, const char *url, + const char *rootelement, xmlXPathContextPtr *ctxt) { struct virParserData private; g_autoptr(xmlParserCtxt) pctxt =3D NULL; g_autoptr(xmlDoc) xml =3D NULL; + xmlNodePtr rootnode; const char *docname; if (filename) @@ -1075,18 +1081,26 @@ virXMLParseHelper(int domcode, return NULL; } - if (xmlDocGetRootElement(xml) =3D=3D NULL) { + if (!(rootnode =3D xmlDocGetRootElement(xml))) { virGenericReportError(domcode, VIR_ERR_INTERNAL_ERROR, "%s", _("missing root element")); return NULL; } + if (rootelement && + !virXMLNodeNameEqual(rootnode, rootelement)) { + virReportError(VIR_ERR_XML_ERROR, + _("expecting root element of '%s', not '%s'"), + rootelement, rootnode->name); + return NULL; + } + if (ctxt) { if (!(*ctxt =3D virXMLXPathContextNew(xml))) return NULL; - (*ctxt)->node =3D xmlDocGetRootElement(xml); + (*ctxt)->node =3D rootnode; } return g_steal_pointer(&xml); diff --git a/src/util/virxml.h b/src/util/virxml.h index 022ead58b5..38da2931a4 100644 --- a/src/util/virxml.h +++ b/src/util/virxml.h @@ -150,6 +150,7 @@ virXMLParseHelper(int domcode, const char *filename, const char *xmlStr, const char *url, + const char *rootelement, xmlXPathContextPtr *ctxt); const char * @@ -166,7 +167,7 @@ virXMLPickShellSafeComment(const char *str1, * Return the parsed document object, or NULL on failure. */ #define virXMLParse(filename, xmlStr, url) \ - virXMLParseHelper(VIR_FROM_THIS, filename, xmlStr, url, NULL) + virXMLParseHelper(VIR_FROM_THIS, filename, xmlStr, url, NULL, NULL) /** * virXMLParseString: @@ -178,7 +179,7 @@ virXMLPickShellSafeComment(const char *str1, * Return the parsed document object, or NULL on failure. */ #define virXMLParseString(xmlStr, url) \ - virXMLParseHelper(VIR_FROM_THIS, NULL, xmlStr, url, NULL) + virXMLParseHelper(VIR_FROM_THIS, NULL, xmlStr, url, NULL, NULL) /** * virXMLParseFile: @@ -189,7 +190,7 @@ virXMLPickShellSafeComment(const char *str1, * Return the parsed document object, or NULL on failure. */ #define virXMLParseFile(filename) \ - virXMLParseHelper(VIR_FROM_THIS, filename, NULL, NULL, NULL) + virXMLParseHelper(VIR_FROM_THIS, filename, NULL, NULL, NULL, NULL) /** * virXMLParseCtxt: @@ -204,7 +205,7 @@ virXMLPickShellSafeComment(const char *str1, * Return the parsed document object, or NULL on failure. */ #define virXMLParseCtxt(filename, xmlStr, url, pctxt) \ - virXMLParseHelper(VIR_FROM_THIS, filename, xmlStr, url, pctxt) + virXMLParseHelper(VIR_FROM_THIS, filename, xmlStr, url, NULL, pctxt) /** * virXMLParseStringCtxt: @@ -218,7 +219,7 @@ virXMLPickShellSafeComment(const char *str1, * Return the parsed document object, or NULL on failure. */ #define virXMLParseStringCtxt(xmlStr, url, pctxt) \ - virXMLParseHelper(VIR_FROM_THIS, NULL, xmlStr, url, pctxt) + virXMLParseHelper(VIR_FROM_THIS, NULL, xmlStr, url, NULL, pctxt) /** * virXMLParseFileCtxt: @@ -231,7 +232,7 @@ virXMLPickShellSafeComment(const char *str1, * Return the parsed document object, or NULL on failure. */ #define virXMLParseFileCtxt(filename, pctxt) \ - virXMLParseHelper(VIR_FROM_THIS, filename, NULL, NULL, pctxt) + virXMLParseHelper(VIR_FROM_THIS, filename, NULL, NULL, NULL, pctxt) int virXMLSaveFile(const char *path, --=20 2.30.2