util: xml: Introduce virXMLParseStringCtxtRoot

Use the new macro instead of virXMLParseStringCtxt in places where the
root node is being validated.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Peter Krempa 2021-04-14 14:16:28 +02:00
parent 3362ab5e02
commit 5c56538937
3 changed files with 10 additions and 19 deletions

View File

@ -16438,16 +16438,9 @@ virDomainDiskDefParse(const char *xmlStr,
g_autoptr(xmlDoc) xml = NULL; g_autoptr(xmlDoc) xml = NULL;
g_autoptr(xmlXPathContext) ctxt = NULL; g_autoptr(xmlXPathContext) ctxt = NULL;
if (!(xml = virXMLParseStringCtxt(xmlStr, _("(disk_definition)"), &ctxt))) if (!(xml = virXMLParseStringCtxtRoot(xmlStr, _("(disk_definition)"), "disk", &ctxt)))
return NULL; return NULL;
if (!virXMLNodeNameEqual(ctxt->node, "disk")) {
virReportError(VIR_ERR_XML_ERROR,
_("expecting root element of 'disk', not '%s'"),
ctxt->node->name);
return NULL;
}
return virDomainDiskDefParseXML(xmlopt, ctxt->node, ctxt, flags); return virDomainDiskDefParseXML(xmlopt, ctxt->node, ctxt, flags);
} }

View File

@ -691,25 +691,19 @@ virStoragePoolDefParseSourceString(const char *srcSpec,
int pool_type) int pool_type)
{ {
g_autoptr(xmlDoc) doc = NULL; g_autoptr(xmlDoc) doc = NULL;
xmlNodePtr node = NULL;
g_autoptr(xmlXPathContext) xpath_ctxt = NULL; g_autoptr(xmlXPathContext) xpath_ctxt = NULL;
g_autoptr(virStoragePoolSource) def = NULL; g_autoptr(virStoragePoolSource) def = NULL;
if (!(doc = virXMLParseStringCtxt(srcSpec, if (!(doc = virXMLParseStringCtxtRoot(srcSpec,
_("(storage_source_specification)"), _("(storage_source_specification)"),
"source",
&xpath_ctxt))) &xpath_ctxt)))
return NULL; return NULL;
def = g_new0(virStoragePoolSource, 1); def = g_new0(virStoragePoolSource, 1);
if (!(node = virXPathNode("/source", xpath_ctxt))) {
virReportError(VIR_ERR_XML_ERROR, "%s",
_("root element was not source"));
return NULL;
}
if (virStoragePoolDefParseSource(xpath_ctxt, def, pool_type, if (virStoragePoolDefParseSource(xpath_ctxt, def, pool_type,
node) < 0) xpath_ctxt->node) < 0)
return NULL; return NULL;
return g_steal_pointer(&def); return g_steal_pointer(&def);

View File

@ -221,6 +221,10 @@ virXMLPickShellSafeComment(const char *str1,
#define virXMLParseStringCtxt(xmlStr, url, pctxt) \ #define virXMLParseStringCtxt(xmlStr, url, pctxt) \
virXMLParseHelper(VIR_FROM_THIS, NULL, xmlStr, url, NULL, pctxt) virXMLParseHelper(VIR_FROM_THIS, NULL, xmlStr, url, NULL, pctxt)
/* virXMLParseStringCtxtRoot is same as above, except it also validates root node name */
#define virXMLParseStringCtxtRoot(xmlStr, url, rootnode, pctxt) \
virXMLParseHelper(VIR_FROM_THIS, NULL, xmlStr, url, rootnode, pctxt)
/** /**
* virXMLParseFileCtxt: * virXMLParseFileCtxt:
* @filename: file to parse * @filename: file to parse