* 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>
* 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)
{
virStorageVolPtr vol;
const char *name = NULL;
+ char * StorageVolPath;
if (!vshConnectionUsability(ctl, ctl->conn))
return false;
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;
}