3e5111
From e302b6b9505730e9f1d139027c87867c973d2a26 Mon Sep 17 00:00:00 2001
3e5111
Message-Id: <e302b6b9505730e9f1d139027c87867c973d2a26@dist-git>
3e5111
From: Pavel Hrdina <phrdina@redhat.com>
3e5111
Date: Tue, 16 May 2017 11:46:12 +0200
3e5111
Subject: [PATCH] util: introduce virBufferEscapeRegex
3e5111
3e5111
Add a helper to escape all possible meta-characters used for
3e5111
POSIX extended regular expressions.
3e5111
3e5111
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
3e5111
(cherry picked from commit 0918b84968ab8aeb1dc17d5da839d68d947f478f)
3e5111
3e5111
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1446980
3e5111
3e5111
Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
3e5111
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
3e5111
---
3e5111
 src/libvirt_private.syms |  1 +
3e5111
 src/util/virbuffer.c     | 19 +++++++++++++++++++
3e5111
 src/util/virbuffer.h     |  3 +++
3e5111
 tests/virbuftest.c       | 40 ++++++++++++++++++++++++++++++++++++++++
3e5111
 4 files changed, 63 insertions(+)
3e5111
3e5111
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
3e5111
index 06c338384..7e1a06db3 100644
3e5111
--- a/src/libvirt_private.syms
3e5111
+++ b/src/libvirt_private.syms
3e5111
@@ -1292,6 +1292,7 @@ virBufferCurrentContent;
3e5111
 virBufferError;
3e5111
 virBufferEscape;
3e5111
 virBufferEscapeN;
3e5111
+virBufferEscapeRegex;
3e5111
 virBufferEscapeSexpr;
3e5111
 virBufferEscapeShell;
3e5111
 virBufferEscapeString;
3e5111
diff --git a/src/util/virbuffer.c b/src/util/virbuffer.c
3e5111
index 80c8e289d..f07b119c0 100644
3e5111
--- a/src/util/virbuffer.c
3e5111
+++ b/src/util/virbuffer.c
3e5111
@@ -556,6 +556,25 @@ virBufferEscapeSexpr(virBufferPtr buf,
3e5111
 }
3e5111
 
3e5111
 /**
3e5111
+ * virBufferEscapeRegex:
3e5111
+ * @buf: the buffer to append to
3e5111
+ * @format: a printf like format string but with only one %s parameter
3e5111
+ * @str: the string argument which needs to be escaped
3e5111
+ *
3e5111
+ * Do a formatted print with a single string to a buffer.  The @str is
3e5111
+ * escaped to avoid using POSIX extended regular expression meta-characters.
3e5111
+ * Escaping is not applied to characters specified in @format. Auto
3e5111
+ * indentation may be applied.
3e5111
+ */
3e5111
+void
3e5111
+virBufferEscapeRegex(virBufferPtr buf,
3e5111
+                     const char *format,
3e5111
+                     const char *str)
3e5111
+{
3e5111
+    virBufferEscape(buf, '\\', "^$.|?*+()[]{}\\", format, str);
3e5111
+}
3e5111
+
3e5111
+/**
3e5111
  * virBufferEscape:
3e5111
  * @buf: the buffer to append to
3e5111
  * @escape: the escape character to inject
3e5111
diff --git a/src/util/virbuffer.h b/src/util/virbuffer.h
3e5111
index d1b64ca3a..7a7014aa7 100644
3e5111
--- a/src/util/virbuffer.h
3e5111
+++ b/src/util/virbuffer.h
3e5111
@@ -88,6 +88,9 @@ void virBufferEscapeString(virBufferPtr buf, const char *format,
3e5111
                            const char *str);
3e5111
 void virBufferEscapeSexpr(virBufferPtr buf, const char *format,
3e5111
                           const char *str);
3e5111
+void virBufferEscapeRegex(virBufferPtr buf,
3e5111
+                          const char *format,
3e5111
+                          const char *str);
3e5111
 void virBufferEscapeShell(virBufferPtr buf, const char *str);
3e5111
 void virBufferURIEncodeString(virBufferPtr buf, const char *str);
3e5111
 
3e5111
diff --git a/tests/virbuftest.c b/tests/virbuftest.c
3e5111
index 8ec6ce51e..6f8d4b72c 100644
3e5111
--- a/tests/virbuftest.c
3e5111
+++ b/tests/virbuftest.c
3e5111
@@ -405,6 +405,35 @@ testBufEscapeN(const void *opaque)
3e5111
 
3e5111
 
3e5111
 static int
3e5111
+testBufEscapeRegex(const void *opaque)
3e5111
+{
3e5111
+    const struct testBufAddStrData *data = opaque;
3e5111
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
3e5111
+    char *actual;
3e5111
+    int ret = -1;
3e5111
+
3e5111
+    virBufferEscapeRegex(&buf, "%s", data->data);
3e5111
+
3e5111
+    if (!(actual = virBufferContentAndReset(&buf))) {
3e5111
+        VIR_TEST_DEBUG("testBufEscapeN: buf is empty");
3e5111
+        goto cleanup;
3e5111
+    }
3e5111
+
3e5111
+    if (STRNEQ_NULLABLE(actual, data->expect)) {
3e5111
+        VIR_TEST_DEBUG("testBufEscapeN: Strings don't match:\n");
3e5111
+        virTestDifference(stderr, data->expect, actual);
3e5111
+        goto cleanup;
3e5111
+    }
3e5111
+
3e5111
+    ret = 0;
3e5111
+
3e5111
+ cleanup:
3e5111
+    VIR_FREE(actual);
3e5111
+    return ret;
3e5111
+}
3e5111
+
3e5111
+
3e5111
+static int
3e5111
 testBufSetIndent(const void *opaque ATTRIBUTE_UNUSED)
3e5111
 {
3e5111
     virBuffer buf = VIR_BUFFER_INITIALIZER;
3e5111
@@ -492,6 +521,17 @@ mymain(void)
3e5111
     DO_TEST_ESCAPEN("equal=escape", "equal\\=escape");
3e5111
     DO_TEST_ESCAPEN("comma,equal=escape", "comma,,equal\\=escape");
3e5111
 
3e5111
+#define DO_TEST_ESCAPE_REGEX(data, expect)                                  \
3e5111
+    do {                                                                    \
3e5111
+        struct testBufAddStrData info = { data, expect };                   \
3e5111
+        if (virTestRun("Buf: EscapeRegex", testBufEscapeRegex, &info) < 0)  \
3e5111
+            ret = -1;                                                       \
3e5111
+    } while (0)
3e5111
+
3e5111
+    DO_TEST_ESCAPE_REGEX("noescape", "noescape");
3e5111
+    DO_TEST_ESCAPE_REGEX("^$.|?*+()[]{}\\",
3e5111
+                         "\\^\\$\\.\\|\\?\\*\\+\\(\\)\\[\\]\\{\\}\\\\");
3e5111
+
3e5111
     return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
3e5111
 }
3e5111
 
3e5111
-- 
3e5111
2.13.0
3e5111