[PATCH 1/2] util: Add function to check if string contains some 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 new-line.
---
 src/util/virxml.c | 18 ++++++++++++++++++
 src/util/virxml.h |  4 ++++
 2 files changed, 22 insertions(+)

diff --git a/src/util/virxml.c b/src/util/virxml.c
index 03bd784..450487e 100644
--- a/src/util/virxml.c
+++ b/src/util/virxml.c
@@ -890,6 +890,24 @@ virXMLChildElementCount(xmlNodePtr node)
     return ret;
 }
 
+/**
+ * virXMLCheckString: checks if string contains at least one of
+ * forbidden characters
+ *
+ * Returns: 0 if string don't contains any of given characters, -1 otherwise
+ */
+int virXMLCheckString(const char *nodeName, char *str,
+                      const char *forbiddenChars)
+{
+    char *c;
+    c = strpbrk(str, forbiddenChars);
+    if (c) {
+        virReportError(VIR_ERR_XML_DETAIL,
+            _("invalid char in %s: %c"), nodeName, *c);
+        return -1;
+    }
+    return 0;
+}
 
 /**
  * virXMLNodeToString: convert an XML node ptr to an XML string
diff --git a/src/util/virxml.h b/src/util/virxml.h
index 7a0a1da..676bf5e 100644
--- a/src/util/virxml.h
+++ b/src/util/virxml.h
@@ -75,6 +75,10 @@ char *          virXMLPropString(xmlNodePtr node,
                                  const char *name);
 long     virXMLChildElementCount(xmlNodePtr node);
 
+int            virXMLCheckString(const char *nodeName,
+                                 char *str,
+                                 const char *forbiddenChars);
+
 /* Internal function; prefer the macros below.  */
 xmlDocPtr      virXMLParseHelper(int domcode,
                                  const char *filename,
-- 
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]