[PATCH 1/3] util: Add function to check if string contains some illegal chars

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]