[PATCH 02/25] util: xml: Introduce virXMLParseStringCtxtRoot

Peter Krempa pkrempa at redhat.com
Fri Apr 16 15:34:20 UTC 2021


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

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/conf/domain_conf.c  |  9 +--------
 src/conf/storage_conf.c | 16 +++++-----------
 src/util/virxml.h       |  4 ++++
 3 files changed, 10 insertions(+), 19 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 17bbeddec6..6795f0b3be 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -16438,16 +16438,9 @@ virDomainDiskDefParse(const char *xmlStr,
     g_autoptr(xmlDoc) xml = NULL;
     g_autoptr(xmlXPathContext) ctxt = NULL;

-    if (!(xml = virXMLParseStringCtxt(xmlStr, _("(disk_definition)"), &ctxt)))
+    if (!(xml = virXMLParseStringCtxtRoot(xmlStr, _("(disk_definition)"), "disk", &ctxt)))
         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);
 }

diff --git a/src/conf/storage_conf.c b/src/conf/storage_conf.c
index 66419616da..328650bd6a 100644
--- a/src/conf/storage_conf.c
+++ b/src/conf/storage_conf.c
@@ -691,25 +691,19 @@ virStoragePoolDefParseSourceString(const char *srcSpec,
                                    int pool_type)
 {
     g_autoptr(xmlDoc) doc = NULL;
-    xmlNodePtr node = NULL;
     g_autoptr(xmlXPathContext) xpath_ctxt = NULL;
     g_autoptr(virStoragePoolSource) def = NULL;

-    if (!(doc = virXMLParseStringCtxt(srcSpec,
-                                      _("(storage_source_specification)"),
-                                      &xpath_ctxt)))
+    if (!(doc = virXMLParseStringCtxtRoot(srcSpec,
+                                          _("(storage_source_specification)"),
+                                          "source",
+                                          &xpath_ctxt)))
         return NULL;

     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,
-                                     node) < 0)
+                                     xpath_ctxt->node) < 0)
         return NULL;

     return g_steal_pointer(&def);
diff --git a/src/util/virxml.h b/src/util/virxml.h
index 38da2931a4..2b40398eee 100644
--- a/src/util/virxml.h
+++ b/src/util/virxml.h
@@ -221,6 +221,10 @@ virXMLPickShellSafeComment(const char *str1,
 #define virXMLParseStringCtxt(xmlStr, url, 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:
  * @filename: file to parse
-- 
2.30.2




More information about the libvir-list mailing list