From 6edee06e6b2a2eab5447e1bb9396afe839995740 Mon Sep 17 00:00:00 2001
From: Mark Reynolds <mreynolds@redhat.com>
Date: Tue, 11 Mar 2014 16:44:34 -0400
Subject: [PATCH] Ticket 47740 - Coverity Fixes (Mark - part 1)

Fixed these issues:

12510 - resource leak in lib/libutil/certmap.c
12509 - resource leak in ldap/servers/plugins/chainingdb/cb_bind.c
12508 - resource leak in ldap/servers/plugins/replication/repl5_replica_config.c
12507 - resource leak in ldap/servers/plugins/replication/repl5_replica_config.c
12506 - resource leak in ldap/servers/plugins/chainingdb/cb_config.c
12505 - resource leak in ldap/servers/plugins/posix-winsync/posix-winsync.c
12500 - resource leak in ldap/servers/plugins/chainingdb/cb_instance.c

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

Reviewed by: ?
---
 ldap/servers/plugins/chainingdb/cb_bind.c          |    5 +-
 ldap/servers/plugins/chainingdb/cb_config.c        |   70 +++++++++-----------
 ldap/servers/plugins/chainingdb/cb_instance.c      |   61 ++++++++---------
 ldap/servers/plugins/posix-winsync/posix-winsync.c |    9 ++-
 .../plugins/replication/repl5_replica_config.c     |    2 +
 lib/ldaputil/certmap.c                             |    5 +-
 6 files changed, 75 insertions(+), 77 deletions(-)

