]> xenbits.xensource.com Git - libvirt.git/commitdiff
virsh: fix memory leak in cmdVolPath code
authorAlex Jia <ajia@redhat.com>
Thu, 28 Jul 2011 02:42:51 +0000 (10:42 +0800)
committerOsier Yang <jyang@redhat.com>
Thu, 28 Jul 2011 02:42:51 +0000 (10:42 +0800)
* tools/virsh.c: avoid memory leak in cmdVolPath.
* src/libvirt.c: Add doc for virStorageVolGetPath to tell one
  must free() the returned path after use.

* how to reproduce?

% dd if=/dev/zero of=/var/lib/libvirt/images/foo.img count=1 bs=10M
% virsh pool-refresh default
% valgrind -v --leak-check=full virsh vol-path --vol \
/var/lib/libvirt/images/foo.img

* actual results:

Detected in valgrind run:

==16436== 32 bytes in 1 blocks are definitely lost in loss record 7 of 22
==16436==    at 0x4A05FDE: malloc (vg_replace_malloc.c:236)
==16436==    by 0x386A314B3D: xdr_string (in /lib64/libc-2.12.so)
==16436==    by 0x3DF8CD770D: xdr_remote_nonnull_string (remote_protocol.c:3
==16436==    by 0x3DF8CD7EC8: xdr_remote_storage_vol_get_path_ret
% virsh pool-refresh default
% valgrind -v --leak-check=full virsh vol-path --vol \
/var/lib/libvirt/images/foo.img

Signed-off-by: Alex Jia <ajia@redhat.com>
src/libvirt.c
tools/virsh.c

index 988320447b2a4f119367a0fce15a71cb921ebcdf..eeaf0b61458918c8b241fb2327fe2b50c910b11b 100644 (file)
@@ -11860,7 +11860,8 @@ error:
  * pool documentation for information on getting the
  * persistent naming
  *
- * Returns the storage volume path, or NULL on error
+ * Returns the storage volume path, or NULL on error. The
+ * caller must free() the returned path after use.
  */
 char *
 virStorageVolGetPath(virStorageVolPtr vol)
index 57aded30b75a1a1faa197a8eac86b1b31293dcbc..61f69f05bce7150ed882d3f52b5ef29ae37ed92c 100644 (file)
@@ -9320,6 +9320,7 @@ cmdVolPath(vshControl *ctl, const vshCmd *cmd)
 {
     virStorageVolPtr vol;
     const char *name = NULL;
+    char * StorageVolPath;
 
     if (!vshConnectionUsability(ctl, ctl->conn))
         return false;
@@ -9328,7 +9329,13 @@ cmdVolPath(vshControl *ctl, const vshCmd *cmd)
         return false;
     }
 
-    vshPrint(ctl, "%s\n", virStorageVolGetPath(vol));
+    if ((StorageVolPath = virStorageVolGetPath(vol)) == NULL) {
+        virStorageVolFree(vol);
+        return false;
+    }
+
+    vshPrint(ctl, "%s\n", StorageVolPath);
+    VIR_FREE(StorageVolPath);
     virStorageVolFree(vol);
     return true;
 }