[ofa-general] [PATCH 5/6] [DAPL v1] fix memory leak in error path

Patrick Marchand Latifi patrick.latifi at qlogic.com
Tue Feb 19 03:17:29 PST 2008


Make sure we don't leak the hash table if dapl_hca_alloc fails.

Signed-off-by: Patrick Marchand Latifi <patrick.latifi at qlogic.com>
---

 dapl/common/dapl_hca_util.c |   53 +++++++++++++++++++++++++++----------------
 1 files changed, 33 insertions(+), 20 deletions(-)

diff --git a/dapl/common/dapl_hca_util.c b/dapl/common/dapl_hca_util.c
index 9bf35da..2b112b3 100644
--- a/dapl/common/dapl_hca_util.c
+++ b/dapl/common/dapl_hca_util.c
@@ -65,33 +65,46 @@ dapl_hca_alloc (
     DAPL_HCA	*hca_ptr;
 
     hca_ptr = dapl_os_alloc (sizeof (DAPL_HCA));
-    if ( NULL != hca_ptr )
+    if ( NULL == hca_ptr )
     {
-	dapl_os_memzero (hca_ptr, sizeof (DAPL_HCA));
+	goto bail;
+    }
 
-        if ( DAT_SUCCESS == dapls_hash_create (
+    dapl_os_memzero (hca_ptr, sizeof (DAPL_HCA));
+
+    if ( DAT_SUCCESS != dapls_hash_create (
                  DAPL_HASH_TABLE_DEFAULT_CAPACITY, &hca_ptr->lmr_hash_table) )
-        {
-            dapl_os_lock_init(&hca_ptr->lock);
-            dapl_llist_init_head(&hca_ptr->ia_list_head);
+    {
+	goto bail;
+    }
+
+    dapl_os_lock_init(&hca_ptr->lock);
+    dapl_llist_init_head(&hca_ptr->ia_list_head);
             
-            hca_ptr->name = dapl_os_strdup(name);
-            hca_ptr->ib_hca_handle = IB_INVALID_HANDLE;
-            hca_ptr->port_num = dapl_os_strtol(port, NULL, 0);
-	    if (hca_ptr->name == NULL)
-	    {
-		dapl_os_free (hca_ptr, sizeof (DAPL_HCA));
-		hca_ptr = NULL;
-	    }
-        }
-        else
-        {
-            dapl_os_free (hca_ptr, sizeof (DAPL_HCA));
-            hca_ptr = NULL;
-        }
+    hca_ptr->name = dapl_os_strdup(name);
+    if ( NULL == hca_ptr->name )
+    {
+	goto bail;
     }
+
+    hca_ptr->ib_hca_handle = IB_INVALID_HANDLE;
+    hca_ptr->port_num = dapl_os_strtol(port, NULL, 0);
     
     return (hca_ptr);
+
+bail:
+
+    if ( NULL != hca_ptr )
+    {
+	if ( NULL != hca_ptr->lmr_hash_table )
+	{
+	    dapls_hash_free (hca_ptr->lmr_hash_table);
+	}
+
+	dapl_os_free (hca_ptr, sizeof (DAPL_HCA));
+    }
+
+    return NULL;
 }
 
 /*




More information about the general mailing list