Re: [PATCH 01/23] util: Introduce virStringListCopy

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On 10/04/2017 10:58 AM, Jiri Denemark wrote:
> The API makes a deep copy of a NULL-terminated string list.
> 
> Signed-off-by: Jiri Denemark <jdenemar@xxxxxxxxxx>
> ---
>  src/util/virstring.c | 37 +++++++++++++++++++++++++++++++++++++
>  src/util/virstring.h |  3 +++
>  2 files changed, 40 insertions(+)
> 
> diff --git a/src/util/virstring.c b/src/util/virstring.c
> index 0288d1e677..820b282ac5 100644
> --- a/src/util/virstring.c
> +++ b/src/util/virstring.c
> @@ -239,6 +239,43 @@ virStringListRemove(char ***strings,
>  }
>  
>  
> +/**
> + * virStringListCopy:
> + * @dst: where to store the copy of @strings
> + * @src: a NULL-terminated array of strings
> + *
> + * Makes a deep copy of the @src string list and stores it in @dst. Callers
> + * are responsible for freeing both @dst and @src.
> + *
> + * Returns 0 on success, -1 on error.
> + */
> +int
> +virStringListCopy(char ***dst,
> +                  const char **src)
> +{

I think it would make more sense to have this return @copy (or call it
@dst, doesn't matter) rather than 0, -1 which only means @dst wasn't
populated.  There's only 1 consumer (in patch 2)...

w/ the return of char** similar to other vir*Copy's and even
virStringListAdd...

Reviewed-by: John Ferlan <jferlan@xxxxxxxxxx>

John

Ironically virStringListAdd does essentially the same thing if you pass
@item==NULL since VIR_STRDUP(x, NULL) just sets x to NULL...  Of course
the other difference is that @dst in the *Add case could be an array of
NULL string entries.

> +    char **copy = NULL;
> +    size_t i;
> +
> +    if (!src)
> +        return 0;
> +
> +    if (VIR_ALLOC_N(copy, virStringListLength(src) + 1) < 0)> +        goto error;
> +
> +    for (i = 0; src[i]; i++) {
> +        if (VIR_STRDUP(copy[i], src[i]) < 0)
> +            goto error;
> +    }
> +
> +    *dst = copy;
> +    return 0;
> +
> + error:
> +    virStringListFree(copy);
> +    return -1;
> +}
> +
> +
>  /**
>   * virStringListFree:
>   * @str_array: a NULL-terminated array of strings to free
> diff --git a/src/util/virstring.h b/src/util/virstring.h
> index 1290fcce15..cfd91be314 100644
> --- a/src/util/virstring.h
> +++ b/src/util/virstring.h
> @@ -46,6 +46,9 @@ char **virStringListAdd(const char **strings,
>  void virStringListRemove(char ***strings,
>                           const char *item);
>  
> +int virStringListCopy(char ***dst,
> +                      const char **src);
> +
>  void virStringListFree(char **strings);
>  void virStringListFreeCount(char **strings,
>                              size_t count);
> 

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



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]
  Powered by Linux