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 <pkrempa@xxxxxxxxxx> --- 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_UNUSED, ...) * @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 = NULL; g_autoptr(xmlDoc) xml = NULL; + xmlNodePtr rootnode; const char *docname; if (filename) @@ -1075,18 +1081,26 @@ virXMLParseHelper(int domcode, return NULL; } - if (xmlDocGetRootElement(xml) == NULL) { + if (!(rootnode = 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 = virXMLXPathContextNew(xml))) return NULL; - (*ctxt)->node = xmlDocGetRootElement(xml); + (*ctxt)->node = 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, -- 2.30.2