[libvirt] [PATCH 07/12] storage: use GRegex virStorageBackendLogicalParseVolExtents

Ján Tomko posted 12 patches 6 years, 2 months ago
[libvirt] [PATCH 07/12] storage: use GRegex virStorageBackendLogicalParseVolExtents
Posted by Ján Tomko 6 years, 2 months ago
Using GRegex simplifies the code since g_match_info_fetch will
copy the matched substring for us.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
---
 src/storage/storage_backend_logical.c | 49 +++++++--------------------
 1 file changed, 13 insertions(+), 36 deletions(-)

diff --git a/src/storage/storage_backend_logical.c b/src/storage/storage_backend_logical.c
index 48023abd1a..d6c81e25b1 100644
--- a/src/storage/storage_backend_logical.c
+++ b/src/storage/storage_backend_logical.c
@@ -23,7 +23,6 @@
 
 #include <sys/wait.h>
 #include <sys/stat.h>
-#include <regex.h>
 #include <unistd.h>
 #include <fcntl.h>
 
@@ -121,16 +120,16 @@ static int
 virStorageBackendLogicalParseVolExtents(virStorageVolDefPtr vol,
                                         char **const groups)
 {
+    g_autoptr(GRegex) re = NULL;
+    g_autoptr(GError) err = NULL;
+    g_autoptr(GMatchInfo) info = NULL;
     int nextents, ret = -1;
     const char *regex_unit = "(\\S+)\\((\\S+)\\)";
     char *p = NULL;
     size_t i;
-    int err, nvars;
     unsigned long long offset, size, length;
     virStorageVolSourceExtent extent;
     g_autofree char *regex = NULL;
-    g_autofree regex_t *reg = NULL;
-    g_autofree regmatch_t *vars = NULL;
 
     memset(&extent, 0, sizeof(extent));
 
@@ -174,29 +173,14 @@ virStorageBackendLogicalParseVolExtents(virStorageVolDefPtr vol,
         strcat(regex, regex_unit);
     }
 
-    if (VIR_ALLOC(reg) < 0)
-        goto cleanup;
-
-    /* Each extent has a "path:offset" pair, and vars[0] will
-     * be the whole matched string.
-     */
-    nvars = (nextents * 2) + 1;
-    if (VIR_ALLOC_N(vars, nvars) < 0)
-        goto cleanup;
-
-    err = regcomp(reg, regex, REG_EXTENDED);
-    if (err != 0) {
-        char error[100];
-        regerror(err, reg, error, sizeof(error));
+    re = g_regex_new(regex, 0, 0, &err);
+    if (!re) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
-                       _("Failed to compile regex %s"),
-                       error);
-        goto cleanup;
+                       _("Failed to compile regex %s"), err->message);
+        return -1;
     }
 
-    err = regexec(reg, groups[3], nvars, vars, 0);
-    regfree(reg);
-    if (err != 0) {
+    if (!g_regex_match(re, groups[3], 0, &info)) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("malformed volume extent devices value"));
         goto cleanup;
