From 0447609b8deb1ced097d6837cba16ee2baa58aac Mon Sep 17 00:00:00 2001
From: Mark Reynolds <mreynolds@redhat.com>
Date: Wed, 12 Mar 2014 16:58:32 -0400
Subject: [PATCH] Ticket 47740 - Fix coverity erorrs - Part 4

12498 - resource leak - /ldap/servers/plugins/chainingdn/cb_instance.c
12490 - resource leak - /ldap/servers/plugins/chainingdn/cb_controls.c
12489 - resource leak - /ldap/servers/plugins/replication/repl5_replica_config.c
12488 - resource leak - /ldap/servers/slapd/main.c
12485 - resource leak - /ldap/servers/plugins/replication/repl5_replica_config.c
12484 - resource leak - /ldap/servers/plugins/replication/repl5_replica_config.c
12483 - resource leak - /ldap/servers/slapd/back-ldbm/upgrade.c
12482 - resource leak - /ldap/servers/plugins/replication/repl5_replica.c
12481 - resource leak - /ldap/servers/plugins/acl/aclplugin.c
12476 - resource leak - /ldap/servers/slapd/back-ldbm/upgrade.c
12475 - resource leak - /ldap/servers/slapd/back-ldbm/upgrade.c

https://fedorahosted.org/389/ticket/47740

Reviewed by: ?
---
 ldap/servers/plugins/acl/aclplugin.c               |    8 ++----
 ldap/servers/plugins/chainingdb/cb_controls.c      |   14 ++++++-----
 ldap/servers/plugins/chainingdb/cb_instance.c      |   18 +++++++-------
 ldap/servers/plugins/replication/repl5_replica.c   |    3 +-
 .../plugins/replication/repl5_replica_config.c     |   13 +++++++---
 ldap/servers/slapd/back-ldbm/upgrade.c             |    3 ++
 ldap/servers/slapd/main.c                          |    5 ++-
 ldap/servers/slapd/proxyauth.c                     |   24 ++++++++++----------
 8 files changed, 49 insertions(+), 39 deletions(-)

