From 4a309ffb1373f5666061c185c724e601e44a5276 Mon Sep 17 00:00:00 2001
From: Ludwig Krispenz <lkrispen@redhat.com>
Date: Fri, 9 Jun 2017 10:28:10 +0200
Subject: [PATCH 4/4] ticket 49043 - part4 - fix errors and leaks reported by
 ASAN test

---
 ldap/servers/plugins/replication/urp.c           | 38 ++++++++++++++++++++----
 ldap/servers/plugins/replication/urp_tombstone.c | 11 +++++--
 ldap/servers/slapd/slap.h                        |  4 +--
 3 files changed, 43 insertions(+), 10 deletions(-)

diff --git a/ldap/servers/plugins/replication/urp.c b/ldap/servers/plugins/replication/urp.c
index 5a33e86..e1d6626 100644
--- a/ldap/servers/plugins/replication/urp.c
+++ b/ldap/servers/plugins/replication/urp.c
@@ -761,12 +761,15 @@ urp_post_add_operation( Slapi_PBlock *pb )
 		 * move it to a valid parent if parent is also a conflict
 		 */
 		Slapi_DN *conflict_sdn = slapi_sdn_new_dn_byval(conflict_dn);
+		char *parent_dn = slapi_dn_parent(conflict_dn);
 		slapi_pblock_get( pb, SLAPI_OPERATION, &op);
 		get_repl_session_id (pb, sessionid, &opcsn);
 		slapi_log_err(SLAPI_LOG_ERR, sessionid,
 			                "urp_post_add_operation - Entry %s is conflict from tombstone, check parent\n",
 			                conflict_dn);
-		rc = tombstone_to_conflict_check_parent(sessionid, slapi_dn_parent(conflict_dn),NULL, NULL, opcsn, conflict_sdn);
+		rc = tombstone_to_conflict_check_parent(sessionid, parent_dn, NULL, NULL, opcsn, conflict_sdn);
+		slapi_sdn_free(&conflict_sdn);
+		slapi_ch_free_string(&parent_dn);
 	}
 	return rc;
 }
@@ -1389,6 +1392,7 @@ urp_add_new_entry_to_conflict (Slapi_PBlock *pb, char *sessionid, Slapi_Entry *a
 				Slapi_Value *new_v = slapi_value_new();
 				slapi_value_init_berval(new_v, &bv);
 				slapi_attr_add_value(attr, new_v);
+				slapi_value_free(&new_v);
 				slapi_entry_set_flag(addentry, SLAPI_ENTRY_LDAPSUBENTRY);
 			}
 		}
@@ -1484,8 +1488,8 @@ urp_add_check_tombstone (Slapi_PBlock *pb, char *sessionid, Slapi_Entry *entry,
 			const char *adduniqueid = slapi_entry_get_uniqueid (entries[i]);
 			const char *basedn = slapi_entry_get_dn_const (entry);
 			char *newrdn = get_rdn_plus_uniqueid ( sessionid, basedn, adduniqueid );
-			char *conflict_dn = slapi_ch_smprintf("%s,%s",newrdn,
-						slapi_dn_parent_ext(slapi_entry_get_dn_const(entries[i]),1));
+			char *parentdn = slapi_dn_parent_ext(slapi_entry_get_dn_const(entries[i]),1);
+			char *conflict_dn = slapi_ch_smprintf("%s,%s",newrdn, parentdn);
 			slapi_log_err (SLAPI_LOG_ERR, sessionid,
 				"urp_add_check_tombstone - found tombstone for newer entry(%s) create conflict (%s).\n",
 				slapi_entry_get_dn_const(entries[i]),conflict_dn);
@@ -1499,6 +1503,9 @@ urp_add_check_tombstone (Slapi_PBlock *pb, char *sessionid, Slapi_Entry *entry,
 			/* now turn the existing tombstone into a conflict */
 			tombstone_to_conflict(sessionid, slapi_entry_dup(entries[i]), conflict_sdn, "tombstoneToConflict", opcsn, NULL);
 			slapi_pblock_set(pb, SLAPI_URP_TOMBSTONE_CONFLICT_DN, conflict_dn);
+			slapi_ch_free_string(&newrdn);
+			slapi_ch_free_string(&parentdn);
+			slapi_sdn_free(&conflict_sdn);
 			rc = 2;
 		}
 		slapi_ch_free_string(&to_value);
@@ -1509,6 +1516,11 @@ urp_add_check_tombstone (Slapi_PBlock *pb, char *sessionid, Slapi_Entry *entry,
 	}
 
 done:
+	slapi_free_search_results_internal(newpb);
+	slapi_pblock_destroy(newpb);
+	if (filter) {
+		PR_smprintf_free(filter);
+	}
 	return rc;
 }
 
