Coverity: fix memory leak in guestfish.
[libguestfs.git] / daemon / devsparts.c
index 1781def..52a6d30 100644 (file)
@@ -89,6 +89,7 @@ foreach_block_device (block_dev_func_t func)
   if(0 != errno) {
     reply_with_perror ("readdir: /sys/block");
     free_stringslen(r, size);
+    closedir (dir);
     return NULL;
   }
 
@@ -105,8 +106,9 @@ foreach_block_device (block_dev_func_t func)
     return NULL;
   }
 
-  /* Sort the devices */
-  sort_strings (r, size);
+  /* Sort the devices.  Note that r might be NULL if there are no devices. */
+  if (r != NULL)
+    sort_strings (r, size);
 
   /* NULL terminate the list */
   if (add_string (&r, &size, &alloc, NULL) == -1) {
@@ -174,6 +176,7 @@ add_partitions(const char *device,
   if(0 != errno) {
       reply_with_perror ("readdir: %s", devdir);
       free_stringslen(*r, *size);
+      closedir (dir);
       return -1;
   }