diff --git a/ldap/servers/plugins/acl/aclplugin.c b/ldap/servers/plugins/acl/aclplugin.c
index 8ae634c..a5ce149 100644
--- a/ldap/servers/plugins/acl/aclplugin.c
+++ b/ldap/servers/plugins/acl/aclplugin.c
@@ -197,7 +197,7 @@ aclplugin_preop_modify ( Slapi_PBlock *pb )
 int
 aclplugin_preop_common( Slapi_PBlock *pb )
 {
-	char		*proxy_dn;	/* id being assumed */
+	char		*proxy_dn = NULL;	/* id being assumed */
 	char		*dn;		/* proxy master */
 	char		*errtext = NULL;
 	int			lderr;
@@ -221,9 +221,7 @@ aclplugin_preop_common( Slapi_PBlock *pb )
 	 * The proxy_dn is the id being assumed, while dn
 	 * is the "proxy master".
 	*/
-	proxy_dn = NULL;
-	if ( LDAP_SUCCESS != ( lderr = proxyauth_get_dn( pb, &proxy_dn,
-			&errtext ))) {
+	if ( LDAP_SUCCESS != ( lderr = proxyauth_get_dn( pb, &proxy_dn, &errtext ))) {
 		/*
 		 * Fatal error -- send a result to the client and arrange to skip
 		 * any further processing.
@@ -231,7 +229,7 @@ aclplugin_preop_common( Slapi_PBlock *pb )
 		slapi_send_ldap_result( pb, lderr, NULL, errtext, 0, NULL );
 		TNF_PROBE_1_DEBUG(aclplugin_preop_common_end ,"ACL","",
 						tnf_string,proxid_error,"");
-
+		slapi_ch_free_string(&proxy_dn);
 		return 1;	/* skip any further processing */
 	}
 	slapi_pblock_get ( pb, SLAPI_REQUESTOR_DN, &dn );
diff --git a/ldap/servers/plugins/chainingdb/cb_controls.c b/ldap/servers/plugins/chainingdb/cb_controls.c
index 2a698a0..e1fc9c3 100644
--- a/ldap/servers/plugins/chainingdb/cb_controls.c
+++ b/ldap/servers/plugins/chainingdb/cb_controls.c
@@ -200,20 +200,22 @@ int cb_update_controls( Slapi_PBlock * pb,
                 char * requestor,*rootdn;
                 char * requestorCopy=NULL;
 
-                rootdn=cb_get_rootdn();
+                rootdn = cb_get_rootdn();
                 slapi_pblock_get( pb, SLAPI_REQUESTOR_NDN, &requestor );
                 requestorCopy = slapi_ch_strdup(requestor);
                 slapi_dn_ignore_case(requestorCopy);
 
                 if (!strcmp( requestorCopy, rootdn )) {    /* UTF8- aware */
                     slapi_log_error( SLAPI_LOG_PLUGIN,CB_PLUGIN_SUBSYSTEM,
-                    "Use of user <%s> incompatible with proxied auth. control\n",rootdn);
-                    rc=LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
-                    slapi_ch_free((void **)&requestorCopy);
+                            "Use of user <%s> incompatible with proxied auth. control\n",
+                            rootdn);
+                    rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
+                    slapi_ch_free_string(&rootdn);
+                    slapi_ch_free_string(&requestorCopy);
                     break;
                 }
-                slapi_ch_free((void **)&rootdn);
-                slapi_ch_free((void **)&requestorCopy);
+                slapi_ch_free_string(&rootdn);
+                slapi_ch_free_string(&requestorCopy);
             }
 
             addauth=0;
diff --git a/ldap/servers/plugins/chainingdb/cb_instance.c b/ldap/servers/plugins/chainingdb/cb_instance.c
index ce7675b..86bb635 100644
--- a/ldap/servers/plugins/chainingdb/cb_instance.c
+++ b/ldap/servers/plugins/chainingdb/cb_instance.c
@@ -738,11 +738,11 @@ static int cb_instance_hosturl_set(void *arg, void *value, char *errorbuf, int p
 
 	if ((LDAP_SUCCESS == rc) && apply) {
 
-               	slapi_rwlock_wrlock(inst->rwl_config_lock);
-
-	        if (( phase != CB_CONFIG_PHASE_INITIALIZATION ) &&
-       			( phase != CB_CONFIG_PHASE_STARTUP )) {
+		slapi_rwlock_wrlock(inst->rwl_config_lock);
 
+		if (( phase != CB_CONFIG_PHASE_INITIALIZATION ) &&
+			( phase != CB_CONFIG_PHASE_STARTUP ))
+		{
 			/* Dynamic modification				*/
 			/* Don't free char * pointer now		*/
 			/* STore them in a waste basket 		*/
@@ -766,17 +766,17 @@ static int cb_instance_hosturl_set(void *arg, void *value, char *errorbuf, int p
 		/* the url and update the configuration  */
 
 		if ((ludp->lud_host==NULL) || (strlen(ludp->lud_host)==0)) {
-                        inst->pool->hostname=(char *)slapi_ch_strdup((char *)get_localhost_DNS());
+			inst->pool->hostname = get_localhost_DNS();
 		} else {
-               		inst->pool->hostname = slapi_ch_strdup( ludp->lud_host );
+			inst->pool->hostname = slapi_ch_strdup( ludp->lud_host );
 		}
-               	inst->pool->url = slapi_ch_strdup( url);
-               	inst->pool->secure = secure;
+		inst->pool->url = slapi_ch_strdup( url);
+		inst->pool->secure = secure;
 
 		if ((ludp->lud_port==0) && inst->pool->secure)
 			inst->pool->port=CB_LDAP_SECURE_PORT;
 		else
-               		inst->pool->port = ludp->lud_port;
+			inst->pool->port = ludp->lud_port;
 
 		/* Build a charray of <host>:<port> */
 		/* hostname is of the form <host>[:port] <host>[:port] */
diff --git a/ldap/servers/plugins/replication/repl5_replica.c b/ldap/servers/plugins/replication/repl5_replica.c
index 3eeb322..69b4690 100644
--- a/ldap/servers/plugins/replication/repl5_replica.c
+++ b/ldap/servers/plugins/replication/repl5_replica.c
@@ -2319,6 +2319,7 @@ _replica_configure_ruv  (Replica *r, PRBool isLocked)
 								"failed to recreate replica ruv tombstone entry"
 								" (%s); LDAP error - %d\n",
 								slapi_sdn_get_dn(r->repl_root), rc);
+							slapi_ch_free_string(&generation);
 							goto done;
 						}
 					}
@@ -2326,7 +2327,7 @@ _replica_configure_ruv  (Replica *r, PRBool isLocked)
 #undef RUV_UPDATE_FULL
 				}
 
-			    slapi_ch_free((void **)&generation);
+			    slapi_ch_free_string(&generation);
 			    return_value = 0;
 		    }
             else
diff --git a/ldap/servers/plugins/replication/repl5_replica_config.c b/ldap/servers/plugins/replication/repl5_replica_config.c
index 6374b81..49bd906 100644
--- a/ldap/servers/plugins/replication/repl5_replica_config.c
+++ b/ldap/servers/plugins/replication/repl5_replica_config.c
@@ -1201,8 +1201,6 @@ static int replica_execute_cl2ldif_task (Object *r, char *returntext)
     }
 
     PR_snprintf (fName, MAXPATHLEN, "%s/%s.ldif", clDir, replica_get_name (replica));