@@ -1520,6 +1532,7 @@ urp_delete_check_conflict (char *sessionid, Slapi_Entry *tombstone_entry, CSN *o
 	char *filter = NULL;
 	Slapi_PBlock *newpb = NULL;
 	Slapi_Entry **entries = NULL;
+	Slapi_Entry *conflict_e = NULL;
 	char *validdn = slapi_entry_attr_get_charptr(tombstone_entry, "nscpentrydn");
 	char *parent_dn = slapi_dn_parent (validdn);
 
@@ -1546,7 +1559,9 @@ urp_delete_check_conflict (char *sessionid, Slapi_Entry *tombstone_entry, CSN *o
 		goto done;
 	}
 
-	conflict_to_tombstone(sessionid, slapi_entry_dup(entries[0]), opcsn);
+	conflict_e = slapi_entry_dup(entries[0]);
+	conflict_to_tombstone(sessionid, conflict_e, opcsn);
+	slapi_entry_free(conflict_e);
 	rc = 1;
 done:
 	slapi_free_search_results_internal(newpb);
@@ -1602,6 +1617,8 @@ urp_find_valid_entry_to_delete(Slapi_PBlock *pb, const Slapi_Entry *deleteentry,
 	}
 
 done:
+	slapi_free_search_results_internal(newpb);
+	slapi_pblock_destroy(newpb);
 	return delete_nsuniqueid;
 }
 
@@ -1616,6 +1633,8 @@ urp_find_tombstone_for_glue (Slapi_PBlock *pb, char *sessionid, const Slapi_Entr
 	const char *basedn = slapi_sdn_get_dn(parentdn);
 	char *conflict_csnstr = slapi_entry_attr_get_charptr(entry, "conflictcsn");
 	CSN *conflict_csn = csn_new_by_string(conflict_csnstr);
+	slapi_ch_free_string(&conflict_csnstr);
+	CSN *tombstone_csn = NULL;
 
 	char *filter = slapi_filter_sprintf("(&(objectclass=nstombstone)(nscpentrydn=%s))", basedn);
 	newpb = slapi_pblock_new();
@@ -1639,10 +1658,10 @@ urp_find_tombstone_for_glue (Slapi_PBlock *pb, char *sessionid, const Slapi_Entr
 		goto done;
 	}
 	for (int i = 0; entries && (entries[i] != NULL); i++) {
-		CSN *tombstone_csn = NULL;
 		char *tombstone_csn_value = slapi_entry_attr_get_charptr(entries[i], "nstombstonecsn");
 		if (tombstone_csn_value) {
 			tombstone_csn = csn_new_by_string(tombstone_csn_value);
+			slapi_ch_free_string(&tombstone_csn_value);
 			if( csn_compare(tombstone_csn, conflict_csn) > 0 ) {
 				slapi_log_err (SLAPI_LOG_ERR, sessionid,
 					"urp_find_tombstone_for_glue - found tombstone newer than conflict (%s).\n",
@@ -1652,15 +1671,24 @@ urp_find_tombstone_for_glue (Slapi_PBlock *pb, char *sessionid, const Slapi_Entr
 				if (rc) {
 					slapi_log_err(SLAPI_LOG_ERR, sessionid,
 					"urp_resolve parent entry: failed to create glue from tombstone %s\n", slapi_sdn_get_dn(parentdn));
+					slapi_ch_free_string(&tombstone_nsuniqueid);
 					tombstone_nsuniqueid = NULL;
 				}
 				break;
 			}
 		}
 	}
+	csn_free(&tombstone_csn);
 
 done:
+	csn_free(&conflict_csn);
+	slapi_ch_free_string(&parent_dn);
 	slapi_pblock_set(pb, SLAPI_PLUGIN_INTOP_RESULT, &rc);
+	slapi_free_search_results_internal(newpb);
+	slapi_pblock_destroy(newpb);
+	if (filter) {
+		PR_smprintf_free(filter);
+	}
 	return tombstone_nsuniqueid;
 }
 
diff --git a/ldap/servers/plugins/replication/urp_tombstone.c b/ldap/servers/plugins/replication/urp_tombstone.c
index a41c880..f5e2f8b 100644
--- a/ldap/servers/plugins/replication/urp_tombstone.c
+++ b/ldap/servers/plugins/replication/urp_tombstone.c
@@ -68,6 +68,7 @@ get_valid_parent_for_conflict(Slapi_Entry *entry)
 				"get_valid_parent_for_conflict - valid entry dn: %s\n",
 				validdn);
 		}
