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

[Libguestfs] [PATCH] 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 devsparts/list-disk-labels to this behaviour.
---
 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


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