@@ -204,23 +188,16 @@ virStorageBackendLogicalParseVolExtents(virStorageVolDefPtr vol,
 
     p = groups[3];
 
-    /* vars[0] is skipped */
+    /* Each extent has a "path:offset" pair, and match #0
+     * is the whole matched string.
+     */
     for (i = 0; i < nextents; i++) {
         size_t j;
-        int len;
         g_autofree char *offset_str = NULL;
 
         j = (i * 2) + 1;
-        len = vars[j].rm_eo - vars[j].rm_so;
-        p[vars[j].rm_eo] = '\0';
-
-        if (VIR_STRNDUP(extent.path,
-                        p + vars[j].rm_so, len) < 0)
-            goto cleanup;
-
-        len = vars[j + 1].rm_eo - vars[j + 1].rm_so;
-        if (VIR_STRNDUP(offset_str, p + vars[j + 1].rm_so, len) < 0)
-            goto cleanup;
+        extent.path = g_match_info_fetch(info, j);
+        offset_str = g_match_info_fetch(info, j + 1);
 
         if (virStrToLong_ull(offset_str, NULL, 10, &offset) < 0) {
             virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
-- 
2.21.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 07/12] storage: use GRegex virStorageBackendLogicalParseVolExtents
Posted by Peter Krempa 6 years, 2 months ago
On Wed, Nov 13, 2019 at 16:48:48 +0100, Ján Tomko wrote:
> Using GRegex simplifies the code since g_match_info_fetch will
> copy the matched substring for us.
> 
> Signed-off-by: Ján Tomko <jtomko@redhat.com>
> ---
>  src/storage/storage_backend_logical.c | 49 +++++++--------------------
>  1 file changed, 13 insertions(+), 36 deletions(-)

I'm getting a build failure with this patch:

../../src/storage/storage_backend_logical.c:128:11: error: variable 'p' set but not used [-Werror=unused-but-set-variable]
  128 |     char *p = NULL;

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

Re: [libvirt] [PATCH 07/12] storage: use GRegex virStorageBackendLogicalParseVolExtents
Posted by Ján Tomko 6 years, 2 months ago
On Thu, Nov 14, 2019 at 08:11:46AM +0100, Peter Krempa wrote:
>On Wed, Nov 13, 2019 at 16:48:48 +0100, Ján Tomko wrote:
>> Using GRegex simplifies the code since g_match_info_fetch will
>> copy the matched substring for us.
>>
>> Signed-off-by: Ján Tomko <jtomko@redhat.com>
>> ---
>>  src/storage/storage_backend_logical.c | 49 +++++++--------------------
>>  1 file changed, 13 insertions(+), 36 deletions(-)
>
>I'm getting a build failure with this patch:
>
>../../src/storage/storage_backend_logical.c:128:11: error: variable 'p' set but not used [-Werror=unused-but-set-variable]
>  128 |     char *p = NULL;

Oops, another case where GCC reports a slightly different set of
warnings than CLang.

Consider the following squashed in. Jano

diff --git a/src/storage/storage_backend_logical.c b/src/storage/storage_backend_logical.c
index d6c81e25b1..42dec05ba0 100644
--- a/src/storage/storage_backend_logical.c
+++ b/src/storage/storage_backend_logical.c
@@ -125,7 +125,6 @@ virStorageBackendLogicalParseVolExtents(virStorageVolDefPtr vol,
     g_autoptr(GMatchInfo) info = NULL;
     int nextents, ret = -1;
     const char *regex_unit = "(\\S+)\\((\\S+)\\)";
-    char *p = NULL;
     size_t i;
     unsigned long long offset, size, length;
     virStorageVolSourceExtent extent;
@@ -186,8 +185,6 @@ virStorageBackendLogicalParseVolExtents(virStorageVolDefPtr vol,
         goto cleanup;
     }
 
-    p = groups[3];
-
     /* Each extent has a "path:offset" pair, and match #0
      * is the whole matched string.
      */

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 07/12] storage: use GRegex virStorageBackendLogicalParseVolExtents
Posted by Peter Krempa 6 years, 2 months ago
On Thu, Nov 14, 2019 at 13:14:33 +0100, Ján Tomko wrote:
> On Thu, Nov 14, 2019 at 08:11:46AM +0100, Peter Krempa wrote:
> > On Wed, Nov 13, 2019 at 16:48:48 +0100, Ján Tomko wrote:
> > > Using GRegex simplifies the code since g_match_info_fetch will
> > > copy the matched substring for us.
> > > 
> > > Signed-off-by: Ján Tomko <jtomko@redhat.com>
> > > ---
> > >  src/storage/storage_backend_logical.c | 49 +++++++--------------------
> > >  1 file changed, 13 insertions(+), 36 deletions(-)
> > 
> > I'm getting a build failure with this patch:
> > 
> > ../../src/storage/storage_backend_logical.c:128:11: error: variable 'p' set but not used [-Werror=unused-but-set-variable]
> >  128 |     char *p = NULL;
> 
> Oops, another case where GCC reports a slightly different set of
> warnings than CLang.
> 
> Consider the following squashed in. Jano
> 
> diff --git a/src/storage/storage_backend_logical.c b/src/storage/storage_backend_logical.c
> index d6c81e25b1..42dec05ba0 100644
> --- a/src/storage/storage_backend_logical.c
> +++ b/src/storage/storage_backend_logical.c
> @@ -125,7 +125,6 @@ virStorageBackendLogicalParseVolExtents(virStorageVolDefPtr vol,
>     g_autoptr(GMatchInfo) info = NULL;
>     int nextents, ret = -1;
>     const char *regex_unit = "(\\S+)\\((\\S+)\\)";
> -    char *p = NULL;
>     size_t i;
>     unsigned long long offset, size, length;
>     virStorageVolSourceExtent extent;
> @@ -186,8 +185,6 @@ virStorageBackendLogicalParseVolExtents(virStorageVolDefPtr vol,
>         goto cleanup;
>     }
> 
> -    p = groups[3];
> -
>     /* Each extent has a "path:offset" pair, and match #0
>      * is the whole matched string.
>      */
> 


Reviewed-by: Peter Krempa <pkrempa@redhat.com>



> --
> libvir-list mailing list
> libvir-list@redhat.com
> https://www.redhat.com/mailman/listinfo/libvir-list

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list