[Libguestfs] [PATCH] daemon: free the string on stringsbuf add failure

Richard W.M. Jones rjones at redhat.com
Wed Sep 10 16:32:37 UTC 2014


On Wed, Sep 10, 2014 at 01:13:03PM +0200, Pino Toscano wrote:
> If add_string_nodup fails free the passed string instead of leaking it,
> as that string would have been owned by the stringbuf.
> 
> Adapt devsparts/list-disk-labels to this behaviour.

ACK, but would be good to check over the other calls to
add_string_nodup just to make sure that no changes are required.

Rich.

> ---
>  daemon/devsparts.c | 8 ++++----
>  daemon/guestfsd.c  | 1 +
>  2 files changed, 5 insertions(+), 4 deletions(-)
> 
> diff --git a/daemon/devsparts.c b/daemon/devsparts.c
> index 70d4ba5..d71a3b3 100644
> --- a/daemon/devsparts.c
> +++ b/daemon/devsparts.c
> @@ -312,7 +312,6 @@ do_list_disk_labels (void)
>  {
>    DIR *dir = NULL;
>    struct dirent *d;
> -  char *rawdev = NULL;
>    DECLARE_STRINGSBUF (ret);
>  
>    dir = opendir (GUESTFSDIR);
> @@ -324,6 +323,7 @@ do_list_disk_labels (void)
>    errno = 0;
>    while ((d = readdir (dir)) != NULL) {
>      CLEANUP_FREE char *path = NULL;
> +    char *rawdev = NULL;
>  
>      if (d->d_name[0] == '.')
>        continue;
> @@ -341,12 +341,13 @@ do_list_disk_labels (void)
>        goto error;
>      }
>  
> -    if (add_string (&ret, d->d_name) == -1)
> +    if (add_string (&ret, d->d_name) == -1) {
> +      free (rawdev);
>        goto error;
> +    }
>  
>      if (add_string_nodup (&ret, rawdev) == -1)
>        goto error;
> -    rawdev = NULL;            /* buffer now owned by the stringsbuf */
>    }
>  
>    /* Check readdir didn't fail */
> @@ -374,6 +375,5 @@ do_list_disk_labels (void)
>   error:
>    if (dir)
>      closedir (dir);
> -  free (rawdev);
>    return NULL;
>  }
> diff --git a/daemon/guestfsd.c b/daemon/guestfsd.c
> index 3616f3a..4a24557 100644
> --- a/daemon/guestfsd.c
> +++ b/daemon/guestfsd.c
> @@ -532,6 +532,7 @@ add_string_nodup (struct stringsbuf *sb, char *str)
>        reply_with_perror ("realloc");
>        free_stringslen (sb->argv, sb->size);
>        sb->argv = NULL;
> +      free (str);
>        return -1;
>      }
>      sb->argv = new_argv;
> -- 
> 1.9.3
> 
> _______________________________________________
> Libguestfs mailing list
> Libguestfs at redhat.com
> https://www.redhat.com/mailman/listinfo/libguestfs

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
Fedora Windows cross-compiler. Compile Windows programs, test, and
build Windows installers. Over 100 libraries supported.
http://fedoraproject.org/wiki/MinGW




More information about the Libguestfs mailing list