-    slapi_ch_free_string (&clDir);
-
     slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
                     "Beginning changelog export of replica \"%s\"\n",
                     replica_get_name(replica));
@@ -1221,6 +1219,8 @@ static int replica_execute_cl2ldif_task (Object *r, char *returntext)
         rc = LDAP_OPERATIONS_ERROR;
     }
 bail:
+    slapi_ch_free_string (&clDir);
+
     return rc;
 }
 
@@ -1276,7 +1276,6 @@ static int replica_execute_ldif2cl_task (Object *r, char *returntext)
                     "Beginning changelog import of replica \"%s\"\n",
                     replica_get_name(replica));
     imprc = cl5ImportLDIF (clDir, fName, rlist);
-    slapi_ch_free_string (&clDir);
     if (CL5_SUCCESS == imprc)
     {
         slapi_log_error(SLAPI_LOG_FATAL, repl_plugin_name,
@@ -1306,8 +1305,11 @@ static int replica_execute_ldif2cl_task (Object *r, char *returntext)
             config.dir?config.dir:"null config dir");
         rc = LDAP_OPERATIONS_ERROR;
     }
+
 bail:
+    slapi_ch_free_string(&clDir);
     changelog5_config_done(&config);
+
     /* if cl5ImportLDIF returned an error, report it first. */
     return imprc?imprc:rc;
 }
@@ -2274,9 +2276,12 @@ replica_send_cleanruv_task(Repl_Agmt *agmt, cleanruv_data *clean_data)
     rc = ldap_modify_ext_s( ld, repl_dn, mods, NULL, NULL);
 
     if(rc != LDAP_SUCCESS){
+        char *hostname = agmt_get_hostname(agmt);
+
         cleanruv_log(clean_data->task, CLEANALLRUV_ID, "Failed to add CLEANRUV task (%s) to replica "
             "(%s).  You will need to manually run the CLEANRUV task on this replica (%s) error (%d)",
-            repl_dn, agmt_get_long_name(agmt), agmt_get_hostname(agmt), rc);
+            repl_dn, agmt_get_long_name(agmt), hostname, rc);
+        slapi_ch_free_string(&hostname);
     }
     slapi_ch_free_string(&repl_dn);
     slapi_sdn_free(&sdn);
