Browse Source

Bug 617630 - fix coverify Defect Type: Resource leaks issues CID 12052 - 12093

https://bugzilla.redhat.com/show_bug.cgi?id=617630
Resolves: bug 617630
Bug description: fix coverify Defect Type: Resource leaks issues CID 12086, 12091, 12092.
description: The vlv_build_idl() has been modified to release idl when an error occurs. The "if (idl)" near the end of the function is unnecessary because at that point idl cannot be NULL. The "if (candidate)" has been inverted to catch NULL pointer.

The vlv_filter_candidates() and the vlv_trim_candidates() have been modified to check for NULL filteredCandidates in the beginning to avoid resource leaks.
Endi S. Dewata 15 years ago
parent
commit
458ef0f1c2
1 changed files with 27 additions and 20 deletions
  1. 27 20
      ldap/servers/slapd/back-ldbm/vlv.c

+ 27 - 20
ldap/servers/slapd/back-ldbm/vlv.c

@@ -1034,11 +1034,13 @@ int vlv_build_idl(PRUint32 start, PRUint32 stop, DB *db, DBC *dbc,
     DBT key = {0};
     DBT key = {0};
     DBT data = {0};
     DBT data = {0};
     ID id;
     ID id;
+    int rc = LDAP_SUCCESS;
 
 
     idl = idl_alloc(stop-start+1);
     idl = idl_alloc(stop-start+1);
     if (!idl) {
     if (!idl) {
         /* out of memory :( */
         /* out of memory :( */
-        return LDAP_OPERATIONS_ERROR;
+        rc = LDAP_OPERATIONS_ERROR;
+        goto error;
     }
     }
     recno = start+1;
     recno = start+1;
     key.size = sizeof(recno);
     key.size = sizeof(recno);
@@ -1063,24 +1065,30 @@ int vlv_build_idl(PRUint32 start, PRUint32 stop, DB *db, DBC *dbc,
         if (err == ENOMEM)
         if (err == ENOMEM)
             LDAPDebug(LDAP_DEBUG_ANY, "   nomem: wants %d key, %d data\n",
             LDAPDebug(LDAP_DEBUG_ANY, "   nomem: wants %d key, %d data\n",
                       key.size, data.size, 0);
                       key.size, data.size, 0);
-        return LDAP_OPERATIONS_ERROR;
+        rc = LDAP_OPERATIONS_ERROR;
+        goto error;
+    }
+
+    if (!candidates)
+    {
+        goto error;
     }
     }
 
 
     /* success! */
     /* success! */
-    if (idl) {
-        if (candidates)
-        {
-            if (dosort)
-            {
-                qsort((void *)&idl->b_ids[0], idl->b_nids,
-                      (size_t)sizeof(ID), vlv_idl_sort_cmp);
-            }
-            *candidates = idl;
-        }
-        else
-            idl_free(idl);        /* ??? */
+    if (dosort)
+    {
+        qsort((void *)&idl->b_ids[0], idl->b_nids,
+              (size_t)sizeof(ID), vlv_idl_sort_cmp);
     }
     }
-    return LDAP_SUCCESS;
+    *candidates = idl;
+
+    goto done;
+
+error:
+    if (idl) idl_free(idl);
+
+done:
+    return rc;
 }
 }
 
 
 
 
@@ -1256,7 +1264,7 @@ vlv_filter_candidates(backend *be, Slapi_PBlock *pb, const IDList *candidates, c
 	int return_value = LDAP_SUCCESS;
 	int return_value = LDAP_SUCCESS;
 
 
 	/* Refuse to filter a non-existent IDlist */
 	/* Refuse to filter a non-existent IDlist */
-	if (NULL == candidates)
+	if (NULL == candidates || NULL == filteredCandidates)
 	{
 	{
 		return LDAP_UNWILLING_TO_PERFORM;
 		return LDAP_UNWILLING_TO_PERFORM;
 	}
 	}
@@ -1330,7 +1338,7 @@ vlv_filter_candidates(backend *be, Slapi_PBlock *pb, const IDList *candidates, c
         	}
         	}
     	} while (!done && id!=NOID);
     	} while (!done && id!=NOID);
 	}
 	}
-    if(filteredCandidates!=NULL)
+
         *filteredCandidates= resultIdl;
         *filteredCandidates= resultIdl;
 	LDAPDebug( LDAP_DEBUG_TRACE, "<= vlv_filter_candidates: Filtering done\n",0, 0, 0 );
 	LDAPDebug( LDAP_DEBUG_TRACE, "<= vlv_filter_candidates: Filtering done\n",0, 0, 0 );
 
 
@@ -1358,7 +1366,7 @@ vlv_trim_candidates(backend *be, const IDList *candidates, const sort_spec* sort
     int do_trim= 1;
     int do_trim= 1;
 
 
 	/* Refuse to trim a non-existent IDlist */
 	/* Refuse to trim a non-existent IDlist */
-	if (NULL == candidates || candidates->b_nids==0)
+	if (NULL == candidates || candidates->b_nids==0 || NULL == trimmedCandidates)
 	{
 	{
 		return LDAP_UNWILLING_TO_PERFORM;
 		return LDAP_UNWILLING_TO_PERFORM;
 	}
 	}
@@ -1410,8 +1418,7 @@ vlv_trim_candidates(backend *be, const IDList *candidates, const sort_spec* sort
         }
         }
     }
     }
    	LDAPDebug( LDAP_DEBUG_TRACE, "<= vlv_trim_candidates: Trimmed list contains %lu entries.\n",(u_long)resultIdl->b_nids, 0, 0 );
    	LDAPDebug( LDAP_DEBUG_TRACE, "<= vlv_trim_candidates: Trimmed list contains %lu entries.\n",(u_long)resultIdl->b_nids, 0, 0 );
-    if(trimmedCandidates!=NULL)
-        *trimmedCandidates= resultIdl;
+    *trimmedCandidates= resultIdl;
     return return_value;
     return return_value;
 }
 }