This new function can be used to check if e.g. name of XML
node don't contains forbidden chars like "/" or "\n".
---
src/libvirt_private.syms | 1 +
src/util/virxml.c | 28 ++++++++++++++++++++++++++++
src/util/virxml.h | 3 +++
3 files changed, 32 insertions(+)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 55b6a24..f91f179 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2678,6 +2678,7 @@ virUUIDParse;
# util/virxml.h
virXMLChildElementCount;
virXMLExtractNamespaceXML;
+virXMLNodeHasIllegalChars;
virXMLNodeSanitizeNamespaces;
virXMLNodeToString;
virXMLParseHelper;
diff --git a/src/util/virxml.c b/src/util/virxml.c
index 03bd784..b0d0a94 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -462,6 +462,34 @@ virXPathLongLong(const char *xpath,
return ret;
}
+
+/**
+ * virXMLNodeHasIllegalChars: checks if string contains at least one of
+ * forbidden characters
+ *
+ * @node_name: Name of checked node
+ * @node: string to check
+ * @chars: illegal chars to check
+ *
+ * If node contains any of illegal chars VIR_ERR_XML_DETAIL error will be
+ * reported.
+ *
+ * Returns: 0 if string don't contains any of given characters, -1 otherwise
+ */
+int
+virXMLNodeHasIllegalChars(const char *node_name,
+ const char *node,
+ const char *chars)
+{
+ if (strpbrk(node, chars)) {
+ virReportError(VIR_ERR_XML_DETAIL,
+ _("invalid char in %s: 0x%02x"), node_name, *chars);
+ return -1;
+ }
+ return 0;
+}
+
+
/**
* virXMLPropString:
* @node: XML dom node pointer
diff --git a/src/util/virxml.h b/src/util/virxml.h
index 7a0a1da..4425cf8 100644
--- a/src/util/virxml.h
+++ b/src/util/virxml.h
@@ -71,6 +71,9 @@ xmlNodePtr virXPathNode(const char *xpath,
int virXPathNodeSet(const char *xpath,
xmlXPathContextPtr ctxt,
xmlNodePtr **list);
+int virXMLNodeHasIllegalChars(const char *node_name,
+ const char *node,
+ const char *chars);
char * virXMLPropString(xmlNodePtr node,
const char *name);
long virXMLChildElementCount(xmlNodePtr node);
--
2.10.0