+		slapi_ch_free_string(&replconflict);
 	}
 	return valid_DN;
 }
@@ -95,7 +96,7 @@ tombstone_to_conflict_check_parent (
 				"tombstone_to_conflict_check_parent - checking parent entry: %s\n",
 				parentdn);
 	}
-	newpb= slapi_pblock_new();
+	newpb = slapi_pblock_new();
 	slapi_search_internal_set_pb(
 					newpb,
 					parentdn,
@@ -130,6 +131,7 @@ tombstone_to_conflict_check_parent (
 					NULL,
 					OP_FLAG_NOOP);
 				slapi_rdn_free(&rdn);
+				slapi_sdn_free(&newsuperior);
 			}
 		}
 		}
@@ -137,6 +139,9 @@ tombstone_to_conflict_check_parent (
 	default:
 		break;
 	}
+
+	slapi_free_search_results_internal (newpb);
+	slapi_pblock_destroy(newpb);
 	return rc;
 }
 
@@ -230,7 +235,6 @@ conflict_to_tombstone(char *sessionid, Slapi_Entry *entry, CSN *opcsn)
 				sessionid, conflictdn, newrdn);
 		}
 	}
-	slapi_ch_free_string(&replconflict);
 
 	if (op_result) goto done;
 
@@ -269,6 +273,7 @@ tombstone_to_conflict (
 	char *parentuniqueid = slapi_entry_attr_get_charptr (tombstoneentry, SLAPI_ATTR_VALUE_PARENT_UNIQUEID); /* Allocated */
 	char *parentdn = slapi_dn_parent(slapi_sdn_get_ndn(conflictdn));
 	const CSN *dncsn = entry_get_dncsn(tombstoneentry);
+	csn_as_string(dncsn, PR_FALSE, csnstr);
 
 	slapi_log_err(SLAPI_LOG_ERR, repl_plugin_name,
 			                 "tombstone_to_conflict - %s - trying to resurrect tombstone as '%s'.\n",
@@ -288,7 +293,7 @@ tombstone_to_conflict (
 	slapi_mods_init (&smods, 5);
 	slapi_mods_add (&smods, LDAP_MOD_ADD, "objectclass", strlen("ldapsubentry"),"ldapsubentry");
 	slapi_mods_add (&smods, LDAP_MOD_DELETE, "objectclass", strlen("glue"),"glue");
-	slapi_mods_add (&smods, LDAP_MOD_REPLACE, "conflictcsn", strlen(csnstr),csn_as_string(dncsn, PR_FALSE, csnstr));
+	slapi_mods_add (&smods, LDAP_MOD_REPLACE, "conflictcsn", strlen(csnstr), csnstr);
 	PR_snprintf (buf, sizeof(buf), "%s (%s) %s",
 	             REASON_ANNOTATE_DN, "ADD", entrydn);
 	slapi_mods_add (&smods, LDAP_MOD_ADD, ATTR_NSDS5_REPLCONFLICT, strlen(buf), buf);
diff --git a/ldap/servers/slapd/slap.h b/ldap/servers/slapd/slap.h
index 6183d96..ce46ae1 100644
--- a/ldap/servers/slapd/slap.h
+++ b/ldap/servers/slapd/slap.h
@@ -855,8 +855,8 @@ struct matchingRuleList {
 #define PLUGIN_LIST_INDEX 19
 #define PLUGIN_LIST_BETXNPREOPERATION 20
 #define PLUGIN_LIST_BETXNPOSTOPERATION 21
-#define PLUGIN_LIST_GLOBAL_MAX 22
-#define PLUGIN_LIST_MMR 23
+#define PLUGIN_LIST_MMR 22
+#define PLUGIN_LIST_GLOBAL_MAX 23
 
 /* plugin configuration attributes */
 #define ATTR_PLUGIN_PATH				"nsslapd-pluginPath"
-- 
2.5.5

