[libvirt] [PATCH 05/12] libxl: use GRegex in xenParseSxprVifRate

Ján Tomko jtomko at redhat.com
Wed Nov 13 15:48:46 UTC 2019


Use GRegex from GLib instead of regcomp.

Signed-off-by: Ján Tomko <jtomko at redhat.com>
---
 src/libxl/xen_common.c | 20 +++++++-------------
 1 file changed, 7 insertions(+), 13 deletions(-)

diff --git a/src/libxl/xen_common.c b/src/libxl/xen_common.c
index c06ab6e995..15a2db8add 100644
--- a/src/libxl/xen_common.c
+++ b/src/libxl/xen_common.c
@@ -24,8 +24,6 @@
 
 #include <config.h>
 
-#include <regex.h>
-
 #include "internal.h"
 #include "virerror.h"
 #include "virconf.h"
@@ -1060,10 +1058,10 @@ static const char *vif_bytes_per_sec_re = "^[0-9]+[GMK]?[Bb]/s$";
 static int
 xenParseSxprVifRate(const char *rate, unsigned long long *kbytes_per_sec)
 {
+    g_autoptr(GRegex) regex = NULL;
+    g_autoptr(GError) err = NULL;
     g_autofree char *trate = NULL;
     char *p;
-    regex_t rec;
-    int err;
     char *suffix;
     unsigned long long tmp;
     int ret = -1;
@@ -1074,17 +1072,14 @@ xenParseSxprVifRate(const char *rate, unsigned long long *kbytes_per_sec)
     if (p != NULL)
         *p = 0;
 
-    err = regcomp(&rec, vif_bytes_per_sec_re, REG_EXTENDED|REG_NOSUB);
-    if (err != 0) {
-        char error[100];
-        regerror(err, &rec, error, sizeof(error));
+    regex = g_regex_new(vif_bytes_per_sec_re, 0, 0, &err);
+    if (!regex) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("Failed to compile regular expression '%s': %s"),
-                       vif_bytes_per_sec_re, error);
-        goto cleanup;
+                       _("Failed to compile regex %s"), err->message);
+        return -1;
     }
 
-    if (regexec(&rec, trate, 0, NULL, 0)) {
+    if (!g_regex_match(regex, trate, 0, NULL)) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Invalid rate '%s' specified"), rate);
         goto cleanup;
@@ -1108,7 +1103,6 @@ xenParseSxprVifRate(const char *rate, unsigned long long *kbytes_per_sec)
     ret = 0;
 
  cleanup:
-    regfree(&rec);
     return ret;
 }
 
-- 
2.21.0




More information about the libvir-list mailing list