[libvirt] [PATCH 2/9] Remove VIR_STRNDUP usage that passes -1

Ján Tomko posted 9 patches 6 years, 2 months ago
[libvirt] [PATCH 2/9] Remove VIR_STRNDUP usage that passes -1
Posted by Ján Tomko 6 years, 2 months ago
Replace all the usage of
  VIR_STRNDUP(dest, b, p ? p - b : -1)
with separate calls to g_strndup/g_strdup.

Signed-off-by: Ján Tomko <jtomko@redhat.com>
---
 src/bhyve/bhyve_parse_command.c | 38 +++++++++++++++++++++------------
 src/libxl/xen_common.c          | 15 +++++++------
 src/remote/remote_driver.c      |  6 ++++--
 3 files changed, 37 insertions(+), 22 deletions(-)

diff --git a/src/bhyve/bhyve_parse_command.c b/src/bhyve/bhyve_parse_command.c
index fa3b881f21..5c5dfae4a9 100644
--- a/src/bhyve/bhyve_parse_command.c
+++ b/src/bhyve/bhyve_parse_command.c
@@ -150,8 +150,10 @@ bhyveCommandLineToArgv(const char *nativeConfig,
         start = curr;
         next = strchr(curr, '\n');
 
-        if (VIR_STRNDUP(line, curr, next ? next - curr : -1) < 0)
-            goto error;
+        if (next)
+            line = g_strndup(curr, next - curr);
+        else
+            line = g_strdup(curr);
 
         if (VIR_RESIZE_N(lines, lines_alloc, line_count, 2) < 0) {
             VIR_FREE(line);
@@ -194,8 +196,10 @@ bhyveCommandLineToArgv(const char *nativeConfig,
                 next = strchr(start, ' ');
             }
 
-            if (VIR_STRNDUP(arg, curr, next ? next - curr : -1) < 0)
-                goto error;
+            if (next)
+                arg = g_strndup(curr, next - curr);
+            else
+                arg = g_strdup(curr);
 
             if (next && (*next == '\'' || *next == '"'))
                 next++;
@@ -366,8 +370,10 @@ bhyveParsePCISlot(const char *slotdef,
 
        next = strchr(curr, ':');
 
-       if (VIR_STRNDUP(val, curr, next? next - curr : -1) < 0)
-           goto error;
+       if (next)
+           val = g_strndup(curr, next - curr);
+       else
+           val = g_strdup(curr);
 
        if (virStrToLong_ui(val, NULL, 10, &values[i]) < 0)
            goto error;
@@ -441,9 +447,10 @@ bhyveParsePCIDisk(virDomainDefPtr def,
         goto error;
 
     separator = strchr(config, ',');
-    if (VIR_STRNDUP(disk->src->path, config,
-                    separator? separator - config : -1) < 0)
-        goto error;
+    if (separator)
+        disk->src->path = g_strndup(config, separator - config);
+    else
+        disk->src->path = g_strdup(config);
 
     if (bus == VIR_DOMAIN_DISK_BUS_VIRTIO) {
         idx = *nvirtiodisk;
@@ -515,9 +522,10 @@ bhyveParsePCINet(virDomainDefPtr def,
     }
 
     separator = strchr(config, ',');
-    if (VIR_STRNDUP(net->ifname, config,
-                    separator? separator - config : -1) < 0)
-        goto error;
+    if (separator)
+        net->ifname = g_strndup(config, separator - config);
+    else
+        net->ifname = g_strdup(config);
 
     if (!separator)
         goto cleanup;
@@ -578,8 +586,10 @@ bhyveParseBhyvePCIArg(virDomainDefPtr def,
     if (conf)
         conf++; /* Skip initial comma */
 
-    if (VIR_STRNDUP(emulation, separator, conf? conf - separator - 1 : -1) < 0)
-        goto error;
+    if (conf)
+        emulation = g_strndup(separator, conf - separator - 1);
+    else
+        emulation = g_strdup(separator);
 
     if (bhyveParsePCISlot(slotdef, &pcislot, &bus, &function) < 0)
         goto error;
diff --git a/src/libxl/xen_common.c b/src/libxl/xen_common.c
index c31a5c952e..a4a9ec59bf 100644
--- a/src/libxl/xen_common.c
+++ b/src/libxl/xen_common.c
@@ -823,9 +823,11 @@ xenParseSxprChar(const char *value,
 
         offset2 = strchr(offset, ',');
         offset++;
-        if (VIR_STRNDUP(def->source->data.tcp.service, offset,
-                        offset2 ? offset2 - offset : -1) < 0)
-            goto error;
+        if (offset2)
+            def->source->data.tcp.service = g_strndup(offset,
+                                                      offset2 - offset);
+        else
+            def->source->data.tcp.service = g_strdup(offset);
 
         if (offset2 && strstr(offset2, ",server"))
             def->source->data.tcp.listen = true;
@@ -875,9 +877,10 @@ xenParseSxprChar(const char *value,
     case VIR_DOMAIN_CHR_TYPE_UNIX:
     {
         const char *offset = strchr(value, ',');
-        if (VIR_STRNDUP(def->source->data.nix.path, value,
-                        offset ? offset - value : -1) < 0)
-            goto error;
+        if (offset)
+            def->source->data.nix.path = g_strndup(value, offset - value);
+        else
+            def->source->data.nix.path = g_strdup(value);
 
         if (offset != NULL &&
             strstr(offset, ",server") != NULL)
diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c
index 451a45f590..503f49a902 100644
--- a/src/remote/remote_driver.c
+++ b/src/remote/remote_driver.c
@@ -215,8 +215,10 @@ static int remoteSplitURIScheme(virURIPtr uri,
 
     *driver = *transport = NULL;
 
-    if (VIR_STRNDUP(*driver, uri->scheme, p ? p - uri->scheme : -1) < 0)
-        return -1;
+    if (p)
+        *driver = g_strndup(uri->scheme, p - uri->scheme);
+    else
+        *driver = g_strdup(uri->scheme);
 
     if (p) {
         *transport = g_strdup(p + 1);
-- 
2.21.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 2/9] Remove VIR_STRNDUP usage that passes -1
Posted by Daniel Henrique Barboza 6 years, 2 months ago

On 11/12/19 2:02 PM, Ján Tomko wrote:
> Replace all the usage of
>    VIR_STRNDUP(dest, b, p ? p - b : -1)
> with separate calls to g_strndup/g_strdup.
> 
> Signed-off-by: Ján Tomko <jtomko@redhat.com>
> ---
>   src/bhyve/bhyve_parse_command.c | 38 +++++++++++++++++++++------------
>   src/libxl/xen_common.c          | 15 +++++++------
>   src/remote/remote_driver.c      |  6 ++++--
>   3 files changed, 37 insertions(+), 22 deletions(-)
> 
> diff --git a/src/bhyve/bhyve_parse_command.c b/src/bhyve/bhyve_parse_command.c
> index fa3b881f21..5c5dfae4a9 100644
> --- a/src/bhyve/bhyve_parse_command.c
> +++ b/src/bhyve/bhyve_parse_command.c
> @@ -150,8 +150,10 @@ bhyveCommandLineToArgv(const char *nativeConfig,
>           start = curr;
>           next = strchr(curr, '\n');
>   
> -        if (VIR_STRNDUP(line, curr, next ? next - curr : -1) < 0)
> -            goto error;
> +        if (next)
> +            line = g_strndup(curr, next - curr);
> +        else
> +            line = g_strdup(curr);
>   
>           if (VIR_RESIZE_N(lines, lines_alloc, line_count, 2) < 0) {
>               VIR_FREE(line);
> @@ -194,8 +196,10 @@ bhyveCommandLineToArgv(const char *nativeConfig,
>                   next = strchr(start, ' ');
>               }
>   
> -            if (VIR_STRNDUP(arg, curr, next ? next - curr : -1) < 0)
> -                goto error;
> +            if (next)
> +                arg = g_strndup(curr, next - curr);
> +            else
> +                arg = g_strdup(curr);
>   
>               if (next && (*next == '\'' || *next == '"'))
>                   next++;
> @@ -366,8 +370,10 @@ bhyveParsePCISlot(const char *slotdef,
>   
>          next = strchr(curr, ':');
>   
> -       if (VIR_STRNDUP(val, curr, next? next - curr : -1) < 0)
> -           goto error;
> +       if (next)
> +           val = g_strndup(curr, next - curr);
> +       else
> +           val = g_strdup(curr);
>   
>          if (virStrToLong_ui(val, NULL, 10, &values[i]) < 0)
>              goto error;
> @@ -441,9 +447,10 @@ bhyveParsePCIDisk(virDomainDefPtr def,
>           goto error;
>   
>       separator = strchr(config, ',');
> -    if (VIR_STRNDUP(disk->src->path, config,
> -                    separator? separator - config : -1) < 0)
> -        goto error;
> +    if (separator)
> +        disk->src->path = g_strndup(config, separator - config);
> +    else
> +        disk->src->path = g_strdup(config);
>   
>       if (bus == VIR_DOMAIN_DISK_BUS_VIRTIO) {
>           idx = *nvirtiodisk;
> @@ -515,9 +522,10 @@ bhyveParsePCINet(virDomainDefPtr def,
>       }
>   
>       separator = strchr(config, ',');
> -    if (VIR_STRNDUP(net->ifname, config,
> -                    separator? separator - config : -1) < 0)
> -        goto error;
> +    if (separator)
> +        net->ifname = g_strndup(config, separator - config);
> +    else
> +        net->ifname = g_strdup(config);
>   
>       if (!separator)
>           goto cleanup;
> @@ -578,8 +586,10 @@ bhyveParseBhyvePCIArg(virDomainDefPtr def,
>       if (conf)
>           conf++; /* Skip initial comma */
>   
> -    if (VIR_STRNDUP(emulation, separator, conf? conf - separator - 1 : -1) < 0)
> -        goto error;
> +    if (conf)
> +        emulation = g_strndup(separator, conf - separator - 1);
> +    else
> +        emulation = g_strdup(separator);



Nit: there's an 'if (conf)' right before the VIR_STRNDUP() call you're replacing.
You can get a ride in it to insert the g_strndup call instead of adding a new
'if (conf)':

         if (conf) {
             conf++; /* Skip initial comma */
             emulation = g_strndup(separator, conf - separator - 1);
         } else {
             emulation = g_strdup(separator);
         }


Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>


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