diff --git a/ldap/servers/plugins/chainingdb/cb_bind.c b/ldap/servers/plugins/chainingdb/cb_bind.c
index 18e526a..7c22931 100644
--- a/ldap/servers/plugins/chainingdb/cb_bind.c
+++ b/ldap/servers/plugins/chainingdb/cb_bind.c
@@ -307,6 +307,7 @@ chainingdb_bind( Slapi_PBlock *pb )
 		rc = status;
 		allocated_errmsg = 1;
 	} else if ( LDAP_USER_CANCELLED != rc ) {
+		slapi_ch_free_string(&errmsg);
 		errmsg = ldap_err2string( rc );
 		if (rc == LDAP_TIMEOUT) {
 			cb_ping_farm(cb,NULL,0);
@@ -332,8 +333,8 @@ chainingdb_bind( Slapi_PBlock *pb )
 		ldap_controls_free( resctrls );
 	}
 	slapi_ch_free((void **)& matcheddn );
-	if ( allocated_errmsg && errmsg != NULL ) {
-		slapi_ch_free((void **)& errmsg );
+	if ( allocated_errmsg ) {
+		slapi_ch_free_string(&errmsg);
 	}
 
 	slapi_sdn_free(&mysdn);
diff --git a/ldap/servers/plugins/chainingdb/cb_config.c b/ldap/servers/plugins/chainingdb/cb_config.c
index 717ea3c..d05470c 100644
--- a/ldap/servers/plugins/chainingdb/cb_config.c
+++ b/ldap/servers/plugins/chainingdb/cb_config.c
@@ -487,45 +487,39 @@ cb_config_modify_callback(Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entr
 
                         slapi_rwlock_unlock(cb->config.rwl_config_lock);
 		} else
-		if ( !strcasecmp ( attr_name, CB_CONFIG_GLOBAL_CHAINABLE_COMPONENTS )) {
-                        char * config_attr_value;
-                        int done=0;
-
-                        slapi_rwlock_wrlock(cb->config.rwl_config_lock);
-
-                        for (j = 0; mods[i]->mod_bvalues && mods[i]->mod_bvalues[j]; j++) {
-                                config_attr_value = (char *) mods[i]->mod_bvalues[j]->bv_val;
-                                if (SLAPI_IS_MOD_REPLACE(mods[i]->mod_op)) {
-                                        if (!done) {
-                                                charray_free(cb->config.chainable_components);
-                                                cb->config.chainable_components=NULL;
-                                                done=1;
-                                        }
-                                        charray_add(&cb->config.chainable_components,
-                                                slapi_dn_normalize(slapi_ch_strdup(config_attr_value)
-));
-                                } else
-                                if (SLAPI_IS_MOD_ADD(mods[i]->mod_op)) {
-                                        charray_add(&cb->config.chainable_components,
-                                                slapi_dn_normalize(slapi_ch_strdup(config_attr_value)
-));
-                                } else
-                                if (SLAPI_IS_MOD_DELETE(mods[i]->mod_op)) {
-                                        charray_remove(cb->config.chainable_components,
-                                                slapi_dn_normalize(slapi_ch_strdup(config_attr_value)
-),
-												0 /* freeit */);
-                                }
-                        }
-                        if (NULL == mods[i]->mod_bvalues) {
-                                charray_free(cb->config.chainable_components);
-                                cb->config.chainable_components=NULL;
-                        }
-
-                        slapi_rwlock_unlock(cb->config.rwl_config_lock);
-                }
-
+			if ( !strcasecmp ( attr_name, CB_CONFIG_GLOBAL_CHAINABLE_COMPONENTS )) {
+				char *config_attr_value;
+				char *attr_val;
+				int done=0;
+
+				slapi_rwlock_wrlock(cb->config.rwl_config_lock);
+
+				for (j = 0; mods[i]->mod_bvalues && mods[i]->mod_bvalues[j]; j++) {
+					config_attr_value = (char *) mods[i]->mod_bvalues[j]->bv_val;
+					if (SLAPI_IS_MOD_REPLACE(mods[i]->mod_op)) {
+						if (!done) {
+							charray_free(cb->config.chainable_components);
+							cb->config.chainable_components=NULL;
+							done=1;
+						}
+						charray_add(&cb->config.chainable_components,
+						slapi_dn_normalize(slapi_ch_strdup(config_attr_value)));
+					} else if (SLAPI_IS_MOD_ADD(mods[i]->mod_op)) {
+						charray_add(&cb->config.chainable_components,
+						slapi_dn_normalize(slapi_ch_strdup(config_attr_value)));
+					} else if (SLAPI_IS_MOD_DELETE(mods[i]->mod_op)) {
+						attr_val = slapi_dn_normalize(slapi_ch_strdup(config_attr_value));
+						charray_remove(cb->config.chainable_components, attr_val, 0 /* freeit */);
+						slapi_ch_free_string(&attr_val);
+					}
+				}
+				if (NULL == mods[i]->mod_bvalues) {
+					charray_free(cb->config.chainable_components);
+					cb->config.chainable_components=NULL;
+				}
 
+				slapi_rwlock_unlock(cb->config.rwl_config_lock);
+		}
 	}
 	*returncode=LDAP_SUCCESS;
 	return SLAPI_DSE_CALLBACK_OK;
diff --git a/ldap/servers/plugins/chainingdb/cb_instance.c b/ldap/servers/plugins/chainingdb/cb_instance.c
index 95781b5..ce7675b 100644
--- a/ldap/servers/plugins/chainingdb/cb_instance.c
+++ b/ldap/servers/plugins/chainingdb/cb_instance.c
@@ -417,43 +417,40 @@ int cb_instance_modify_config_callback(Slapi_PBlock *pb, Slapi_Entry* entryBefor
                         continue;
 		} 
 		if ( !strcasecmp ( attr_name, CB_CONFIG_CHAINING_COMPONENTS )) {
-               		char * config_attr_value;
-                       	int done=0;
+			char *config_attr_value;
+			char *attr_val;
+			int done=0;
 			int j;
 
-        		slapi_rwlock_wrlock(inst->rwl_config_lock);
-                       	for (j = 0; mods[i]->mod_bvalues && mods[i]->mod_bvalues[j]; j++) {
-                               	config_attr_value = (char *) mods[i]->mod_bvalues[j]->bv_val;
-                               	if (SLAPI_IS_MOD_REPLACE(mods[i]->mod_op)) {
-                                       	if (!done) {
-                                               	charray_free(inst->chaining_components);
-                                               	inst->chaining_components=NULL;
-                                               	done=1;
-                                       	}
+			slapi_rwlock_wrlock(inst->rwl_config_lock);
+			for (j = 0; mods[i]->mod_bvalues && mods[i]->mod_bvalues[j]; j++) {
+				config_attr_value = (char *) mods[i]->mod_bvalues[j]->bv_val;
+				if (SLAPI_IS_MOD_REPLACE(mods[i]->mod_op)) {
+					if (!done) {
+						charray_free(inst->chaining_components);
+						inst->chaining_components=NULL;
+						done=1;
+					}
 					/* XXXSD assume dns */
-                                       	charray_add(&inst->chaining_components,
-                                               	slapi_dn_normalize(slapi_ch_strdup(config_attr_value)));
-                               	} else
-                               	if (SLAPI_IS_MOD_ADD(mods[i]->mod_op)) {
-                                       	charray_add(&inst->chaining_components,
-                                               	slapi_dn_normalize(slapi_ch_strdup(config_attr_value)));
-                               	} else
-                               	if (SLAPI_IS_MOD_DELETE(mods[i]->mod_op)) {
-                                       	charray_remove(inst->chaining_components,
-                                               	slapi_dn_normalize(slapi_ch_strdup(config_attr_value)),
-												0 /* freeit */);
-                               	}
-                       	}
-                       	if (NULL == mods[i]->mod_bvalues) {
-                               	charray_free(inst->chaining_components);
-                               	inst->chaining_components=NULL;
-                       	}
-        		slapi_rwlock_unlock(inst->rwl_config_lock);
-                        continue;
+					charray_add(&inst->chaining_components,
+									slapi_dn_normalize(slapi_ch_strdup(config_attr_value)));
+				} else if (SLAPI_IS_MOD_ADD(mods[i]->mod_op)) {
+					charray_add(&inst->chaining_components,
+								slapi_dn_normalize(slapi_ch_strdup(config_attr_value)));
+				} else if (SLAPI_IS_MOD_DELETE(mods[i]->mod_op)) {
+					attr_val = slapi_dn_normalize(slapi_ch_strdup(config_attr_value));
+					charray_remove(inst->chaining_components,attr_val, 0 /* freeit */);
+					slapi_ch_free_string(&attr_val);
+				}
+			}
+			if (NULL == mods[i]->mod_bvalues) {
+				charray_free(inst->chaining_components);
+				inst->chaining_components=NULL;
+			}
+			slapi_rwlock_unlock(inst->rwl_config_lock);
+			continue;
 		} 
 
-
-
 		if (SLAPI_IS_MOD_DELETE(mods[i]->mod_op) || 
                         SLAPI_IS_MOD_ADD(mods[i]->mod_op)) {
 
diff --git a/ldap/servers/plugins/posix-winsync/posix-winsync.c b/ldap/servers/plugins/posix-winsync/posix-winsync.c
index 45a6b80..129bdac 100644
--- a/ldap/servers/plugins/posix-winsync/posix-winsync.c
+++ b/ldap/servers/plugins/posix-winsync/posix-winsync.c
@@ -1389,21 +1389,22 @@ posix_winsync_end_update_cb(void *cbdata, const Slapi_DN *ds_subtree, const Slap
         int rc = 0;
         char *dn = slapi_create_dn_string("cn=%s,cn=%s,cn=tasks,cn=config",
                                           posix_winsync_plugin_name, MEMBEROFTASK);
-        slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
-                        "--> posix_winsync_end_update_cb, create task %s\n", dn);
+
         if (NULL == dn) {
             slapi_pblock_destroy(pb);
+            slapi_entry_free(e_task);
             slapi_log_error(SLAPI_LOG_FATAL, posix_winsync_plugin_name,
                             "posix_winsync_end_update_cb: "
                                 "failed to create task dn: cn=%s,%s,cn=tasks,cn=config\n",
                             posix_winsync_plugin_name, MEMBEROFTASK);
             return;
         }
-
+        slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
+                        "--> posix_winsync_end_update_cb, create task %s\n", dn);
         slapi_log_error(SLAPI_LOG_PLUGIN, posix_winsync_plugin_name,
                         "--> posix_winsync_end_update_cb, init'ing task\n");
 
-        slapi_entry_init(e_task, slapi_ch_strdup(dn), NULL);
+        slapi_entry_init(e_task, dn, NULL);
         slapi_entry_add_string(e_task, "cn", slapi_ch_strdup(posix_winsync_plugin_name));
         slapi_entry_add_string(e_task, "objectClass", "extensibleObject");
         slapi_entry_add_string(e_task, "basedn", slapi_sdn_get_dn(ds_subtree));
diff --git a/ldap/servers/plugins/replication/repl5_replica_config.c b/ldap/servers/plugins/replication/repl5_replica_config.c
index ae4a7d8..6374b81 100644
--- a/ldap/servers/plugins/replication/repl5_replica_config.c
+++ b/ldap/servers/plugins/replication/repl5_replica_config.c
@@ -444,10 +444,12 @@ replica_config_modify (Slapi_PBlock *pb, Slapi_Entry* entryBefore, Slapi_Entry*
                 }
                 else if (strcasecmp (config_attr, attr_replicaType) == 0)
 			    {
+                	slapi_ch_free_string(&new_repl_type);
 					new_repl_type = slapi_ch_strdup(config_attr_value);
                 }
                 else if (strcasecmp (config_attr, attr_replicaId) == 0)
 			    {
+                	slapi_ch_free_string(&new_repl_id);
 					new_repl_id = slapi_ch_strdup(config_attr_value);
                 }
                 else if (strcasecmp (config_attr, attr_flags) == 0)
diff --git a/lib/ldaputil/certmap.c b/lib/ldaputil/certmap.c
index f3da425..e27633c 100644
--- a/lib/ldaputil/certmap.c
+++ b/lib/ldaputil/certmap.c
@@ -745,7 +745,10 @@ static int ldapu_cert_searchfn_default (void *cert, LDAP *ld,
 	len = strlen(certmap_info->searchAttr) + strlen(subjectDN) +
 	    strlen("=") + 1;
 	certFilter = (char *)ldapu_malloc(len * sizeof(char));
-	if (!certFilter) return LDAPU_ERR_OUT_OF_MEMORY;
+	if (!certFilter){
+		free(subjectDN);
+		return LDAPU_ERR_OUT_OF_MEMORY;
+	}
 	sprintf(certFilter, "%s=%s", certmap_info->searchAttr, subjectDN);
 	free(subjectDN);
 	if ( ldapu_strcasecmp(basedn, "") ) {
-- 
1.7.1

