[libvirt] [PATCH] storage: Fix the memory leak

Osier Yang jyang at redhat.com
Thu Jan 23 10:23:32 UTC 2014


The return value of virGetFCHostNameByWWN is a strdup'ed string.
Also add comments to declare that the caller should take care of
freeing it.
---
 src/storage/storage_backend_scsi.c | 15 ++++++++++-----
 src/util/virutil.c                 |  5 ++++-
 2 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/src/storage/storage_backend_scsi.c b/src/storage/storage_backend_scsi.c
index fce2bae..b38e530 100644
--- a/src/storage/storage_backend_scsi.c
+++ b/src/storage/storage_backend_scsi.c
@@ -668,6 +668,8 @@ static int
 deleteVport(virStoragePoolSourceAdapter adapter)
 {
     unsigned int parent_host;
+    char *name = NULL;
+    int ret = -1;
 
     if (adapter.type != VIR_STORAGE_POOL_SOURCE_ADAPTER_TYPE_FC_HOST)
         return 0;
@@ -680,18 +682,21 @@ deleteVport(virStoragePoolSourceAdapter adapter)
     if (!adapter.data.fchost.parent)
         return 0;
 
-    if (!(virGetFCHostNameByWWN(NULL, adapter.data.fchost.wwnn,
-                                adapter.data.fchost.wwpn)))
+    if (!(name = virGetFCHostNameByWWN(NULL, adapter.data.fchost.wwnn,
+                                       adapter.data.fchost.wwpn)))
         return -1;
 
     if (getHostNumber(adapter.data.fchost.parent, &parent_host) < 0)
-        return -1;
+        goto cleanup;
 
     if (virManageVport(parent_host, adapter.data.fchost.wwpn,
                        adapter.data.fchost.wwnn, VPORT_DELETE) < 0)
-        return -1;
+        goto cleanup;
 
-    return 0;
+    ret = 0;
+cleanup:
+    VIR_FREE(name);
+    return ret;
 }
 
 
diff --git a/src/util/virutil.c b/src/util/virutil.c
index 87cc2e7..7a2fbb0 100644
--- a/src/util/virutil.c
+++ b/src/util/virutil.c
@@ -1820,7 +1820,10 @@ cleanup:
 /* virGetHostNameByWWN:
  *
  * Iterate over the sysfs tree to get FC host name (e.g. host5)
- * by wwnn,wwpn pair.
+ * by the provided "wwnn,wwpn" pair.
+ *
+ * Returns the FC host name which must be freed by the caller,
+ * or NULL on failure.
  */
 char *
 virGetFCHostNameByWWN(const char *sysfs_prefix,
-- 
1.8.1.4




More information about the libvir-list mailing list