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

Daniel Henrique Barboza danielhb413 at gmail.com
Wed Nov 13 12:05:00 UTC 2019



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 at 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 at gmail.com>





More information about the libvir-list mailing list