If add_string_nodup fails free the passed string instead of leaking it,
as that string would have been owned by the stringbuf.
Adapt few places to this behaviour.
---
daemon/btrfs.c | 4 +---
daemon/devsparts.c | 8 ++++----
daemon/guestfsd.c | 1 +
3 files changed, 6 insertions(+), 7 deletions(-)
diff --git a/daemon/btrfs.c b/daemon/btrfs.c
index 9b52aa8..d70565a 100644
--- a/daemon/btrfs.c
+++ b/daemon/btrfs.c
@@ -1123,10 +1123,8 @@ do_btrfs_subvolume_show (const char *subvolume)
}
if (ss) {
- if (add_string_nodup (&ret, ss) == -1) {
- free (ss);
+ if (add_string_nodup (&ret, ss) == -1)
return NULL;
- }
} else {
if (add_string (&ret, "") == -1)
return NULL;
diff --git a/daemon/devsparts.c b/daemon/devsparts.c
index 7c690f8..41c728c 100644
--- a/daemon/devsparts.c
+++ b/daemon/devsparts.c
@@ -311,7 +311,6 @@ do_list_disk_labels (void)
{
DIR *dir = NULL;
struct dirent *d;
- char *rawdev = NULL;
DECLARE_STRINGSBUF (ret);
dir = opendir (GUESTFSDIR);
@@ -330,6 +329,7 @@ do_list_disk_labels (void)
errno = 0;
while ((d = readdir (dir)) != NULL) {
CLEANUP_FREE char *path = NULL;
+ char *rawdev;
if (d->d_name[0] == '.')
continue;
@@ -347,12 +347,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 */
@@ -380,6 +381,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 8b9acc7..af151bd 100644
--- a/daemon/guestfsd.c
+++ b/daemon/guestfsd.c
@@ -512,6 +512,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;
--
2.5.5