[libvirt] [PATCH] log: actually do substring matches with fnmatch

Daniel P. Berrangé berrange at redhat.com
Mon May 14 13:53:49 UTC 2018


Historically we matched log filters with strstr(), and when switching to
fnmatch in cbb0fd3cfdc287f6f4653ef1f04a7cfb2ea51b27, it was stated that
we would continue to match substrings, with "foo" being equivalent to
"*foo*". Unfortuntely I forget to provide the code to actually make that
happen. This fixes it to prepend and append "*" if not already present in
the user's pattern.

Signed-off-by: Daniel P. Berrangé <berrange at redhat.com>
---
 src/util/virlog.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/src/util/virlog.c b/src/util/virlog.c
index be9fc0cf78..d548010b10 100644
--- a/src/util/virlog.c
+++ b/src/util/virlog.c
@@ -1409,6 +1409,8 @@ virLogFilterNew(const char *match,
 {
     virLogFilterPtr ret = NULL;
     char *mdup = NULL;
+    size_t mlen = strlen(match);
+    size_t off = 0;
 
     virCheckFlags(VIR_LOG_STACK_TRACE, NULL);
 
@@ -1418,9 +1420,19 @@ virLogFilterNew(const char *match,
         return NULL;
     }
 
-    if (VIR_STRDUP_QUIET(mdup, match) < 0)
+    if (VIR_ALLOC_N_QUIET(mdup, mlen + 3) < 0)
         return NULL;
 
+    if (match[0] != '*') {
+        mdup[off++] = '*';
+    }
+    memcpy(mdup + off, match, mlen + 1);
+    off += mlen;
+    if (match[mlen - 1] != '*') {
+        mdup[off++] = '*';
+        mdup[off++] = '\0';
+    }
+
     if (VIR_ALLOC_QUIET(ret) < 0) {
         VIR_FREE(mdup);
         return NULL;
-- 
2.17.0




More information about the libvir-list mailing list