daemon: free the string on stringsbuf add failure

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.
This commit is contained in:
Pino Toscano
2016-07-07 15:10:37 +02:00
parent d061b67410
commit 6a06b87559
3 changed files with 6 additions and 7 deletions

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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;