[libvirt] [PATCH v3 16/19] libxl: convert over to use GRegex for regular expressions

Daniel P. Berrangé berrange at redhat.com
Thu Oct 10 10:54:10 UTC 2019


Reviewed-by: Ján Tomko <jtomko at redhat.com>
Reviewed-by: Pavel Hrdina <phrdina at redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange at redhat.com>
---
 src/libxl/libxl_capabilities.c | 42 +++++++++++++++-------------------
 1 file changed, 19 insertions(+), 23 deletions(-)

diff --git a/src/libxl/libxl_capabilities.c b/src/libxl/libxl_capabilities.c
index 73ae0b3fa1..65c68ffb52 100644
--- a/src/libxl/libxl_capabilities.c
+++ b/src/libxl/libxl_capabilities.c
@@ -21,7 +21,6 @@
 #include <config.h>
 
 #include <libxl.h>
-#include <regex.h>
 
 #include "internal.h"
 #include "virlog.h"
@@ -374,10 +373,10 @@ static int
 libxlCapsInitGuests(libxl_ctx *ctx, virCapsPtr caps)
 {
     const libxl_version_info *ver_info;
-    int err;
-    regex_t regex;
+    g_autoptr(GRegex) regex = NULL;
+    g_autoptr(GError) err = NULL;
+    g_autoptr(GMatchInfo) info = NULL;
     char *str, *token;
-    regmatch_t subs[4];
     char *saveptr = NULL;
     size_t i;
 
@@ -398,12 +397,10 @@ libxlCapsInitGuests(libxl_ctx *ctx, virCapsPtr caps)
         return -1;
     }
 
-    err = regcomp(&regex, XEN_CAP_REGEX, REG_EXTENDED);
-    if (err != 0) {
-        char error[100];
-        regerror(err, &regex, error, sizeof(error));
+    regex = g_regex_new(XEN_CAP_REGEX, G_REGEX_EXTENDED, 0, &err);
+    if (!regex) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("Failed to compile regex %s"), error);
+                       _("Failed to compile regex %s"), err->message);
         return -1;
     }
 
@@ -436,31 +433,31 @@ libxlCapsInitGuests(libxl_ctx *ctx, virCapsPtr caps)
          nr_guest_archs < sizeof(guest_archs) / sizeof(guest_archs[0])
                  && (token = strtok_r(str, " ", &saveptr)) != NULL;
          str = NULL) {
-        if (regexec(&regex, token, sizeof(subs) / sizeof(subs[0]),
-                    subs, 0) == 0) {
-            int hvm = STRPREFIX(&token[subs[1].rm_so], "hvm");
+        if (g_regex_match(regex, token, 0, &info)) {
+            g_autofree char *modestr = g_match_info_fetch(info, 1);
+            g_autofree char *archstr = g_match_info_fetch(info, 2);
+            g_autofree char *suffixstr = g_match_info_fetch(info, 3);
+            int hvm = STRPREFIX(modestr, "hvm");
             virArch arch;
             int pae = 0, nonpae = 0, ia64_be = 0;
 
-            if (STRPREFIX(&token[subs[2].rm_so], "x86_32")) {
+            if (STRPREFIX(archstr, "x86_32")) {
                 arch = VIR_ARCH_I686;
-                if (subs[3].rm_so != -1 &&
-                    STRPREFIX(&token[subs[3].rm_so], "p"))
+                if (suffixstr != NULL && STRPREFIX(suffixstr, "p"))
                     pae = 1;
                 else
                     nonpae = 1;
-            } else if (STRPREFIX(&token[subs[2].rm_so], "x86_64")) {
+            } else if (STRPREFIX(archstr, "x86_64")) {
                 arch = VIR_ARCH_X86_64;
-            } else if (STRPREFIX(&token[subs[2].rm_so], "ia64")) {
+            } else if (STRPREFIX(archstr, "ia64")) {
                 arch = VIR_ARCH_ITANIUM;
-                if (subs[3].rm_so != -1 &&
-                    STRPREFIX(&token[subs[3].rm_so], "be"))
+                if (suffixstr != NULL && STRPREFIX(suffixstr, "be"))
                     ia64_be = 1;
-            } else if (STRPREFIX(&token[subs[2].rm_so], "powerpc64")) {
+            } else if (STRPREFIX(archstr, "powerpc64")) {
                 arch = VIR_ARCH_PPC64;
-            } else if (STRPREFIX(&token[subs[2].rm_so], "armv7l")) {
+            } else if (STRPREFIX(archstr, "armv7l")) {
                 arch = VIR_ARCH_ARMV7L;
-            } else if (STRPREFIX(&token[subs[2].rm_so], "aarch64")) {
+            } else if (STRPREFIX(archstr, "aarch64")) {
                 arch = VIR_ARCH_AARCH64;
             } else {
                 continue;
@@ -515,7 +512,6 @@ libxlCapsInitGuests(libxl_ctx *ctx, virCapsPtr caps)
 #endif
         }
     }
-    regfree(&regex);
 
     for (i = 0; i < nr_guest_archs; ++i) {
         virCapsGuestPtr guest;
-- 
2.21.0




More information about the libvir-list mailing list