diff --git a/ldap/servers/slapd/back-ldbm/upgrade.c b/ldap/servers/slapd/back-ldbm/upgrade.c
index 4f322fa..2c0d685 100644
--- a/ldap/servers/slapd/back-ldbm/upgrade.c
+++ b/ldap/servers/slapd/back-ldbm/upgrade.c
@@ -168,6 +168,7 @@ check_db_version( struct ldbminfo *li, int *action )
     *action = 0;
     dbversion_read(li, li->li_directory, &ldbmversion, &dataversion);
     if (NULL == ldbmversion || '\0' == *ldbmversion) {
+        slapi_ch_free_string(&ldbmversion);
         slapi_ch_free_string(&dataversion);
         return 0;
     }
@@ -251,6 +252,8 @@ check_db_inst_version( ldbm_instance *inst )
 
     dbversion_read(inst->inst_li, inst_dirp, &ldbmversion, &dataversion);
     if (NULL == ldbmversion || '\0' == *ldbmversion) {
+        slapi_ch_free_string(&ldbmversion);
+        slapi_ch_free_string(&dataversion);
         return rval;
     }
     
diff --git a/ldap/servers/slapd/main.c b/ldap/servers/slapd/main.c
index 0c5b2ab..dd1f88a 100644
--- a/ldap/servers/slapd/main.c
+++ b/ldap/servers/slapd/main.c
@@ -2959,7 +2959,7 @@ slapd_debug_level_string2level( const char *s )
         char    *cur, *next, *scopy;
 
         level = 0;
-        cur = scopy = slapi_ch_strdup( s );     
+        scopy = slapi_ch_strdup( s );
 
         for ( cur = scopy; cur != NULL; cur = next ) {
                 if (( next = strchr( cur, '+' )) != NULL ) {
@@ -2979,12 +2979,13 @@ slapd_debug_level_string2level( const char *s )
 
                         if ( NULL == slapd_debug_level_map[i].dle_string ) {
                                 fprintf( stderr, "Unrecognized debug level \"%s\"\n", cur );
+                                slapi_ch_free_string(&scopy);
                                 return -1;
                         }
                 }
         }
 
-        slapi_ch_free( (void **)&scopy );
+        slapi_ch_free_string(&scopy);
 
         return level;
 }
diff --git a/ldap/servers/slapd/proxyauth.c b/ldap/servers/slapd/proxyauth.c
index 817adce..58c15d8 100644
--- a/ldap/servers/slapd/proxyauth.c
+++ b/ldap/servers/slapd/proxyauth.c
@@ -183,11 +183,11 @@ parse_LDAPProxyAuth(struct berval *spec_ber, int version, char **errtextp,
 int
 proxyauth_get_dn( Slapi_PBlock *pb, char **proxydnp, char **errtextp )
 {
-  char *dn = 0;
-  LDAPProxyAuth *spec = 0;
-  int rv, lderr = LDAP_SUCCESS;	/* optimistic */
+	char *dn = 0;
+	LDAPProxyAuth *spec = 0;
+	int rv, lderr = LDAP_SUCCESS;	/* optimistic */
 
-  	BEGIN
+	BEGIN
 		struct berval *spec_ber;
 		LDAPControl **controls;
 		int present;
@@ -223,29 +223,29 @@ proxyauth_get_dn( Slapi_PBlock *pb, char **proxydnp, char **errtextp )
 			if ( critical ) {
 				lderr = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
 			} else {
-                            lderr = rv;
-                        }                         
+				lderr = rv;
+			}
 			break;
 		}
 
 		dn = slapi_ch_strdup(spec->auth_dn);
 		if (slapi_dn_isroot(dn) ) {
-                        if (critical)
-                            lderr = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
-                        else
-                            lderr = LDAP_UNWILLING_TO_PERFORM;
+			if (critical)
+				lderr = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
+			else
+				lderr = LDAP_UNWILLING_TO_PERFORM;
 			*errtextp = "Proxy dn should not be rootdn";
 			break;
 			
 		}
 	END
 
-    if (spec) delete_LDAPProxyAuth(spec);
+	if (spec) delete_LDAPProxyAuth(spec);
 
 	if ( NULL != proxydnp ) {
 		*proxydnp = dn;
 	} else {
-		slapi_ch_free( (void **)&dn );
+		slapi_ch_free_string(&dn);
 	}
 
 	return lderr;
-- 
1.7.1

