diff --git a/CHANGES b/CHANGES
index 2a5343ea881cc7fa955edb617f26ff9c2279fb70..1bda83020d910d87d1ee8a5b7badedc58676f6d1 100644
--- a/CHANGES
+++ b/CHANGES
@@ -38,6 +38,7 @@ OpenLDAP 2.4.24 Engineering
 	Fixed liblutil getpass prompts (ITS#6702)
 	Fixed ldapsearch segfault with deref (ITS#6638)
 	Fixed ldapsearch multiple controls parsing (ITS#6651)
+	Fixed slapd SlapyReply usage (ITS#6758)
 	Fixed slapd acl parsing overflow (ITS#6611)
 	Fixed slapd assert control (ITS#5862)
 	Fixed slapd assertions and debugging (ITS#6759)
diff --git a/contrib/slapd-modules/addpartial/addpartial-overlay.c b/contrib/slapd-modules/addpartial/addpartial-overlay.c
index 62db7981f56770f093a05be8d0e029adede62555..8c5032d6e651f9e8fb0aedef111389a061b0b8ac 100644
--- a/contrib/slapd-modules/addpartial/addpartial-overlay.c
+++ b/contrib/slapd-modules/addpartial/addpartial-overlay.c
@@ -43,7 +43,6 @@ static slap_overinst addpartial;
 static int addpartial_add( Operation *op, SlapReply *rs)
 {
     Operation nop = *op;
-    SlapReply nrs = { REP_RESULT };
     Entry *toAdd = NULL;
     Entry *found = NULL;
     slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
@@ -258,12 +257,6 @@ static int addpartial_add( Operation *op, SlapReply *rs)
                 Debug(LDAP_DEBUG_TRACE, "%s: mods to do...\n",
                       addpartial.on_bi.bi_type, 0, 0);
 
-                memset(&nrs, 0, sizeof(nrs));
-                nrs.sr_type = REP_RESULT;
-                nrs.sr_err = LDAP_SUCCESS;
-                nrs.sr_entry = NULL;
-                nrs.sr_text = NULL;
-
                 nop.o_tag = LDAP_REQ_MODIFY;
                 nop.orm_modlist = mods;
                 nop.orm_no_opattrs = 0;
@@ -281,6 +274,7 @@ static int addpartial_add( Operation *op, SlapReply *rs)
 
                 if(nop.o_bd->be_modify)
                 {
+                    SlapReply nrs = { REP_RESULT };
                     rc = (nop.o_bd->be_modify)(&nop, &nrs);
                 }
 
diff --git a/contrib/slapd-modules/autogroup/autogroup.c b/contrib/slapd-modules/autogroup/autogroup.c
index 87cf8f00b2b8a5ded072fec6218f9d8c9c38672b..5fd689c40791b0cd5cc81e65cd06495b576f654b 100644
--- a/contrib/slapd-modules/autogroup/autogroup.c
+++ b/contrib/slapd-modules/autogroup/autogroup.c
@@ -450,6 +450,8 @@ autogroup_add_members_from_filter( Operation *op, Entry *e, autogroup_entry_t *a
 	o.o_bd->bd_info = (BackendInfo *)on;	
 
 	if ( modify == 1 && agg.agg_mod ) {
+		rs_reinit( &rs, REP_RESULT );
+
 		o = *op;
 		o.o_callback = &null_cb;
 		o.o_tag = LDAP_REQ_MODIFY;
@@ -1705,7 +1707,6 @@ autogroup_db_open(
 	autogroup_def_t		*agd;
 	autogroup_sc_t		ags;
 	Operation		*op;
-	SlapReply		rs = { REP_RESULT };
 	slap_callback		cb = { 0 };
 
 	void				*thrctx = ldap_pvt_thread_pool_context();
@@ -1748,6 +1749,7 @@ autogroup_db_open(
 	op->o_callback = &cb;
 
 	for (agd = agi->agi_def ; agd ; agd = agd->agd_next) {
+		SlapReply	rs = { REP_RESULT };
 
 		autogroup_build_def_filter(agd, op);
 
diff --git a/contrib/slapd-modules/cloak/cloak.c b/contrib/slapd-modules/cloak/cloak.c
index 413e89e0124d5ac1f22cdb8e6b495e55f7d76c3c..ae4bca63884e6d09ae4605004b577b5597e8fa77 100644
--- a/contrib/slapd-modules/cloak/cloak.c
+++ b/contrib/slapd-modules/cloak/cloak.c
@@ -220,10 +220,8 @@ cloak_search_response_cb( Operation *op, SlapReply *rs )
 	/*
 	 * We are now committed to cloak an attribute.
 	 */
-	if ( rs->sr_flags & REP_ENTRY_MODIFIABLE )
-		me = e;
-	else
-		me = entry_dup( e );
+	rs_ensure_entry_modifiable( op, rs, (slap_overinst *) op->o_bd->bd_info );
+	me = rs->sr_entry;
 		
 	for ( ci = (cloak_info_t *)sc->sc_private; ci; ci = ci->ci_next ) {
 		Attribute *a;
@@ -250,14 +248,6 @@ cloak_search_response_cb( Operation *op, SlapReply *rs )
 
 	}
 
-	if ( me != e ) {
-		if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED )
-			entry_free( e );
-
-		rs->sr_entry = me;
-        	rs->sr_flags |= REP_ENTRY_MODIFIABLE | REP_ENTRY_MUSTBEFREED;
-	}
-
 	return ( SLAP_CB_CONTINUE );
 }
 
diff --git a/contrib/slapd-modules/nssov/pam.c b/contrib/slapd-modules/nssov/pam.c
index 90dd16eb71e6032264716a15be3daf8e4dddcd20..61cf2665f2473adf60a4ab637cb1a9efc02ecab9 100644
--- a/contrib/slapd-modules/nssov/pam.c
+++ b/contrib/slapd-modules/nssov/pam.c
@@ -206,7 +206,6 @@ int pam_authc(nssov_info *ni,TFILE *fp,Operation *op)
 	int32_t tmpint32;
 	int rc;
 	slap_callback cb = {0};
-	SlapReply rs = {REP_RESULT};
 	char dnc[1024];
 	char uidc[32];
 	char svcc[256];
@@ -274,7 +273,6 @@ int pam_authz(nssov_info *ni,TFILE *fp,Operation *op)
 	int rc;
 	Entry *e = NULL;
 	Attribute *a;
-	SlapReply rs = {REP_RESULT};
 	slap_callback cb = {0};
 
 	READ_STRING(fp,uidc);
@@ -314,6 +312,7 @@ int pam_authz(nssov_info *ni,TFILE *fp,Operation *op)
 		AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
 		struct berval hostdn = BER_BVNULL;
 		struct berval odn = op->o_ndn;
+		SlapReply rs = {REP_RESULT};
 		op->o_dn = dn;
 		op->o_ndn = dn;
 		{
@@ -342,8 +341,7 @@ int pam_authz(nssov_info *ni,TFILE *fp,Operation *op)
 				!BER_BVISEMPTY(&ni->ni_pam_defhost)) {
 				filter.bv_len = sizeof(fbuf);
 				filter.bv_val = fbuf;
-				memset(&rs2, 0, sizeof(rs2));
-				rs2.sr_type = REP_RESULT;
+				rs_reinit(&rs2, REP_RESULT);
 				nssov_filter_byname(mi,0,&ni->ni_pam_defhost,&filter);
 				op->ors_filterstr = filter;
 				op->ors_filter = str2filter_x(op, filter.bv_val);
@@ -382,6 +380,7 @@ int pam_authz(nssov_info *ni,TFILE *fp,Operation *op)
 		!BER_BVISEMPTY(&ni->ni_pam_group_dn) &&
 		ni->ni_pam_group_ad) {
 		AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
+		SlapReply rs = {REP_RESULT};
 		op->o_callback = &cb;
 		cb.sc_response = slap_null_cb;
 		op->o_tag = LDAP_REQ_COMPARE;
diff --git a/servers/slapd/back-bdb/compare.c b/servers/slapd/back-bdb/compare.c
index dcf8fd9d7681aed3f2984ea13a2f6f28f8ec3351..031bef063bcae5249b2e34f0010e84cb963eb499 100644
--- a/servers/slapd/back-bdb/compare.c
+++ b/servers/slapd/back-bdb/compare.c
@@ -27,7 +27,6 @@ bdb_compare( Operation *op, SlapReply *rs )
 	struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
 	Entry		*e = NULL;
 	EntryInfo	*ei;
-	Attribute	*a;
 	int		manageDSAit = get_manageDSAit( op );
 
 	DB_TXN		*rtxn;
diff --git a/servers/slapd/back-bdb/dn2id.c b/servers/slapd/back-bdb/dn2id.c
index b83496c3bfd7f1daff8c064efc29e7485fc34ab0..afa2164214c55404f5f12bab4de0607a32bef613 100644
--- a/servers/slapd/back-bdb/dn2id.c
+++ b/servers/slapd/back-bdb/dn2id.c
@@ -151,7 +151,6 @@ bdb_dn2id_delete(
 	DB *db = bdb->bi_dn2id->bdi_db;
 	char		*buf;
 	DBT		key;
-	DB_LOCK	lock;
 	struct berval	pdn, ptr;
 	int		rc;
 
@@ -614,7 +613,6 @@ hdb_dn2id_delete(
 	int rc;
 	ID	nid;
 	unsigned char dlen[2];
-	DB_LOCK	lock;
 
 	Debug( LDAP_DEBUG_TRACE, "=> hdb_dn2id_delete 0x%lx: \"%s\"\n",
 		e->e_id, e->e_ndn, 0 );
diff --git a/servers/slapd/back-bdb/search.c b/servers/slapd/back-bdb/search.c
index bd43ea04a87b026c708ba036d2077e977845ac3c..1980152e375df1a703a2d0d27de0eb4369a70f04 100644
--- a/servers/slapd/back-bdb/search.c
+++ b/servers/slapd/back-bdb/search.c
@@ -748,8 +748,6 @@ fetch_entry_retry:
 			goto loop_continue;
 		}
 
-		rs->sr_entry = e;
-
 		if ( is_entry_subentry( e ) ) {
 			if( op->oq_search.rs_scope != LDAP_SCOPE_BASE ) {
 				if(!get_subentries_visibility( op )) {
@@ -881,6 +879,7 @@ fetch_entry_retry:
 			blis.bli_lock = lock;
 			blis.bli_flag = BLI_DONTFREE;
 
+			rs->sr_entry = e;
 			rs->sr_flags = REP_ENTRY_MUSTRELEASE;
 
 			send_search_reference( op, rs );
@@ -912,7 +911,7 @@ fetch_entry_retry:
 		}
 
 		/* if it matches the filter and scope, send it */
-		rs->sr_err = test_filter( op, rs->sr_entry, op->oq_search.rs_filter );
+		rs->sr_err = test_filter( op, e, op->oq_search.rs_filter );
 
 		if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
 			/* check size limit */
@@ -956,9 +955,13 @@ fetch_entry_retry:
 				rs->sr_attrs = op->oq_search.rs_attrs;
 				rs->sr_operational_attrs = NULL;
 				rs->sr_ctrls = NULL;
+				rs->sr_entry = e;
+				RS_ASSERT( e->e_private != NULL );
 				rs->sr_flags = REP_ENTRY_MUSTRELEASE;
 				rs->sr_err = LDAP_SUCCESS;
 				rs->sr_err = send_search_entry( op, rs );
+				rs->sr_attrs = NULL;
+				rs->sr_entry = NULL;
 
 				/* send_search_entry will usually free it.
 				 * an overlay might leave its own copy here;
@@ -976,7 +979,6 @@ fetch_entry_retry:
 							OpExtra, oe_next );
 					}
 				}
-				rs->sr_entry = NULL;
 				e = NULL;
 
 				switch ( rs->sr_err ) {
@@ -1012,6 +1014,7 @@ loop_continue:
 			slap_zn_runlock(bdb->bi_cache.c_zctx, e);
 #endif
 			bdb_cache_return_entry_r( bdb, e , &lock );
+			RS_ASSERT( rs->sr_entry == NULL );
 			e = NULL;
 			rs->sr_entry = NULL;
 		}
@@ -1326,4 +1329,3 @@ send_paged_response(
 done:
 	(void) ber_free_buf( ber );
 }
-
diff --git a/servers/slapd/back-dnssrv/search.c b/servers/slapd/back-dnssrv/search.c
index 7746f86f07774c74c0c3dc0cf90348c086daf369..31fca8770f5d6ee60877f3d3edbb4037564353e6 100644
--- a/servers/slapd/back-dnssrv/search.c
+++ b/servers/slapd/back-dnssrv/search.c
@@ -219,6 +219,7 @@ dnssrv_back_search(
 			send_search_entry( op, rs );
 			rs->sr_entry = NULL;
 			rs->sr_attrs = NULL;
+			rs->sr_flags = 0;
 		}
 
 		entry_clean( &e );
@@ -237,4 +238,3 @@ done:
 	if( urls != NULL ) ber_bvarray_free( urls );
 	return 0;
 }
-
diff --git a/servers/slapd/back-ldap/chain.c b/servers/slapd/back-ldap/chain.c
index e021fa68ab4cbad0c5a66806a941c9a76ef685a4..759fb53157501ad7450cc4fb9f67eb70a133932f 100644
--- a/servers/slapd/back-ldap/chain.c
+++ b/servers/slapd/back-ldap/chain.c
@@ -703,7 +703,7 @@ ldap_chain_search(
 	 * to be set once for all (correct?) */
 	li.li_bvuri = bvuri;
 	for ( ; !BER_BVISNULL( &ref[0] ); ref++ ) {
-		SlapReply	rs2 = { 0 };
+		SlapReply	rs2 = { REP_RESULT };
 		LDAPURLDesc	*srv;
 		req_search_s	save_oq_search = op->oq_search,
 				tmp_oq_search = { 0 };
diff --git a/servers/slapd/back-ldap/distproc.c b/servers/slapd/back-ldap/distproc.c
index a878e1daa52e3f8c5565e05cf3a957f2e1fa5961..7fb41d4b7b5858ff508e61b46e5449a9d9700890 100644
--- a/servers/slapd/back-ldap/distproc.c
+++ b/servers/slapd/back-ldap/distproc.c
@@ -107,6 +107,7 @@ typedef struct ContinuationReference_t {
 } ContinuationReference_t;
 #define	CR_INIT		{ NULL, BER_BVNULL, LDAP_DP_RT_UNKNOWN, BER_BVNULL, LDAP_DP_SS_UNKNOWN, NULL, BER_BVNULL }
 
+#ifdef unused
 static struct berval	bv2rt[] = {
 	BER_BVC( "superior" ),
 	BER_BVC( "subordinate" ),
@@ -196,6 +197,7 @@ ldap_distproc_str2ss( const char *s )
 	ber_str2bv( s, 0, 0, &bv );
 	return ldap_distproc_bv2ss( &bv );
 }
+#endif /* unused */
 
 /*
  * NOTE: this overlay assumes that the chainingBehavior control
@@ -239,12 +241,6 @@ static int ldap_distproc_db_init_one( BackendDB *be );
 #define	ldap_distproc_db_close_one(be)		(0)
 #define	ldap_distproc_db_destroy_one(be, ca)	(lback)->bi_db_destroy( (be), (ca) )
 
-static int
-ldap_distproc_parse_ctrl(
-	Operation	*op,
-	SlapReply	*rs,
-	LDAPControl	*ctrl );
-
 static int
 ldap_distproc_uri_cmp( const void *c1, const void *c2 )
 {
diff --git a/servers/slapd/back-ldap/extended.c b/servers/slapd/back-ldap/extended.c
index e1402c3bb1fe51134781dbc0a2ca82fde7a5d413..b0cc19fb48d1dee590bc7a0a82a575c5016df261 100644
--- a/servers/slapd/back-ldap/extended.c
+++ b/servers/slapd/back-ldap/extended.c
@@ -90,6 +90,9 @@ ldap_back_extended(
 {
 	int	i;
 
+	RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
+	rs->sr_flags &= ~REP_ENTRY_MASK;	/* paranoia */
+
 	for ( i = 0; exop_table[i].extended != NULL; i++ ) {
 		if ( bvmatch( &exop_table[i].oid, &op->oq_extended.rs_reqoid ) )
 		{
@@ -397,4 +400,3 @@ retry:
 
 	return rc;
 }
-
diff --git a/servers/slapd/back-ldap/search.c b/servers/slapd/back-ldap/search.c
index e4fe3eade45c8479601f83b67867269b3ff8919c..0b569fe2f13064612277c8837dd45fd65057f260 100644
--- a/servers/slapd/back-ldap/search.c
+++ b/servers/slapd/back-ldap/search.c
@@ -170,6 +170,9 @@ ldap_back_search(
 	char		**references = NULL;
 	void		*matchctx = NULL;
 
+	rs_assert_ready( rs );
+	rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia, we can set rs = non-entry */
+
 	if ( !ldap_back_dobind( &lc, op, rs, LDAP_BACK_SENDERR ) ) {
 		return rs->sr_err;
 	}
@@ -342,6 +345,7 @@ retry:
 					rs->sr_ctrls = NULL;
 				}
 				rs->sr_entry = NULL;
+				rs->sr_flags = 0;
 				if ( !BER_BVISNULL( &ent.e_name ) ) {
 					assert( ent.e_name.bv_val != bdn.bv_val );
 					op->o_tmpfree( ent.e_name.bv_val, op->o_tmpmemctx );
@@ -399,6 +403,7 @@ retry:
 				BER_BVZERO( &rs->sr_ref[ cnt ] );
 
 				/* ignore return value by now */
+				RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
 				rs->sr_entry = NULL;
 				( void )send_search_reference( op, rs );
 
@@ -976,4 +981,3 @@ cleanup:
 
 	return rc;
 }
-
diff --git a/servers/slapd/back-ldif/ldif.c b/servers/slapd/back-ldif/ldif.c
index 4557e6e4d95e24b08d4f0fcf8c8330c27cacd0dc..78d35629980a7698625f1510812b5c87af62863f 100644
--- a/servers/slapd/back-ldif/ldif.c
+++ b/servers/slapd/back-ldif/ldif.c
@@ -660,9 +660,14 @@ ldif_send_entry( Operation *op, SlapReply *rs, Entry *e, int scope )
 		else if ( test_filter( op, e, op->ors_filter ) == LDAP_COMPARE_TRUE ) {
 			rs->sr_entry = e;
 			rs->sr_attrs = op->ors_attrs;
+			/* Could set REP_ENTRY_MUSTBEFREED too for efficiency,
+			 * but refraining lets us test unFREEable MODIFIABLE
+			 * entries.  Like entries built on the stack.
+			 */
 			rs->sr_flags = REP_ENTRY_MODIFIABLE;
 			rc = send_search_entry( op, rs );
 			rs->sr_entry = NULL;
+			rs->sr_attrs = NULL;
 		}
 	}
 
diff --git a/servers/slapd/back-meta/conn.c b/servers/slapd/back-meta/conn.c
index 52a5f82d2316b47cb266e055d112aeaf1e9d41ac..8f2b9bd335641a45e2e0f15779f93c5471a74a97 100644
--- a/servers/slapd/back-meta/conn.c
+++ b/servers/slapd/back-meta/conn.c
@@ -908,7 +908,7 @@ meta_back_get_candidate(
 
 	} else if ( candidate == META_TARGET_MULTIPLE ) {
 		Operation	op2 = *op;
-		SlapReply	rs2 = { 0 };
+		SlapReply	rs2 = { REP_RESULT };
 		slap_callback	cb2 = { 0 };
 		int		rc;
 
@@ -1891,4 +1891,3 @@ meta_back_quarantine(
 done:;
 	ldap_pvt_thread_mutex_unlock( &mt->mt_quarantine_mutex );
 }
-
diff --git a/servers/slapd/back-meta/search.c b/servers/slapd/back-meta/search.c
index 2ea5645fc92fa2190a670e66b118354cea2c34e6..9ae4370259b38baf96b865076ceb5220bd7c06ec 100644
--- a/servers/slapd/back-meta/search.c
+++ b/servers/slapd/back-meta/search.c
@@ -789,6 +789,9 @@ meta_back_search( Operation *op, SlapReply *rs )
 	SlapReply	*candidates = NULL;
 	int		do_taint = 0;
 
+	rs_assert_ready( rs );
+	rs->sr_flags &= ~REP_ENTRY_MASK; /* paranoia, we can set rs = non-entry */
+
 	/*
 	 * controls are set in ldap_back_dobind()
 	 * 
diff --git a/servers/slapd/back-monitor/compare.c b/servers/slapd/back-monitor/compare.c
index a16feb8cc127ea7783feb6a7f890a42a113a746b..36dce49b20da0e1f7229933feaeab5a6e3ceff60 100644
--- a/servers/slapd/back-monitor/compare.c
+++ b/servers/slapd/back-monitor/compare.c
@@ -31,7 +31,6 @@ monitor_back_compare( Operation *op, SlapReply *rs )
 {
 	monitor_info_t	*mi = ( monitor_info_t * ) op->o_bd->be_private;
 	Entry           *e, *matched = NULL;
-	Attribute	*a;
 	int		rc;
 
 	/* get entry with reader lock */
@@ -58,17 +57,12 @@ monitor_back_compare( Operation *op, SlapReply *rs )
 	}
 
 	rs->sr_err = slap_compare_entry( op, e, op->orc_ava );
-
-return_results:;
 	rc = rs->sr_err;
 	switch ( rc ) {
 	case LDAP_COMPARE_FALSE:
 	case LDAP_COMPARE_TRUE:
 		rc = LDAP_SUCCESS;
 		break;
-
-	default:
-		break;
 	}
 		
 	send_ldap_result( op, rs );
diff --git a/servers/slapd/back-monitor/init.c b/servers/slapd/back-monitor/init.c
index ef8b1eb3768f22c7d67b09e875d88e491429b285..bdfffcd64433da0f0d34c09958ab3a8e256fcd39 100644
--- a/servers/slapd/back-monitor/init.c
+++ b/servers/slapd/back-monitor/init.c
@@ -830,7 +830,7 @@ monitor_search2ndn(
 	OperationBuffer	opbuf;
 	Operation	*op;
 	void	*thrctx;
-	SlapReply	rs = { 0 };
+	SlapReply	rs = { REP_RESULT };
 	slap_callback	cb = { NULL, monitor_search2ndn_cb, NULL, NULL };
 	int		rc;
 
diff --git a/servers/slapd/back-ndb/search.cpp b/servers/slapd/back-ndb/search.cpp
index 57d97bcd548d01b780e8671dcac46230c3ab1172..7631cb798cc0c3c7a054f229e3808d3242378254 100644
--- a/servers/slapd/back-ndb/search.cpp
+++ b/servers/slapd/back-ndb/search.cpp
@@ -483,6 +483,7 @@ static int ndb_oc_search( Operation *op, SlapReply *rs, Ndb *ndb, NdbTransaction
 				rs->sr_flags = 0;
 				rc = send_search_entry( op, rs );
 				rs->sr_entry = NULL;
+				rs->sr_attrs = NULL;
 			} else {
 				rc = 0;
 			}
diff --git a/servers/slapd/back-passwd/search.c b/servers/slapd/back-passwd/search.c
index 20c783c0e441c638e2c0147b3b6ede930dcce9ff..6a4258ab2c1e8eaabd4d50a110e53e06393e5a02 100644
--- a/servers/slapd/back-passwd/search.c
+++ b/servers/slapd/back-passwd/search.c
@@ -123,6 +123,8 @@ passwd_back_search(
 				rs->sr_attrs = op->ors_attrs;
 				rs->sr_flags = REP_ENTRY_MODIFIABLE;
 				send_search_entry( op, rs );
+				rs->sr_flags = 0;
+				rs->sr_attrs = NULL;
 			}
 
 			entry_clean( &e );
@@ -173,6 +175,8 @@ passwd_back_search(
 					rs->sr_attrs = op->ors_attrs;
 					rs->sr_flags = REP_ENTRY_MODIFIABLE;
 					send_search_entry( op, rs );
+					rs->sr_flags = 0;
+					rs->sr_entry = NULL;
 				}
 
 				entry_clean( &e );
@@ -238,6 +242,9 @@ passwd_back_search(
 			rs->sr_attrs = op->ors_attrs;
 			rs->sr_flags = REP_ENTRY_MODIFIABLE;
 			send_search_entry( op, rs );
+			rs->sr_flags = 0;
+			rs->sr_entry = NULL;
+			rs->sr_attrs = NULL;
 		}
 
 		entry_clean( &e );
diff --git a/servers/slapd/back-perl/search.c b/servers/slapd/back-perl/search.c
index 554d0f3f85306af52576309a2d4178b9f1e4a1b5..0d73f82ecbca5d6c43ad7bd95e4106c4f0ef2c58 100644
--- a/servers/slapd/back-perl/search.c
+++ b/servers/slapd/back-perl/search.c
@@ -92,8 +92,10 @@ perl_back_search(
 						rs->sr_flags = REP_ENTRY_MODIFIABLE;
 						rs->sr_err = LDAP_SUCCESS;
 						rs->sr_err = send_search_entry( op, rs );
+						rs->sr_flags = 0;
+						rs->sr_attrs = NULL;
+						rs->sr_entry = NULL;
 						if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
-							rs->sr_entry = NULL;
 							goto done;
 						}
 					}
@@ -124,4 +126,3 @@ done:;
 
 	return 0;
 }
-
diff --git a/servers/slapd/back-shell/result.c b/servers/slapd/back-shell/result.c
index 416d41c45e49c8cc37312447fc62674337686e8b..25ac11802137dcf5a47565ffc42e3bad02e686b0 100644
--- a/servers/slapd/back-shell/result.c
+++ b/servers/slapd/back-shell/result.c
@@ -103,6 +103,7 @@ read_and_send_results(
 				rs->sr_flags = REP_ENTRY_MODIFIABLE;
 				send_search_entry( op, rs );
 				entry_free( rs->sr_entry );
+				rs->sr_attrs = NULL;
 			}
 
 			bp = buf;
diff --git a/servers/slapd/back-sock/result.c b/servers/slapd/back-sock/result.c
index 052afb6c5117a58f4eaa11e9d2d55abd1e0b5dc0..085e79d5a5d29bf1730e224a7bb89f47bb9b2d09 100644
--- a/servers/slapd/back-sock/result.c
+++ b/servers/slapd/back-sock/result.c
@@ -100,6 +100,7 @@ sock_read_and_send_results(
 				rs->sr_flags = REP_ENTRY_MODIFIABLE;
 				send_search_entry( op, rs );
 				entry_free( rs->sr_entry );
+				rs->sr_attrs = NULL;
 			}
 
 			bp = buf;
diff --git a/servers/slapd/back-sql/compare.c b/servers/slapd/back-sql/compare.c
index c46146dc960f7c8ccfc18346d23fdf71d8d5ec65..072f6049ece80c08bf305c3b24e599872ac89285 100644
--- a/servers/slapd/back-sql/compare.c
+++ b/servers/slapd/back-sql/compare.c
@@ -98,7 +98,7 @@ backsql_compare( Operation *op, SlapReply *rs )
 	}
 
 	if ( is_at_operational( op->oq_compare.rs_ava->aa_desc->ad_type ) ) {
-		SlapReply	nrs = { 0 };
+		SlapReply	nrs = { REP_SEARCH };
 		Attribute	**ap;
 
 		for ( ap = &e.e_attrs; *ap; ap = &(*ap)->a_next )
diff --git a/servers/slapd/back-sql/delete.c b/servers/slapd/back-sql/delete.c
index e126945722cfe3c4bd165d82d2f4851015fb4ce5..a95188642230d72c945faeb05eed2de2a801fc6a 100644
--- a/servers/slapd/back-sql/delete.c
+++ b/servers/slapd/back-sql/delete.c
@@ -334,7 +334,7 @@ backsql_tree_delete(
 {
 	Operation		op2 = *op;
 	slap_callback		sc = { 0 };
-	SlapReply		rs2 = { 0 };
+	SlapReply		rs2 = { REP_RESULT };
 	backsql_tree_delete_t	btd = { 0 };
 
 	int			rc;
diff --git a/servers/slapd/back-sql/search.c b/servers/slapd/back-sql/search.c
index 696f34822af517e28c24546aa43ec036510f3f48..afcb4cae5cfa6794428dcf0758643e907a426065 100644
--- a/servers/slapd/back-sql/search.c
+++ b/servers/slapd/back-sql/search.c
@@ -2513,7 +2513,7 @@ send_results:;
 #ifdef BACKSQL_SYNCPROV
 	if ( op->o_sync ) {
 		Operation	op2 = *op;
-		SlapReply	rs2 = { 0 };
+		SlapReply	rs2 = { REP_RESULT };
 		Entry		*e = entry_alloc();
 		slap_callback	cb = { 0 };
 
diff --git a/servers/slapd/backend.c b/servers/slapd/backend.c
index 79004869e238cc62854d4601b2453fb8e63940d4..c5b01a06b42320da84e9d3f8608e606bac77a708 100644
--- a/servers/slapd/backend.c
+++ b/servers/slapd/backend.c
@@ -1673,7 +1673,7 @@ fe_acl_attribute(
 
 		a = attr_find( e->e_attrs, entry_at );
 		if ( a == NULL ) {
-			SlapReply	rs = { 0 };
+			SlapReply	rs = { REP_SEARCH };
 			AttributeName	anlist[ 2 ];
 
 			anlist[ 0 ].an_name = entry_at->ad_cname;
@@ -1686,8 +1686,7 @@ fe_acl_attribute(
  			 * to do no harm to entries */
  			rs.sr_entry = e;
   			rc = backend_operational( op, &rs );
- 			rs.sr_entry = NULL;
- 
+
 			if ( rc == LDAP_SUCCESS ) {
 				if ( rs.sr_operational_attrs ) {
 					freeattr = 1;
@@ -1838,7 +1837,7 @@ backend_access(
 		} else {
 			a = attr_find( e->e_attrs, entry_at );
 			if ( a == NULL ) {
-				SlapReply	rs = { 0 };
+				SlapReply	rs = { REP_SEARCH };
 				AttributeName	anlist[ 2 ];
 
 				anlist[ 0 ].an_name = entry_at->ad_cname;
@@ -1853,7 +1852,6 @@ backend_access(
 				 * to do no harm to entries */
 				rs.sr_entry = e;
 				rc = backend_operational( op, &rs );
-				rs.sr_entry = NULL;
 
 				if ( rc == LDAP_SUCCESS ) {
 					if ( rs.sr_operational_attrs ) {
diff --git a/servers/slapd/backglue.c b/servers/slapd/backglue.c
index 4dba4915731ecfa13e0a9af552f587db83cbfc73..31afe2fff471ccf3320df8ef012a0e3d2e52e916 100644
--- a/servers/slapd/backglue.c
+++ b/servers/slapd/backglue.c
@@ -419,9 +419,10 @@ glue_op_search ( Operation *op, SlapReply *rs )
 		if ( op->o_bd == b0 )
 			return SLAP_CB_CONTINUE;
 
-		rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
 		if (op->o_bd && op->o_bd->be_search) {
 			rs->sr_err = op->o_bd->be_search( op, rs );
+		} else {
+			rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
 		}
 		return rs->sr_err;
 
diff --git a/servers/slapd/bconfig.c b/servers/slapd/bconfig.c
index 0fb7c5f3c5a0f263cdb7de3a11c1742ee8220823..3e6bd36a0461939628a86e168b8a5af275163919 100644
--- a/servers/slapd/bconfig.c
+++ b/servers/slapd/bconfig.c
@@ -4004,10 +4004,12 @@ config_setup_ldif( BackendDB *be, const char *dir, int readit ) {
 
 		op->o_tag = LDAP_REQ_ADD;
 		if ( rc == LDAP_SUCCESS && sc.frontend ) {
+			rs_reinit( &rs, REP_RESULT );
 			op->ora_e = sc.frontend;
 			rc = op->o_bd->be_add( op, &rs );
 		}
 		if ( rc == LDAP_SUCCESS && sc.config ) {
+			rs_reinit( &rs, REP_RESULT );
 			op->ora_e = sc.config;
 			rc = op->o_bd->be_add( op, &rs );
 		}
@@ -5290,6 +5292,7 @@ out2:;
 out:;
 	{	int repl = op->o_dont_replicate;
 		if ( rs->sr_err == LDAP_COMPARE_TRUE ) {
+			rs->sr_text = NULL; /* Set after config_add_internal */
 			rs->sr_err = LDAP_SUCCESS;
 			op->o_dont_replicate = 1;
 		}
@@ -5621,6 +5624,7 @@ out:
 out_noop:
 	if ( rc == LDAP_SUCCESS ) {
 		attrs_free( save_attrs );
+		rs->sr_text = NULL;
 	} else {
 		attrs_free( e->e_attrs );
 		e->e_attrs = save_attrs;
@@ -5667,7 +5671,10 @@ config_back_modify( Operation *op, SlapReply *rs )
 	rdn = ce->ce_entry->e_nname;
 	ptr = strchr( rdn.bv_val, '=' );
 	rdn.bv_len = ptr - rdn.bv_val;
-	slap_bv2ad( &rdn, &rad, &rs->sr_text );
+	rs->sr_err = slap_bv2ad( &rdn, &rad, &rs->sr_text );
+	if ( rs->sr_err != LDAP_SUCCESS ) {
+		goto out;
+	}
 
 	/* Some basic validation... */
 	for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
@@ -6168,6 +6175,8 @@ config_build_attrs( Entry *e, AttributeType **at, AttributeDescription *ad,
 	return 0;
 }
 
+/* currently (2010) does not access rs except possibly writing rs->sr_err */
+
 Entry *
 config_build_entry( Operation *op, SlapReply *rs, CfEntryInfo *parent,
 	ConfigArgs *c, struct berval *rdn, ConfigOCs *main, ConfigOCs *extra )
@@ -6265,9 +6274,12 @@ fail:
 		op->ora_modlist = NULL;
 		slap_add_opattrs( op, NULL, NULL, 0, 0 );
 		if ( !op->o_noop ) {
-			op->o_bd->be_add( op, rs );
-			if ( ( rs->sr_err != LDAP_SUCCESS ) 
-					&& (rs->sr_err != LDAP_ALREADY_EXISTS) ) {
+			SlapReply rs2 = {REP_RESULT};
+			op->o_bd->be_add( op, &rs2 );
+			rs->sr_err = rs2.sr_err;
+			rs_assert_done( &rs2 );
+			if ( ( rs2.sr_err != LDAP_SUCCESS ) 
+					&& (rs2.sr_err != LDAP_ALREADY_EXISTS) ) {
 				goto fail;
 			}
 		}
@@ -6548,9 +6560,11 @@ config_back_db_open( BackendDB *be, ConfigReply *cr )
 
 	/* Create schema nodes for included schema... */
 	if ( cfb->cb_config->c_kids ) {
+		int rc;
 		c.depth = 0;
 		c.ca_private = cfb->cb_config->c_kids;
-		if (config_build_schema_inc( &c, ce, op, &rs )) {
+		rc = config_build_schema_inc( &c, ce, op, &rs );
+		if ( rc ) {
 			return -1;
 		}
 	}
@@ -6625,8 +6639,10 @@ config_back_db_open( BackendDB *be, ConfigReply *cr )
 			return -1;
 		}
 		ce = e->e_private;
-		if ( be->be_cf_ocs && be->be_cf_ocs->co_cfadd )
+		if ( be->be_cf_ocs && be->be_cf_ocs->co_cfadd ) {
+			rs_reinit( &rs, REP_RESULT );
 			be->be_cf_ocs->co_cfadd( op, &rs, e, &c );
+		}
 		/* Iterate through overlays */
 		if ( oi ) {
 			slap_overinst *on;
@@ -6665,8 +6681,10 @@ config_back_db_open( BackendDB *be, ConfigReply *cr )
 				if ( !oe ) {
 					return -1;
 				}
-				if ( c.bi->bi_cf_ocs && c.bi->bi_cf_ocs->co_cfadd )
+				if ( c.bi->bi_cf_ocs && c.bi->bi_cf_ocs->co_cfadd ) {
+					rs_reinit( &rs, REP_RESULT );
 					c.bi->bi_cf_ocs->co_cfadd( op, &rs, oe, &c );
+				}
 			}
 		}
 	}
diff --git a/servers/slapd/connection.c b/servers/slapd/connection.c
index b9397df38e29e896b70ede5e1fcd8c4225631287..0d508ee7ef6eca1f66a66a5ad5c598025ff70d53 100644
--- a/servers/slapd/connection.c
+++ b/servers/slapd/connection.c
@@ -702,7 +702,6 @@ static void connection_abandon( Connection *c )
 
 	Operation *o, *next, op = {0};
 	Opheader ohdr = {0};
-	SlapReply rs = {0};
 
 	op.o_hdr = &ohdr;
 	op.o_conn = c;
@@ -710,6 +709,8 @@ static void connection_abandon( Connection *c )
 	op.o_tag = LDAP_REQ_ABANDON;
 
 	for ( o = LDAP_STAILQ_FIRST( &c->c_ops ); o; o=next ) {
+		SlapReply rs = {REP_RESULT};
+
 		next = LDAP_STAILQ_NEXT( o, o_next );
 		op.orn_msgid = o->o_msgid;
 		o->o_abandon = 1;
diff --git a/servers/slapd/extended.c b/servers/slapd/extended.c
index 2657eeab8b8613d8610972ee9ca6d19991a23991..3d5fc37572b498ad3dbf910dd39735c231ad19fe 100644
--- a/servers/slapd/extended.c
+++ b/servers/slapd/extended.c
@@ -244,10 +244,12 @@ fe_extended( Operation *op, SlapReply *rs )
 
 		if ( rs->sr_rspoid != NULL ) {
 			free( (char *)rs->sr_rspoid );
+			rs->sr_rspoid = NULL;
 		}
 
 		if ( rs->sr_rspdata != NULL ) {
 			ber_bvfree( rs->sr_rspdata );
+			rs->sr_rspdata = NULL;
 		}
 	} /* end of OpenLDAP extended operation */
 
diff --git a/servers/slapd/overlays/accesslog.c b/servers/slapd/overlays/accesslog.c
index 414909f139d06e16f3088c41b28ae542c72ae8d9..957738c7e2902f5b0eef2310c77675a450d13b41 100644
--- a/servers/slapd/overlays/accesslog.c
+++ b/servers/slapd/overlays/accesslog.c
@@ -669,8 +669,10 @@ accesslog_purge( void *ctx, void *arg )
 		for (i=0; i<pd.used; i++) {
 			op->o_req_dn = pd.dn[i];
 			op->o_req_ndn = pd.ndn[i];
-			if ( !slapd_shutdown )
+			if ( !slapd_shutdown ) {
+				rs_reinit( &rs, REP_RESULT );
 				op->o_bd->be_delete( op, &rs );
+			}
 			ch_free( pd.ndn[i].bv_val );
 			ch_free( pd.dn[i].bv_val );
 		}
@@ -680,6 +682,7 @@ accesslog_purge( void *ctx, void *arg )
 		{
 			Modifications mod;
 			struct berval bv[2];
+			rs_reinit( &rs, REP_RESULT );
 			/* update context's entryCSN to reflect oldest CSN */
 			mod.sml_numvals = 1;
 			mod.sml_values = bv;
diff --git a/servers/slapd/overlays/collect.c b/servers/slapd/overlays/collect.c
index 1c0a73932d9ef4c4910d6ff37ab37f34e60c47bb..d484a4aff52ec6ee25598f7d76b26bcf735aff8d 100644
--- a/servers/slapd/overlays/collect.c
+++ b/servers/slapd/overlays/collect.c
@@ -386,19 +386,7 @@ collect_response( Operation *op, SlapReply *rs )
 			* don't modify it directly. Make a copy and
 			* work with that instead.
 			*/
-			if ( !( rs->sr_flags & REP_ENTRY_MODIFIABLE ) ) {
-				Entry *e;
-
-				e = entry_dup( rs->sr_entry );
-				if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
-					overlay_entry_release_ov( op, rs->sr_entry, 0, on );
-					rs->sr_flags &= ~REP_ENTRY_MUSTRELEASE;
-				} else if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
-					entry_free( rs->sr_entry );
-				}
-				rs->sr_entry = e;
-				rs->sr_flags |= REP_ENTRY_MODIFIABLE|REP_ENTRY_MUSTBEFREED;
-			}
+			rs_ensure_entry_modifiable( op, rs, on );
 
 			/* Loop for each attribute in this collectinfo */
 			for(idx=0; idx<ci->ci_ad_num; idx++) {
diff --git a/servers/slapd/overlays/constraint.c b/servers/slapd/overlays/constraint.c
index ec640d9dad044dfdcf8e02caf43adbf3eceb879e..b3dbbae5c4d64b331c0f7ad3348a7647700fc403 100644
--- a/servers/slapd/overlays/constraint.c
+++ b/servers/slapd/overlays/constraint.c
@@ -541,7 +541,7 @@ constraint_uri_cb( Operation *op, SlapReply *rs )
 }
 
 static int
-constraint_violation( constraint *c, struct berval *bv, Operation *op, SlapReply *rs)
+constraint_violation( constraint *c, struct berval *bv, Operation *op )
 {
 	if ((!c) || (!bv)) return LDAP_SUCCESS;
 	
@@ -556,19 +556,13 @@ constraint_violation( constraint *c, struct berval *bv, Operation *op, SlapReply
 		Operation nop = *op;
 		slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
 		slap_callback cb;
-		SlapReply nrs = { REP_RESULT };
 		int i;
-		int found;
+		int found = 0;
 		int rc;
 		size_t len;
 		struct berval filterstr;
 		char *ptr;
 
-		found = 0;
-
-		nrs.sr_entry = NULL;
-		nrs.sr_nentries = 0;
-
 		cb.sc_next = NULL;
 		cb.sc_response = constraint_uri_cb;
 		cb.sc_cleanup = NULL;
@@ -645,6 +639,8 @@ constraint_violation( constraint *c, struct berval *bv, Operation *op, SlapReply
 			rc = LDAP_OTHER;
 
 		} else {
+			SlapReply nrs = { REP_RESULT };
+
 			Debug(LDAP_DEBUG_TRACE, 
 				"==> constraint_violation uri filter = %s\n",
 				filterstr.bv_val, 0, 0);
@@ -663,7 +659,6 @@ constraint_violation( constraint *c, struct berval *bv, Operation *op, SlapReply
 
 		if (!found)
 			return LDAP_CONSTRAINT_VIOLATION; /* constraint violation */
-			
 	}
 
 	return LDAP_SUCCESS;
@@ -796,7 +791,7 @@ constraint_add( Operation *op, SlapReply *rs )
 			}
 
 			for ( i = 0; b[i].bv_val; i++ ) {
-				rc = constraint_violation( cp, &b[i], op, rs );
+				rc = constraint_violation( cp, &b[i], op );
 				if ( rc ) {
 					goto add_violation;
 				}
@@ -949,7 +944,7 @@ constraint_update( Operation *op, SlapReply *rs )
 				continue;
 
 			for ( i = 0; b[i].bv_val; i++ ) {
-				rc = constraint_violation( cp, &b[i], op, rs );
+				rc = constraint_violation( cp, &b[i], op );
 				if ( rc ) {
 					goto mod_violation;
 				}
diff --git a/servers/slapd/overlays/dynlist.c b/servers/slapd/overlays/dynlist.c
index 749e5d6424b5e8a7bfbe2ba30d56ca5d5455a3ad..54467ebc629622031345059a4beead88e4fb233b 100644
--- a/servers/slapd/overlays/dynlist.c
+++ b/servers/slapd/overlays/dynlist.c
@@ -348,7 +348,6 @@ dynlist_prepare_entry( Operation *op, SlapReply *rs, dynlist_info_t *dli )
 	Attribute	*a, *id = NULL;
 	slap_callback	cb;
 	Operation	o = *op;
-	SlapReply	r = { REP_SEARCH };
 	struct berval	*url;
 	Entry		*e;
 	int		opattrs,
@@ -564,6 +563,7 @@ dynlist_prepare_entry( Operation *op, SlapReply *rs, dynlist_info_t *dli )
 		
 		o.o_bd = select_backend( &o.o_req_ndn, 1 );
 		if ( o.o_bd && o.o_bd->be_search ) {
+			SlapReply	r = { REP_SEARCH };
 			r.sr_attr_flags = slap_attr_flags( o.ors_attrs );
 			(void)o.o_bd->be_search( &o, &r );
 		}
diff --git a/servers/slapd/overlays/memberof.c b/servers/slapd/overlays/memberof.c
index cf1d29bcd9300569d3bb0000c98dd666146c8b18..531ff290d9ed4468c1b5d5ae80fd91fa49643e22 100644
--- a/servers/slapd/overlays/memberof.c
+++ b/servers/slapd/overlays/memberof.c
@@ -243,7 +243,6 @@ memberof_isGroupOrMember( Operation *op, memberof_cbinfo_t *mci )
 	memberof_t		*mo = (memberof_t *)on->on_bi.bi_private;
 
 	Operation		op2 = *op;
-	SlapReply		rs2 = { REP_RESULT };
 	slap_callback		cb = { 0 };
 	BackendInfo	*bi = op->o_bd->bd_info;
 	AttributeName		an[ 2 ];
@@ -276,6 +275,8 @@ memberof_isGroupOrMember( Operation *op, memberof_cbinfo_t *mci )
 	op2.ors_tlimit = SLAP_NO_LIMIT;
 
 	if ( mci->what & MEMBEROF_IS_GROUP ) {
+		SlapReply	rs2 = { REP_RESULT };
+
 		mc.ad = mo->mo_ad_member;
 		mc.foundit = 0;
 		mc.vals = NULL;
@@ -296,6 +297,8 @@ memberof_isGroupOrMember( Operation *op, memberof_cbinfo_t *mci )
 	}
 
 	if ( mci->what & MEMBEROF_IS_MEMBER ) {
+		SlapReply	rs2 = { REP_RESULT };
+
 		mc.ad = mo->mo_ad_memberof;
 		mc.foundit = 0;
 		mc.vals = NULL;
@@ -323,10 +326,9 @@ memberof_isGroupOrMember( Operation *op, memberof_cbinfo_t *mci )
 /*
  * response callback that adds memberof values when a group is modified.
  */
-static int
+static void
 memberof_value_modify(
 	Operation		*op,
-	SlapReply		*rs,
 	struct berval		*ndn,
 	AttributeDescription	*ad,
 	struct berval		*old_dn,
@@ -474,8 +476,6 @@ memberof_value_modify(
 	 * add will fail; better split in two operations, although
 	 * not optimal in terms of performance.  At least it would
 	 * move towards self-repairing capabilities. */
-
-	return rs2.sr_err;
 }
 
 static int
@@ -1257,7 +1257,7 @@ memberof_res_add( Operation *op, SlapReply *rs )
 
 				/* the modification is attempted
 				 * with the original identity */
-				(void)memberof_value_modify( op, rs,
+				memberof_value_modify( op,
 					&ma->a_nvals[ i ], mo->mo_ad_member,
 					NULL, NULL, &op->o_req_dn, &op->o_req_ndn );
 			}
@@ -1276,7 +1276,7 @@ memberof_res_add( Operation *op, SlapReply *rs )
 				if ( dn_match( &a->a_nvals[i], &op->o_req_ndn ))
 					continue;
 
-				(void)memberof_value_modify( op, rs,
+				memberof_value_modify( op,
 						&a->a_nvals[ i ],
 						mo->mo_ad_memberof,
 						NULL, NULL,
@@ -1309,7 +1309,7 @@ memberof_res_delete( Operation *op, SlapReply *rs )
 	vals = mci->member;
 	if ( vals != NULL ) {
 		for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
-			(void)memberof_value_modify( op, rs,
+			memberof_value_modify( op,
 					&vals[ i ], mo->mo_ad_memberof,
 					&op->o_req_dn, &op->o_req_ndn,
 					NULL, NULL );
@@ -1320,7 +1320,7 @@ memberof_res_delete( Operation *op, SlapReply *rs )
 		vals = mci->memberof;
 		if ( vals != NULL ) {
 			for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
-				(void)memberof_value_modify( op, rs,
+				memberof_value_modify( op,
 						&vals[ i ], mo->mo_ad_member,
 						&op->o_req_dn, &op->o_req_ndn,
 						NULL, NULL );
@@ -1366,7 +1366,7 @@ memberof_res_modify( Operation *op, SlapReply *rs )
 		case LDAP_MOD_DELETE:
 			if ( vals != NULL ) {
 				for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
-					memberof_value_modify( op, rs,
+					memberof_value_modify( op,
 							&vals[ i ], mo->mo_ad_member,
 							&op->o_req_dn, &op->o_req_ndn,
 							NULL, NULL );
@@ -1383,7 +1383,7 @@ memberof_res_modify( Operation *op, SlapReply *rs )
 			op->o_bd->bd_info = (BackendInfo *)on;
 			if ( rc == LDAP_SUCCESS ) {
 				for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
-					(void)memberof_value_modify( op, rs,
+					memberof_value_modify( op,
 							&vals[ i ], mo->mo_ad_member,
 							&op->o_req_dn, &op->o_req_ndn,
 							NULL, NULL );
@@ -1400,7 +1400,7 @@ memberof_res_modify( Operation *op, SlapReply *rs )
 			assert( vals != NULL );
 
 			for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
-				memberof_value_modify( op, rs,
+				memberof_value_modify( op,
 						&vals[ i ], mo->mo_ad_member,
 						NULL, NULL,
 						&op->o_req_dn, &op->o_req_ndn );
@@ -1424,7 +1424,7 @@ memberof_res_modify( Operation *op, SlapReply *rs )
 				vals = ml->sml_nvalues;
 				if ( vals != NULL ) {
 					for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
-						memberof_value_modify( op, rs,
+						memberof_value_modify( op,
 								&vals[ i ], mo->mo_ad_memberof,
 								&op->o_req_dn, &op->o_req_ndn,
 								NULL, NULL );
@@ -1439,7 +1439,7 @@ memberof_res_modify( Operation *op, SlapReply *rs )
 				/* delete all ... */
 				if ( vals != NULL ) {
 					for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
-						(void)memberof_value_modify( op, rs,
+						memberof_value_modify( op,
 								&vals[ i ], mo->mo_ad_memberof,
 								&op->o_req_dn, &op->o_req_ndn,
 								NULL, NULL );
@@ -1455,7 +1455,7 @@ memberof_res_modify( Operation *op, SlapReply *rs )
 				assert( ml->sml_nvalues != NULL );
 				vals = ml->sml_nvalues;
 				for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
-					memberof_value_modify( op, rs,
+					memberof_value_modify( op,
 							&vals[ i ], mo->mo_ad_memberof,
 							NULL, NULL,
 							&op->o_req_dn, &op->o_req_ndn );
@@ -1536,7 +1536,7 @@ memberof_res_modrdn( Operation *op, SlapReply *rs )
 
 		if ( rc == LDAP_SUCCESS ) {
 			for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
-				(void)memberof_value_modify( op, rs,
+				memberof_value_modify( op,
 						&vals[ i ], mo->mo_ad_memberof,
 						&op->o_req_dn, &op->o_req_ndn,
 						&newDN, &newNDN );
@@ -1553,7 +1553,7 @@ memberof_res_modrdn( Operation *op, SlapReply *rs )
 
 		if ( rc == LDAP_SUCCESS ) {
 			for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
-				(void)memberof_value_modify( op, rs,
+				memberof_value_modify( op,
 						&vals[ i ], mo->mo_ad_member,
 						&op->o_req_dn, &op->o_req_ndn,
 						&newDN, &newNDN );
diff --git a/servers/slapd/overlays/pcache.c b/servers/slapd/overlays/pcache.c
index 57b0dbcd28d80a55f0303a5cf03e524aecf5063e..68c324d86d3e21f958bb498de1d56c2834d2b104 100644
--- a/servers/slapd/overlays/pcache.c
+++ b/servers/slapd/overlays/pcache.c
@@ -346,7 +346,6 @@ add_query(
 static int
 remove_query_data(
 	Operation	*op,
-	SlapReply	*rs,
 	struct berval	*query_uuid );
 
 /*
@@ -754,11 +753,10 @@ url2query(
 	/* ignore expired queries */
 	if ( expiry_time <= slap_get_time()) {
 		Operation	op2 = *op;
-		SlapReply	rs2 = { 0 };
 
 		memset( &op2.oq_search, 0, sizeof( op2.oq_search ) );
 
-		(void)remove_query_data( &op2, &rs2, &uuid );
+		(void)remove_query_data( &op2, &uuid );
 
 		rc = 0;
 
@@ -867,6 +865,7 @@ merge_entry(
 
 	if ( rc != LDAP_SUCCESS ) {
 		if ( rc == LDAP_ALREADY_EXISTS ) {
+			rs_reinit( &sreply, REP_RESULT );
 			slap_entry2mods( e, &modlist, &text, textbuf, textlen );
 			modlist->sml_op = LDAP_MOD_ADD;
 			op->o_tag = LDAP_REQ_MODIFY;
@@ -1750,7 +1749,6 @@ remove_func (
 static int
 remove_query_data(
 	Operation	*op,
-	SlapReply	*rs,
 	struct berval	*query_uuid )
 {
 	struct query_info	*qi, *qnext;
@@ -1761,8 +1759,6 @@ remove_query_data(
 	slap_callback cb = { NULL, remove_func, NULL, NULL };
 	int deleted = 0;
 
-	sreply.sr_entry = NULL;
-	sreply.sr_nentries = 0;
 	op->ors_filterstr.bv_len = snprintf(filter_str, sizeof(filter_str),
 		"(%s=%s)", ad_queryId->ad_cname.bv_val, query_uuid->bv_val);
 	filter.f_ava = &ava;
@@ -1795,6 +1791,7 @@ remove_query_data(
 
 		op->o_req_dn = qi->xdn;
 		op->o_req_ndn = qi->xdn;
+		rs_reinit( &sreply, REP_RESULT );
 
 		if ( qi->del ) {
 			Debug( pcache_debug, "DELETING ENTRY TEMPLATE=%s\n",
@@ -1973,7 +1970,6 @@ struct search_info {
 static void
 remove_query_and_data(
 	Operation	*op,
-	SlapReply	*rs,
 	cache_manager	*cm,
 	struct berval	*uuid )
 {
@@ -1986,7 +1982,7 @@ remove_query_and_data(
 		Debug( pcache_debug,
 			"Removing query UUID %s\n",
 			uuid->bv_val, 0, 0 );
-		return_val = remove_query_data( op, rs, uuid );
+		return_val = remove_query_data( op, uuid );
 		Debug( pcache_debug,
 			"QUERY REMOVED, SIZE=%d\n",
 			return_val, 0, 0);
@@ -2036,11 +2032,7 @@ fetch_queryId_cb( Operation *op, SlapReply *rs )
 	}
 
 	/* clear entry if required */
-	if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
-		entry_free( rs->sr_entry );
-		rs->sr_entry = NULL;
-		rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
-	}
+	rs_flush_entry( op, rs, (slap_overinst *) op->o_bd->bd_info );
 
 	return rc;
 }
@@ -2059,7 +2051,6 @@ pcache_remove_entries_from_cache(
 	OperationBuffer opbuf;
 	Operation	op2;
 	slap_callback	sc = { 0 };
-	SlapReply	rs = { REP_RESULT };
 	Filter		f = { 0 };
 	char		filtbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(entryUUID=)" ) ];
 	AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
@@ -2106,6 +2097,7 @@ pcache_remove_entries_from_cache(
 
 	for ( s = 0; !BER_BVISNULL( &entryUUIDs[ s ] ); s++ ) {
 		BerVarray	vals = NULL;
+		SlapReply	rs = { REP_RESULT };
 
 		op->ors_filterstr.bv_len = snprintf( filtbuf, sizeof( filtbuf ),
 			"(entryUUID=%s)", entryUUIDs[ s ].bv_val );
@@ -2124,7 +2116,7 @@ pcache_remove_entries_from_cache(
 			for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
 				struct berval	val = vals[ i ];
 
-				remove_query_and_data( op, &rs, cm, &val );
+				remove_query_and_data( op, cm, &val );
 
 				if ( !BER_BVISNULL( &val ) && val.bv_val != vals[ i ].bv_val ) {
 					ch_free( val.bv_val );
@@ -2149,12 +2141,11 @@ pcache_remove_query_from_cache(
 	struct berval	*queryid )
 {
 	Operation	op2 = *op;
-	SlapReply	rs2 = { 0 };
 
 	op2.o_bd = &cm->db;
 
 	/* remove the selected query */
-	remove_query_and_data( &op2, &rs2, cm, queryid );
+	remove_query_and_data( &op2, cm, queryid );
 
 	return LDAP_SUCCESS;
 }
@@ -2245,7 +2236,7 @@ pcache_remove_entry_queries_from_cache(
 		for ( i = 0; !BER_BVISNULL( &vals[ i ] ); i++ ) {
 			struct berval	val = vals[ i ];
 
-			remove_query_and_data( op, &rs, cm, &val );
+			remove_query_and_data( op, cm, &val );
 
 			if ( !BER_BVISNULL( &val ) && val.bv_val != vals[ i ].bv_val ) {
 				ch_free( val.bv_val );
@@ -2261,7 +2252,6 @@ pcache_remove_entry_queries_from_cache(
 static int
 cache_entries(
 	Operation	*op,
-	SlapReply	*rs,
 	struct berval *query_uuid )
 {
 	struct search_info *si = op->o_callback->sc_private;
@@ -2293,7 +2283,7 @@ cache_entries(
 		e->e_private = NULL;
 		while ( cm->cur_entries > (cm->max_entries) ) {
 			BER_BVZERO( &crp_uuid );
-			remove_query_and_data( op_tmp, rs, cm, &crp_uuid );
+			remove_query_and_data( op_tmp, cm, &crp_uuid );
 		}
 
 		return_val = merge_entry(op_tmp, e, 0, query_uuid);
@@ -2401,7 +2391,7 @@ over:;
 			if ( qc != NULL ) {
 				switch ( si->caching_reason ) {
 				case PC_POSITIVE:
-					cache_entries( op, rs, &qc->q_uuid );
+					cache_entries( op, &qc->q_uuid );
 					if ( si->pbi )
 						si->pbi->bi_cq = qc;
 					break;
@@ -2618,7 +2608,7 @@ pc_bind_attrs( Operation *op, Entry *e, QueryTemplate *temp,
 {
 	int i, len = 0;
 	struct berval *vals, pres = BER_BVC("*");
-	char *p1, *p2, *t1;
+	char *p1, *p2;
 	Attribute *a;
 
 	vals = op->o_tmpalloc( temp->bindnattrs * sizeof( struct berval ),
@@ -3226,7 +3216,6 @@ refresh_merge( Operation *op, SlapReply *rs )
 {
 	if ( rs->sr_type == REP_SEARCH ) {
 		refresh_info *ri = op->o_callback->sc_private;
-		BackendDB *be = op->o_bd;
 		Entry *e;
 		dnlist *dnl;
 		slap_callback *ocb;
@@ -3273,6 +3262,7 @@ refresh_merge( Operation *op, SlapReply *rs )
 			slap_mods_free( modlist, 1 );
 			/* mods is NULL if there are no changes */
 			if ( mods ) {
+				SlapReply rs2 = { REP_RESULT };
 				struct berval dn = op->o_req_dn;
 				struct berval ndn = op->o_req_ndn;
 				op->o_tag = LDAP_REQ_MODIFY;
@@ -3280,7 +3270,9 @@ refresh_merge( Operation *op, SlapReply *rs )
 				op->o_req_dn = rs->sr_entry->e_name;
 				op->o_req_ndn = rs->sr_entry->e_nname;
 				op->o_callback = &cb;
-				op->o_bd->be_modify( op, rs );
+				op->o_bd->be_modify( op, &rs2 );
+				rs->sr_err = rs2.sr_err;
+				rs_assert_done( &rs2 );
 				slap_mods_free( mods, 1 );
 				op->o_req_dn = dn;
 				op->o_req_ndn = ndn;
@@ -3334,9 +3326,9 @@ refresh_purge( Operation *op, SlapReply *rs )
 }
 
 static int
-refresh_query( Operation *op, SlapReply *rs, CachedQuery *query,
-	slap_overinst *on )
+refresh_query( Operation *op, CachedQuery *query, slap_overinst *on )
 {
+	SlapReply rs = {REP_RESULT};
 	slap_callback cb = { 0 };
 	refresh_info ri = { 0 };
 	char filter_str[ LDAP_LUTIL_UUIDSTR_BUFSIZE + STRLENOF( "(pcacheQueryID=)" ) ];
@@ -3374,7 +3366,7 @@ refresh_query( Operation *op, SlapReply *rs, CachedQuery *query,
 	op->ors_attrsonly = 0;
 
 	op->o_bd = on->on_info->oi_origdb;
-	rc = op->o_bd->be_search( op, rs );
+	rc = op->o_bd->be_search( op, &rs );
 	if ( rc ) {
 		op->o_bd = ri.ri_be;
 		goto leave;
@@ -3397,17 +3389,17 @@ refresh_query( Operation *op, SlapReply *rs, CachedQuery *query,
 	attrs[ 0 ].an_name = ad_queryId->ad_cname;
 	op->ors_attrs = attrs;
 	op->ors_attrsonly = 0;
-	rs->sr_entry = NULL;
-	rs->sr_nentries = 0;
-	rc = op->o_bd->be_search( op, rs );
+	rs_reinit( &rs, REP_RESULT );
+	rc = op->o_bd->be_search( op, &rs );
 	if ( rc ) goto leave;
 
 	while (( dn = ri.ri_dels )) {
 		op->o_req_dn = dn->dn;
 		op->o_req_ndn = dn->dn;
+		rs_reinit( &rs, REP_RESULT );
 		if ( dn->delete ) {
 			op->o_tag = LDAP_REQ_DELETE;
-			op->o_bd->be_delete( op, rs );
+			op->o_bd->be_delete( op, &rs );
 		} else {
 			Modifications mod;
 			struct berval vals[2];
@@ -3425,7 +3417,7 @@ refresh_query( Operation *op, SlapReply *rs, CachedQuery *query,
 
 			op->o_tag = LDAP_REQ_MODIFY;
 			op->orm_modlist = &mod;
-			op->o_bd->be_modify( op, rs );
+			op->o_bd->be_modify( op, &rs );
 		}
 		ri.ri_dels = dn->next;
 		op->o_tmpfree( dn, op->o_tmpmemctx );
@@ -3450,7 +3442,6 @@ consistency_check(
 	OperationBuffer opbuf;
 	Operation *op;
 
-	SlapReply rs = {REP_RESULT};
 	CachedQuery *query, *qprev;
 	int return_val, pause = PCACHE_CC_PAUSED;
 	QueryTemplate *templ;
@@ -3496,7 +3487,7 @@ consistency_check(
 				if ( query->refcnt )
 					query->expiry_time = op->o_time + templ->ttl;
 				if ( query->expiry_time > op->o_time ) {
-					refresh_query( op, &rs, query, on );
+					refresh_query( op, query, on );
 					continue;
 				}
 			}
@@ -3524,7 +3515,7 @@ consistency_check(
 				if ( BER_BVISNULL( &query->q_uuid ))
 					return_val = 0;
 				else
-					return_val = remove_query_data(op, &rs, &query->q_uuid);
+					return_val = remove_query_data(op, &query->q_uuid);
 				Debug( pcache_debug, "STALE QUERY REMOVED, SIZE=%d\n",
 							return_val, 0, 0 );
 				ldap_pvt_thread_mutex_lock(&cm->cache_mutex);
@@ -4533,7 +4524,7 @@ pcache_db_open2(
 			OperationBuffer	opbuf;
 			Operation	*op;
 			slap_callback	cb = { 0 };
-			SlapReply	rs = { 0 };
+			SlapReply	rs = { REP_RESULT };
 			BerVarray	vals = NULL;
 			Filter		f = { 0 }, f2 = { 0 };
 			AttributeAssertion	ava = ATTRIBUTEASSERTION_INIT;
@@ -4598,8 +4589,8 @@ pcache_db_open2(
 			op->ors_scope = LDAP_SCOPE_SUBTREE;
 			op->ors_attrs = slap_anlist_no_attrs;
 
+			rs_reinit( &rs, REP_RESULT );
 			op->o_callback->sc_response = pcache_cachedquery_count_cb;
-			rs.sr_nentries = 0;
 			op->o_callback->sc_private = &rs.sr_nentries;
 
 			rc = op->o_bd->be_search( op, &rs );
diff --git a/servers/slapd/overlays/refint.c b/servers/slapd/overlays/refint.c
index e5cdd722b4e539eb41ee164f9fbaa556dff8d7b6..c27b506a209dedd52d51dcdd5c97d85d057a6ba1 100644
--- a/servers/slapd/overlays/refint.c
+++ b/servers/slapd/overlays/refint.c
@@ -521,11 +521,11 @@ refint_search_cb(
 static int
 refint_repair(
 	Operation	*op,
-	SlapReply	*rs,
 	refint_data	*id,
 	refint_q	*rq )
 {
 	dependent_data	*dp;
+	SlapReply		rs = {REP_RESULT};
 	int		rc;
 
 	op->o_callback->sc_response = refint_search_cb;
@@ -535,7 +535,7 @@ refint_repair(
 	op->o_ndn = op->o_bd->be_rootndn;
 
 	/* search */
-	rc = op->o_bd->be_search( op, rs );
+	rc = op->o_bd->be_search( op, &rs );
 
 	if ( rc != LDAP_SUCCESS ) {
 		Debug( LDAP_DEBUG_TRACE,
@@ -671,7 +671,8 @@ refint_repair(
 		op2.o_dn = op2.o_bd->be_rootdn;
 		op2.o_ndn = op2.o_bd->be_rootndn;
 		slap_op_time( &op2.o_time, &op2.o_tincr );
-		if ( ( rc = op2.o_bd->be_modify( &op2, &rs2 ) ) != LDAP_SUCCESS ) {
+		rc = op2.o_bd->be_modify( &op2, &rs2 );
+		if ( rc != LDAP_SUCCESS ) {
 			Debug( LDAP_DEBUG_TRACE,
 				"refint_repair: dependent modify failed: %d\n",
 				rs2.sr_err, 0, 0 );
@@ -694,7 +695,6 @@ refint_qtask( void *ctx, void *arg )
 	Connection conn = {0};
 	OperationBuffer opbuf;
 	Operation *op;
-	SlapReply rs = {REP_RESULT};
 	slap_callback cb = { NULL, NULL, NULL, NULL };
 	Filter ftop, *fptr;
 	refint_q *rq;
@@ -771,7 +771,7 @@ refint_qtask( void *ctx, void *arg )
 
 		if ( rq->db != NULL ) {
 			op->o_bd = rq->db;
-			refint_repair( op, &rs, id, rq );
+			refint_repair( op, id, rq );
 
 		} else {
 			BackendDB	*be;
@@ -784,7 +784,7 @@ refint_qtask( void *ctx, void *arg )
 
 				if ( be->be_search && be->be_modify ) {
 					op->o_bd = be;
-					refint_repair( op, &rs, id, rq );
+					refint_repair( op, id, rq );
 				}
 			}
 		}
diff --git a/servers/slapd/overlays/retcode.c b/servers/slapd/overlays/retcode.c
index ae29cb918797b7816fa5b7d2b6ac6b193944792d..4dd568324df2025d446f9aec3f86c272d00f55d6 100644
--- a/servers/slapd/overlays/retcode.c
+++ b/servers/slapd/overlays/retcode.c
@@ -165,7 +165,9 @@ retcode_send_onelevel( Operation *op, SlapReply *rs )
 			rs->sr_entry = &rdi->rdi_e;
 
 			rs->sr_err = send_search_entry( op, rs );
+			rs->sr_flags = 0;
 			rs->sr_entry = NULL;
+			rs->sr_attrs = NULL;
 
 			switch ( rs->sr_err ) {
 			case LDAP_UNAVAILABLE:	/* connection closed */
diff --git a/servers/slapd/overlays/rwm.c b/servers/slapd/overlays/rwm.c
index fd2cacada663fea9e5e439045b60c23418287cdb..c724be2b9dd3ef39fd326f8f149adf6fb71491b7 100644
--- a/servers/slapd/overlays/rwm.c
+++ b/servers/slapd/overlays/rwm.c
@@ -172,7 +172,7 @@ rwm_op_cleanup( Operation *op, SlapReply *rs )
 }
 
 static rwm_op_cb *
-rwm_callback_get( Operation *op, SlapReply *rs )
+rwm_callback_get( Operation *op )
 {
 	rwm_op_cb	*roc;
 
@@ -270,7 +270,7 @@ rwm_op_add( Operation *op, SlapReply *rs )
 	char			*olddn = op->o_req_dn.bv_val;
 	int			isupdate;
 
-	rwm_op_cb		*roc = rwm_callback_get( op, rs );
+	rwm_op_cb		*roc = rwm_callback_get( op );
 
 	rc = rwm_op_dn_massage( op, rs, "addDN", &roc->ros );
 	if ( rc != LDAP_SUCCESS ) {
@@ -413,7 +413,7 @@ rwm_op_bind( Operation *op, SlapReply *rs )
 	slap_overinst		*on = (slap_overinst *) op->o_bd->bd_info;
 	int			rc;
 
-	rwm_op_cb		*roc = rwm_callback_get( op, rs );
+	rwm_op_cb		*roc = rwm_callback_get( op );
 
 	rc = rwm_op_dn_massage( op, rs, "bindDN", &roc->ros );
 	if ( rc != LDAP_SUCCESS ) {
@@ -449,7 +449,7 @@ rwm_op_compare( Operation *op, SlapReply *rs )
 	int			rc;
 	struct berval		mapped_vals[2] = { BER_BVNULL, BER_BVNULL };
 
-	rwm_op_cb		*roc = rwm_callback_get( op, rs );
+	rwm_op_cb		*roc = rwm_callback_get( op );
 
 	rc = rwm_op_dn_massage( op, rs, "compareDN", &roc->ros );
 	if ( rc != LDAP_SUCCESS ) {
@@ -534,7 +534,7 @@ rwm_op_delete( Operation *op, SlapReply *rs )
 	slap_overinst		*on = (slap_overinst *) op->o_bd->bd_info;
 	int			rc;
 
-	rwm_op_cb		*roc = rwm_callback_get( op, rs );
+	rwm_op_cb		*roc = rwm_callback_get( op );
 
 	rc = rwm_op_dn_massage( op, rs, "deleteDN", &roc->ros );
 	if ( rc != LDAP_SUCCESS ) {
@@ -559,7 +559,7 @@ rwm_op_modify( Operation *op, SlapReply *rs )
 	Modifications		**mlp;
 	int			rc;
 
-	rwm_op_cb		*roc = rwm_callback_get( op, rs );
+	rwm_op_cb		*roc = rwm_callback_get( op );
 
 	rc = rwm_op_dn_massage( op, rs, "modifyDN", &roc->ros );
 	if ( rc != LDAP_SUCCESS ) {
@@ -722,7 +722,7 @@ rwm_op_modrdn( Operation *op, SlapReply *rs )
 	int			rc;
 	dncookie		dc;
 
-	rwm_op_cb		*roc = rwm_callback_get( op, rs );
+	rwm_op_cb		*roc = rwm_callback_get( op );
 
 	if ( op->orr_newSup ) {
 		struct berval	nnewSup = BER_BVNULL;
@@ -866,12 +866,7 @@ rwm_entry_get_rw( Operation *op, struct berval *ndn,
 	ObjectClass *oc, AttributeDescription *at, int rw, Entry **ep )
 {
 	slap_overinst		*on = (slap_overinst *) op->o_bd->bd_info;
-	struct ldaprwmap	*rwmap = 
-			(struct ldaprwmap *)on->on_bi.bi_private;
-
 	int			rc;
-	dncookie		dc;
-
 	BackendDB		db;
 	Operation		op2;
 	SlapReply		rs = { REP_SEARCH };
@@ -952,7 +947,7 @@ rwm_op_search( Operation *op, SlapReply *rs )
 
 	char			*text = NULL;
 
-	rwm_op_cb		*roc = rwm_callback_get( op, rs );
+	rwm_op_cb		*roc = rwm_callback_get( op );
 
 	rc = rewrite_session_var_set( rwmap->rwm_rw, op->o_conn,
 		"searchFilter", op->ors_filterstr.bv_val );
@@ -1071,7 +1066,7 @@ rwm_exop_passwd( Operation *op, SlapReply *rs )
 		ber_dupbv_x( &op->o_req_ndn, &op->o_ndn, op->o_tmpmemctx );
 	}
 
-	roc = rwm_callback_get( op, rs );
+	roc = rwm_callback_get( op );
 
 	rc = rwm_op_dn_massage( op, rs, "extendedDN", &roc->ros );
 	if ( rc != LDAP_SUCCESS ) {
@@ -1143,7 +1138,7 @@ rwm_extended( Operation *op, SlapReply *rs )
 		}
 	}
 
-	roc = rwm_callback_get( op, rs );
+	roc = rwm_callback_get( op );
 
 	rc = rwm_op_dn_massage( op, rs, "extendedDN", &roc->ros );
 	if ( rc != LDAP_SUCCESS ) {
@@ -2035,6 +2030,7 @@ rwm_bva_rewrite_add(
 	return rwm_bva_add( &rwmap->rwm_bva_rewrite, idx, argv );
 }
 
+#ifdef unused
 static int
 rwm_bva_map_add(
 	struct ldaprwmap	*rwmap,
@@ -2043,6 +2039,7 @@ rwm_bva_map_add(
 {
 	return rwm_bva_add( &rwmap->rwm_bva_map, idx, argv );
 }
+#endif /* unused */
 
 static int
 rwm_info_init( struct rewrite_info ** rwm_rw )
diff --git a/servers/slapd/overlays/syncprov.c b/servers/slapd/overlays/syncprov.c
index 7207e17c868fa7761d4fafec8ebadc7399fe9b1a..8ca9f13cc38a19e294db28c3629e25b32bdc31d2 100644
--- a/servers/slapd/overlays/syncprov.c
+++ b/servers/slapd/overlays/syncprov.c
@@ -736,6 +736,7 @@ again:
 			/* If we didn't find an exact match, then try for <= */
 			if ( findcsn_retry ) {
 				findcsn_retry = 0;
+				rs_reinit( &frs, REP_RESULT );
 				goto again;
 			}
 			rc = LDAP_NO_SUCH_OBJECT;
@@ -819,8 +820,6 @@ syncprov_free_syncop( syncops *so )
 static int
 syncprov_sendresp( Operation *op, opcookie *opc, syncops *so, int mode )
 {
-	slap_overinst *on = opc->son;
-
 	SlapReply rs = { REP_SEARCH };
 	LDAPControl *ctrls[2];
 	struct berval cookie = BER_BVNULL, csns[2];
@@ -916,7 +915,6 @@ syncprov_qplay( Operation *op, syncops *so )
 {
 	slap_overinst *on = LDAP_SLIST_FIRST(&so->s_op->o_extra)->oe_key;
 	syncres *sr;
-	Entry *e;
 	opcookie opc;
 	int rc = 0;
 
@@ -1378,7 +1376,7 @@ syncprov_op_cleanup( Operation *op, SlapReply *rs )
 	slap_overinst *on = opc->son;
 	syncprov_info_t		*si = on->on_bi.bi_private;
 	syncmatches *sm, *snext;
-	modtarget *mt, mtdummy;
+	modtarget *mt;
 
 	ldap_pvt_thread_mutex_lock( &si->si_ops_mutex );
 	if ( si->si_active )
@@ -1422,12 +1420,12 @@ syncprov_op_cleanup( Operation *op, SlapReply *rs )
 }
 
 static void
-syncprov_checkpoint( Operation *op, SlapReply *rs, slap_overinst *on )
+syncprov_checkpoint( Operation *op, slap_overinst *on )
 {
 	syncprov_info_t *si = (syncprov_info_t *)on->on_bi.bi_private;
 	Modifications mod;
 	Operation opm;
-	SlapReply rsm = { 0 };
+	SlapReply rsm = {REP_RESULT};
 	slap_callback cb = {0};
 	BackendDB be;
 	BackendInfo *bi;
@@ -1472,6 +1470,7 @@ syncprov_checkpoint( Operation *op, SlapReply *rs, slap_overinst *on )
 		char txtbuf[SLAP_TEXT_BUFLEN];
 		size_t textlen = sizeof txtbuf;
 		Entry *e = slap_create_context_csn_entry( opm.o_bd, NULL );
+		rs_reinit( &rsm, REP_RESULT );
 		slap_mods2entry( &mod, &e, 0, 1, &text, txtbuf, textlen);
 		opm.ora_e = e;
 		opm.o_bd->be_add( &opm, &rsm );
@@ -1687,7 +1686,6 @@ syncprov_playlog( Operation *op, SlapReply *rs, sessionlog *sl,
 
 	if ( mmods ) {
 		Operation fop;
-		SlapReply frs = { REP_RESULT };
 		int rc;
 		Filter mf, af;
 		AttributeAssertion eq = ATTRIBUTEASSERTION_INIT;
@@ -1717,18 +1715,19 @@ syncprov_playlog( Operation *op, SlapReply *rs, sessionlog *sl,
 		fop.o_bd->bd_info = (BackendInfo *)on->on_info;
 
 		for ( i=ndel; i<num; i++ ) {
-			if ( uuids[i].bv_len == 0 ) continue;
+		  if ( uuids[i].bv_len != 0 ) {
+			SlapReply frs = { REP_RESULT };
 
 			mf.f_av_value = uuids[i];
 			cb.sc_private = NULL;
 			fop.ors_slimit = 1;
-			frs.sr_nentries = 0;
 			rc = fop.o_bd->be_search( &fop, &frs );
 
 			/* If entry was not found, add to delete list */
 			if ( !cb.sc_private ) {
 				uuids[ndel++] = uuids[i];
 			}
+		  }
 		}
 		fop.o_bd->bd_info = (BackendInfo *)on;
 	}
@@ -1902,7 +1901,7 @@ syncprov_op_response( Operation *op, SlapReply *rs )
 
 		if ( do_check ) {
 			ldap_pvt_thread_rdwr_rlock( &si->si_csn_rwlock );
-			syncprov_checkpoint( op, rs, on );
+			syncprov_checkpoint( op, on );
 			ldap_pvt_thread_rdwr_runlock( &si->si_csn_rwlock );
 		}
 
@@ -2763,17 +2762,7 @@ syncprov_operational(
 				}
 
 				if ( !ap ) {
-					if ( !(rs->sr_flags & REP_ENTRY_MODIFIABLE) ) {
-						Entry *e = entry_dup( rs->sr_entry );
-						if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
-							overlay_entry_release_ov( op, rs->sr_entry, 0, on );
-							rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
-						} else if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
-							entry_free( rs->sr_entry );
-						}
-						rs->sr_entry = e;
-						rs->sr_flags |=
-							REP_ENTRY_MODIFIABLE|REP_ENTRY_MUSTBEFREED;
+					if ( rs_ensure_entry_modifiable( op, rs, on )) {
 						a = attr_find( rs->sr_entry->e_attrs,
 							slap_schema.si_ad_contextCSN );
 					}
@@ -3123,7 +3112,6 @@ syncprov_db_close(
 		Connection conn = {0};
 		OperationBuffer opbuf;
 		Operation *op;
-		SlapReply rs = {REP_RESULT};
 		void *thrctx;
 
 		thrctx = ldap_pvt_thread_pool_context();
@@ -3132,7 +3120,7 @@ syncprov_db_close(
 		op->o_bd = be;
 		op->o_dn = be->be_rootdn;
 		op->o_ndn = be->be_rootndn;
-		syncprov_checkpoint( op, &rs, on );
+		syncprov_checkpoint( op, on );
 	}
 
     return 0;
diff --git a/servers/slapd/overlays/translucent.c b/servers/slapd/overlays/translucent.c
index 8bb6caf5e9897a18c3168b71fc0ffa7e4ac9718c..e37d7dcffa7dbb283034d6322473cb6aa1162b4b 100644
--- a/servers/slapd/overlays/translucent.c
+++ b/servers/slapd/overlays/translucent.c
@@ -724,8 +724,6 @@ static int translucent_pwmod(Operation *op, SlapReply *rs) {
 }
 
 static int translucent_exop(Operation *op, SlapReply *rs) {
-	SlapReply nrs = { REP_RESULT };
-
 	slap_overinst *on = (slap_overinst *) op->o_bd->bd_info;
 	translucent_info *ov = on->on_bi.bi_private;
 	const struct berval bv_exop_pwmod = BER_BVC(LDAP_EXOP_MODIFY_PASSWD);
@@ -817,14 +815,7 @@ static int translucent_search_cb(Operation *op, SlapReply *rs) {
 		if ( tc->step & USE_LIST ) {
 			re = tavl_delete( &tc->list, le, entry_dn_cmp );
 			if ( re ) {
-				if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
-					rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
-					overlay_entry_release_ov( op, rs->sr_entry, 0, on );
-				}
-				if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
-					rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
-					entry_free( rs->sr_entry );
-				}
+				rs_flush_entry( op, rs, on );
 				rc = test_filter( op, re, tc->orig );
 				if ( rc == LDAP_COMPARE_TRUE ) {
 					rs->sr_flags |= REP_ENTRY_MUSTBEFREED;
@@ -857,14 +848,7 @@ static int translucent_search_cb(Operation *op, SlapReply *rs) {
 		rc = overlay_entry_get_ov(op, &rs->sr_entry->e_nname, NULL, NULL, 0, &le, on);
 		if ( rc == LDAP_SUCCESS && le ) {
 			re = entry_dup( rs->sr_entry );
-			if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
-				rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
-				overlay_entry_release_ov( op, rs->sr_entry, 0, on );
-			}
-			if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
-				rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
-				entry_free( rs->sr_entry );
-			}
+			rs_flush_entry( op, rs, on );
 		} else {
 			le = NULL;
 		}
@@ -904,14 +888,7 @@ static int translucent_search_cb(Operation *op, SlapReply *rs) {
 		}
 		/* Dispose of local entry */
 		if ( tc->step & LCL_SIDE ) {
-			if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
-				rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
-				overlay_entry_release_ov( op, rs->sr_entry, 0, on );
-			}
-			if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
-				rs->sr_flags ^= REP_ENTRY_MUSTBEFREED;
-				entry_free( rs->sr_entry );
-			}
+			rs_flush_entry(op, rs, on);
 		} else {
 			overlay_entry_release_ov(op, le, 0, on);
 		}
@@ -1171,6 +1148,7 @@ static int translucent_search(Operation *op, SlapReply *rs) {
 				av = tavl_next( av, TAVL_DIR_RIGHT );
 			}
 			tavl_free( tc.list, NULL );
+			rs->sr_flags = 0;
 			rs->sr_entry = NULL;
 		}
 		send_ldap_result( op, rs );
diff --git a/servers/slapd/overlays/valsort.c b/servers/slapd/overlays/valsort.c
index 6a02a1704f1c084854a161584d87f051ddd686d1..a09e32bbf04bbe18e8c03088118e25bb142094d7 100644
--- a/servers/slapd/overlays/valsort.c
+++ b/servers/slapd/overlays/valsort.c
@@ -297,20 +297,7 @@ valsort_response( Operation *op, SlapReply *rs )
 		a = attr_find( rs->sr_entry->e_attrs, vi->vi_ad );
 		if ( !a ) continue;
 
-		if (( rs->sr_flags & ( REP_ENTRY_MODIFIABLE|REP_ENTRY_MUSTBEFREED ) ) !=
-			( REP_ENTRY_MODIFIABLE|REP_ENTRY_MUSTBEFREED ) )
-		{
-			Entry *e;
-
-			e = entry_dup( rs->sr_entry );
-			if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
-				overlay_entry_release_ov( op, rs->sr_entry, 0, on );
-				rs->sr_flags &= ~REP_ENTRY_MUSTRELEASE;
-			} else if ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
-				entry_free( rs->sr_entry );
-			}
-			rs->sr_entry = e;
-			rs->sr_flags |= REP_ENTRY_MODIFIABLE|REP_ENTRY_MUSTBEFREED;
+		if ( rs_ensure_entry_modifiable( op, rs, on )) {
 			a = attr_find( rs->sr_entry->e_attrs, vi->vi_ad );
 		}
 
diff --git a/servers/slapd/proto-slap.h b/servers/slapd/proto-slap.h
index e3c952a7f2fe6e7ee59b96ce8abca28b506ec4d3..6bf6afd902f8fd221fd6a538a9089181e887da36 100644
--- a/servers/slapd/proto-slap.h
+++ b/servers/slapd/proto-slap.h
@@ -1526,6 +1526,40 @@ LDAP_SLAPD_F (int) get_alias_dn LDAP_P((
 /*
  * result.c
  */
+#if USE_RS_ASSERT /*defined(USE_RS_ASSERT)?(USE_RS_ASSERT):defined(LDAP_TEST)*/
+#ifdef __GNUC__
+# define RS_FUNC_	__FUNCTION__
+#elif defined(__STDC_VERSION__) && (__STDC_VERSION__) >= 199901L
+# define RS_FUNC_	__func__
+#else
+# define rs_assert_(file, line, func, cond) rs_assert__(file, line, cond)
+#endif
+LDAP_SLAPD_V(int)  rs_suppress_assert;
+LDAP_SLAPD_F(void) rs_assert_(const char*, unsigned, const char*, const char*);
+# define RS_ASSERT(cond)		((rs_suppress_assert > 0 || (cond)) \
+	? (void) 0 : rs_assert_(__FILE__, __LINE__, RS_FUNC_, #cond))
+#else
+# define RS_ASSERT(cond)		((void) 0)
+# define rs_assert_ok(rs)		((void) (rs))
+# define rs_assert_ready(rs)	((void) (rs))
+# define rs_assert_done(rs)		((void) (rs))
+#endif
+LDAP_SLAPD_F (void) (rs_assert_ok)		LDAP_P(( const SlapReply *rs ));
+LDAP_SLAPD_F (void) (rs_assert_ready)	LDAP_P(( const SlapReply *rs ));
+LDAP_SLAPD_F (void) (rs_assert_done)	LDAP_P(( const SlapReply *rs ));
+
+#define rs_reinit(rs, type)	do {			\
+		SlapReply *const rsRI = (rs);		\
+		rs_assert_done( rsRI );				\
+		rsRI->sr_type = (type);				\
+		/* Got type before memset in case of rs_reinit(rs, rs->sr_type) */ \
+		assert( !offsetof( SlapReply, sr_type ) );	\
+		memset( (slap_reply_t *) rsRI + 1, 0,		\
+			sizeof(*rsRI) - sizeof(slap_reply_t) );	\
+	} while ( 0 )
+LDAP_SLAPD_F (void) (rs_reinit)	LDAP_P(( SlapReply *rs, slap_reply_t type ));
+LDAP_SLAPD_F (void) rs_flush_entry LDAP_P(( Operation *op,
+	SlapReply *rs, slap_overinst *on ));
 LDAP_SLAPD_F (void) rs_replace_entry LDAP_P(( Operation *op,
 	SlapReply *rs, slap_overinst *on, Entry *e ));
 LDAP_SLAPD_F (int) rs_ensure_entry_modifiable LDAP_P(( Operation *op,
@@ -2131,4 +2165,3 @@ LDAP_SLAPD_F (int) fe_access_allowed LDAP_P((
 LDAP_END_DECL
 
 #endif /* PROTO_SLAP_H */
-
diff --git a/servers/slapd/result.c b/servers/slapd/result.c
index 421fdbc1af41deea2f958b8c54e6292d2b534957..4161a0d3b7c5edb971281503441493440137d82b 100644
--- a/servers/slapd/result.c
+++ b/servers/slapd/result.c
@@ -132,30 +132,133 @@ slap_req2res( ber_tag_t tag )
 	return tag;
 }
 
-#ifdef RS_ASSERT
-#elif 0 && defined LDAP_DEVEL /* FIXME: this should not crash. ITS#5340. */
-#define RS_ASSERT assert
+/* SlapReply debugging, prodo-slap.h overrides it in OpenLDAP releases */
+#if defined(LDAP_TEST) || (defined(USE_RS_ASSERT) && (USE_RS_ASSERT))
+
+int rs_suppress_assert = 0;
+
+/* RS_ASSERT() helper function */
+void rs_assert_(const char*file, unsigned line, const char*fn, const char*cond)
+{
+	int no_assert = rs_suppress_assert, save_errno = errno;
+	const char *s;
+
+	if ( no_assert >= 0 ) {
+		if ( no_assert == 0 && (s = getenv( "NO_RS_ASSERT" )) && *s ) {
+			no_assert = rs_suppress_assert = atoi( s );
+		}
+		if ( no_assert > 0 ) {
+			errno = save_errno;
+			return;
+		}
+	}
+
+#ifdef rs_assert_	/* proto-slap.h #defined away the fn parameter */
+	fprintf( stderr,"%s:%u: "  "RS_ASSERT(%s) failed.\n", file,line,cond );
 #else
-#define RS_ASSERT(cond) ((void) 0)
+	fprintf( stderr,"%s:%u: %s: RS_ASSERT(%s) failed.\n", file,line,fn,cond );
 #endif
+	fflush( stderr );
+
+	errno = save_errno;
+	/* $NO_RS_ASSERT > 0: ignore rs_asserts, 0: abort, < 0: just warn */
+	if ( !no_assert /* from $NO_RS_ASSERT */ ) abort();
+}
 
-/* Set rs->sr_entry after obyeing and clearing sr_flags & REP_ENTRY_MASK. */
+/* SlapReply is consistent */
 void
-rs_replace_entry( Operation *op, SlapReply *rs, slap_overinst *on, Entry *e )
+(rs_assert_ok)( const SlapReply *rs )
+{
+	const slap_mask_t flags = rs->sr_flags;
+
+	if ( flags & REP_ENTRY_MASK ) {
+		RS_ASSERT( !(flags & REP_ENTRY_MUSTRELEASE)
+			|| !(flags & (REP_ENTRY_MASK ^ REP_ENTRY_MUSTRELEASE)) );
+		RS_ASSERT( rs->sr_entry != NULL );
+		RS_ASSERT( (1 << rs->sr_type) &
+			((1 << REP_SEARCH) | (1 << REP_SEARCHREF) |
+			 (1 << REP_RESULT) | (1 << REP_GLUE_RESULT)) );
+	}
+#if defined(USE_RS_ASSERT) && (USE_RS_ASSERT) > 1 /* TODO: Enable when safe */
+	if ( (flags & (REP_MATCHED_MASK | REP_REF_MASK | REP_CTRLS_MASK)) ) {
+		RS_ASSERT( !(flags & REP_MATCHED_MASK) || rs->sr_matched );
+		RS_ASSERT( !(flags & REP_CTRLS_MASK  ) || rs->sr_ctrls   );
+		/* Note: LDAP_REFERRAL + !sr_ref is OK, becomes LDAP_NO_SUCH_OBJECT */
+	}
+#if (USE_RS_ASSERT) > 2
+	if ( rs->sr_err == LDAP_SUCCESS ) {
+		RS_ASSERT( rs->sr_text == NULL );
+		RS_ASSERT( rs->sr_matched == NULL );
+	}
+#endif
+#endif
+}
+
+/* Ready for calling a new backend operation */
+void
+(rs_assert_ready)( const SlapReply *rs )
+{
+	RS_ASSERT( !rs->sr_entry   );
+#if defined(USE_RS_ASSERT) && (USE_RS_ASSERT) > 1 /* TODO: Enable when safe */
+	RS_ASSERT( !rs->sr_text    );
+	RS_ASSERT( !rs->sr_ref     );
+	RS_ASSERT( !rs->sr_matched );
+	RS_ASSERT( !rs->sr_ctrls   );
+	RS_ASSERT( !rs->sr_flags   );
+#if (USE_RS_ASSERT) > 2
+	RS_ASSERT( rs->sr_err == LDAP_SUCCESS );
+#endif
+#else
+	RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
+#endif
+}
+
+/* Backend operation done */
+void
+(rs_assert_done)( const SlapReply *rs )
 {
-	slap_mask_t e_flags = rs->sr_flags & REP_ENTRY_MUSTFLUSH;
+#if defined(USE_RS_ASSERT) && (USE_RS_ASSERT) > 1 /* TODO: Enable when safe */
+	RS_ASSERT( !(rs->sr_flags & ~(REP_ENTRY_MODIFIABLE|REP_NO_OPERATIONALS)) );
+	rs_assert_ok( rs );
+#else
+	RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MUSTFLUSH) );
+#endif
+}
 
-	if ( e_flags && rs->sr_entry != NULL ) {
-		RS_ASSERT( e_flags != REP_ENTRY_MUSTFLUSH );
-		if ( !(e_flags & REP_ENTRY_MUSTRELEASE) ) {
+#endif /* LDAP_TEST || USE_RS_ASSERT */
+
+/* Reset a used SlapReply whose contents has been flushed (freed/released) */
+void
+(rs_reinit)( SlapReply *rs, slap_reply_t type )
+{
+	rs_reinit( rs, type );		/* proto-slap.h macro */
+}
+
+/* Obey and clear rs->sr_flags & REP_ENTRY_MASK.  Clear sr_entry if freed. */
+void
+rs_flush_entry( Operation *op, SlapReply *rs, slap_overinst *on )
+{
+	rs_assert_ok( rs );
+
+	if ( (rs->sr_flags & REP_ENTRY_MUSTFLUSH) && rs->sr_entry != NULL ) {
+		if ( !(rs->sr_flags & REP_ENTRY_MUSTRELEASE) ) {
 			entry_free( rs->sr_entry );
 		} else if ( on != NULL ) {
 			overlay_entry_release_ov( op, rs->sr_entry, 0, on );
 		} else {
 			be_entry_release_rw( op, rs->sr_entry, 0 );
 		}
+		rs->sr_entry = NULL;
 	}
+
 	rs->sr_flags &= ~REP_ENTRY_MASK;
+}
+
+/* Set rs->sr_entry after obeying and clearing sr_flags & REP_ENTRY_MASK. */
+void
+rs_replace_entry( Operation *op, SlapReply *rs, slap_overinst *on, Entry *e )
+{
+	rs_flush_entry( op, rs, on );
 	rs->sr_entry = e;
 }
 
@@ -168,7 +271,7 @@ int
 rs_ensure_entry_modifiable( Operation *op, SlapReply *rs, slap_overinst *on )
 {
 	if ( rs->sr_flags & REP_ENTRY_MODIFIABLE ) {
-		RS_ASSERT((rs->sr_flags & REP_ENTRY_MUSTFLUSH)==REP_ENTRY_MUSTBEFREED);
+		rs_assert_ok( rs );
 		return 0;
 	}
 	rs_replace_entry( op, rs, on, entry_dup( rs->sr_entry ));
@@ -662,6 +765,10 @@ send_ldap_disconnect( Operation	*op, SlapReply *rs )
 		rs->sr_err, rs->sr_text ? rs->sr_text : "", NULL );
 	assert( LDAP_UNSOLICITED_ERROR( rs->sr_err ) );
 
+	/* TODO: Flush the entry if sr_type == REP_SEARCH/REP_SEARCHREF? */
+	RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
+	rs->sr_flags &= ~REP_ENTRY_MASK;	/* paranoia */
+
 	rs->sr_type = REP_EXTENDED;
 	rs->sr_rspdata = NULL;
 
@@ -767,6 +874,9 @@ send_ldap_sasl( Operation *op, SlapReply *rs )
 		rs->sr_err,
 		rs->sr_sasldata ? (long) rs->sr_sasldata->bv_len : -1, NULL );
 
+	RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
+	rs->sr_flags &= ~REP_ENTRY_MASK;	/* paranoia */
+
 	rs->sr_type = REP_SASL;
 	rs->sr_tag = slap_req2res( op->o_tag );
 	rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
@@ -788,6 +898,9 @@ slap_send_ldap_extended( Operation *op, SlapReply *rs )
 		rs->sr_rspoid ? rs->sr_rspoid : "",
 		rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
 
+	RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
+	rs->sr_flags &= ~REP_ENTRY_MASK;	/* paranoia */
+
 	rs->sr_type = REP_EXTENDED;
 	rs->sr_tag = slap_req2res( op->o_tag );
 	rs->sr_msgid = (rs->sr_tag != LBER_SEQUENCE) ? op->o_msgid : 0;
@@ -809,6 +922,9 @@ slap_send_ldap_intermediate( Operation *op, SlapReply *rs )
 		rs->sr_rspoid ? rs->sr_rspoid : "",
 		rs->sr_rspdata != NULL ? rs->sr_rspdata->bv_len : 0 );
 
+	RS_ASSERT( !(rs->sr_flags & REP_ENTRY_MASK) );
+	rs->sr_flags &= ~REP_ENTRY_MASK;	/* paranoia */
+
 	rs->sr_type = REP_INTERMEDIATE;
 	rs->sr_tag = LDAP_RES_INTERMEDIATE;
 	rs->sr_msgid = op->o_msgid;
@@ -1288,11 +1404,7 @@ slap_send_search_entry( Operation *op, SlapReply *rs )
 	Statslog( LDAP_DEBUG_STATS2, "%s ENTRY dn=\"%s\"\n",
 	    op->o_log_prefix, rs->sr_entry->e_nname.bv_val, 0, 0, 0 );
 
-	if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
-		be_entry_release_rw( op, rs->sr_entry, 0 );
-		rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
-		rs->sr_entry = NULL;
-	}
+	rs_flush_entry( op, rs, NULL );
 
 	if ( op->o_res_ber == NULL ) {
 		bytes = send_ldap_ber( op, ber );
@@ -1329,25 +1441,17 @@ error_return:;
 		slap_sl_free( e_flags, op->o_tmpmemctx );
 	}
 
+	/* FIXME: Can break if rs now contains an extended response */
 	if ( rs->sr_operational_attrs ) {
 		attrs_free( rs->sr_operational_attrs );
 		rs->sr_operational_attrs = NULL;
 	}
 	rs->sr_attr_flags = SLAP_ATTRS_UNDEFINED;
 
-	/* FIXME: I think rs->sr_type should be explicitly set to
-	 * REP_SEARCH here. That's what it was when we entered this
-	 * function. send_ldap_error may have changed it, but we
-	 * should set it back so that the cleanup functions know
-	 * what they're doing.
-	 */
-	if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH 
-		&& rs->sr_entry 
-		&& ( rs->sr_flags & REP_ENTRY_MUSTBEFREED ) ) 
-	{
-		entry_free( rs->sr_entry );
-		rs->sr_entry = NULL;
-		rs->sr_flags &= ~REP_ENTRY_MUSTBEFREED;
+	if ( op->o_tag == LDAP_REQ_SEARCH && rs->sr_type == REP_SEARCH ) {
+		rs_flush_entry( op, rs, NULL );
+	} else {
+		RS_ASSERT( (rs->sr_flags & REP_ENTRY_MASK) == 0 );
 	}
 
 	if ( rs->sr_flags & REP_CTRLS_MUSTBEFREED ) {
@@ -1466,12 +1570,7 @@ slap_send_search_reference( Operation *op, SlapReply *rs )
 	}
 
 	rc = 0;
-	if ( rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
-		assert( rs->sr_entry != NULL );
-		be_entry_release_rw( op, rs->sr_entry, 0 );
-		rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
-		rs->sr_entry = NULL;
-	}
+	rs_flush_entry( op, rs, NULL );
 
 #ifdef LDAP_CONNECTIONLESS
 	if (!op->o_conn || op->o_conn->c_is_udp == 0) {
diff --git a/servers/slapd/slap.h b/servers/slapd/slap.h
index c4abd48d34e916b10dd2b5d5cba38012382c6376..3ebc10a7d20719675fe1ffad4b16cd5ed6885124 100644
--- a/servers/slapd/slap.h
+++ b/servers/slapd/slap.h
@@ -2102,23 +2102,23 @@ struct SlapReply {
 		rep_extended_s sru_extended;
 	} sr_un;
 	slap_mask_t sr_flags;
-#define REP_ENTRY_MODIFIABLE	0x0001U
-#define REP_ENTRY_MUSTBEFREED	0x0002U
-#define REP_ENTRY_MUSTRELEASE	0x0004U
-#define	REP_ENTRY_MASK		(REP_ENTRY_MODIFIABLE|REP_ENTRY_MUSTBEFREED|REP_ENTRY_MUSTRELEASE)
+#define	REP_ENTRY_MODIFIABLE	((slap_mask_t) 0x0001U)
+#define	REP_ENTRY_MUSTBEFREED	((slap_mask_t) 0x0002U)
+#define	REP_ENTRY_MUSTRELEASE	((slap_mask_t) 0x0004U)
+#define	REP_ENTRY_MASK		(REP_ENTRY_MODIFIABLE|REP_ENTRY_MUSTFLUSH)
 #define	REP_ENTRY_MUSTFLUSH	(REP_ENTRY_MUSTBEFREED|REP_ENTRY_MUSTRELEASE)
 
-#define REP_MATCHED_MUSTBEFREED	0x0010U
+#define	REP_MATCHED_MUSTBEFREED	((slap_mask_t) 0x0010U)
 #define	REP_MATCHED_MASK	(REP_MATCHED_MUSTBEFREED)
 
-#define REP_REF_MUSTBEFREED	0x0020U
+#define REP_REF_MUSTBEFREED	((slap_mask_t) 0x0020U)
 #define REP_REF_MASK		(REP_REF_MUSTBEFREED)
 
-#define REP_CTRLS_MUSTBEFREED	0x0040U
+#define REP_CTRLS_MUSTBEFREED	((slap_mask_t) 0x0040U)
 #define REP_CTRLS_MASK		(REP_CTRLS_MUSTBEFREED)
 
-#define	REP_NO_ENTRYDN		0x1000U
-#define	REP_NO_SUBSCHEMA	0x2000U
+#define	REP_NO_ENTRYDN		((slap_mask_t) 0x1000U)
+#define	REP_NO_SUBSCHEMA	((slap_mask_t) 0x2000U)
 #define	REP_NO_OPERATIONALS	(REP_NO_ENTRYDN|REP_NO_SUBSCHEMA)
 };
 
diff --git a/servers/slapd/slapi/slapi_pblock.c b/servers/slapd/slapi/slapi_pblock.c
index 9fa9a7106cb7683b13bf9ac7fa7ceefef75dddee..ddb0209326232ac7ed172a2b18db2416f41a89b1 100644
--- a/servers/slapd/slapi/slapi_pblock.c
+++ b/servers/slapd/slapi/slapi_pblock.c
@@ -1176,13 +1176,8 @@ pblock_set( Slapi_PBlock *pb, int param, void *value )
 		break;
 	case SLAPI_SEARCH_RESULT_ENTRY:
 		PBLOCK_ASSERT_OP( pb, 0 );
-		if ( pb->pb_rs->sr_flags & REP_ENTRY_MUSTBEFREED ) {
-			entry_free( pb->pb_rs->sr_entry );
-		} else if ( pb->pb_rs->sr_flags & REP_ENTRY_MUSTRELEASE ) {
-			be_entry_release_r( pb->pb_op, pb->pb_rs->sr_entry );
-			pb->pb_rs->sr_flags ^= REP_ENTRY_MUSTRELEASE;
-		}
-		pb->pb_rs->sr_entry = (Slapi_Entry *)value;
+		rs_replace_entry( pb->pb_op, pb->pb_rs, NULL, (Slapi_Entry *)value );
+		/* TODO: Should REP_ENTRY_MODIFIABLE be set? */
 		pb->pb_rs->sr_flags |= REP_ENTRY_MUSTBEFREED;
 		break;
 	case SLAPI_BIND_RET_SASLCREDS:
@@ -1429,4 +1424,3 @@ slapi_int_pblock_get_next( Slapi_PBlock **pb )
 }
 
 #endif /* LDAP_SLAPI */
-
diff --git a/servers/slapd/syncrepl.c b/servers/slapd/syncrepl.c
index 257f933b32a8e1770e150dfcc7183f4068e290da..4dae14820daa33fff5e7533c89664e3289d7e671 100644
--- a/servers/slapd/syncrepl.c
+++ b/servers/slapd/syncrepl.c
@@ -466,7 +466,7 @@ check_syncprov(
 	AttributeName at[2];
 	Attribute a = {0};
 	Entry e = {0};
-	SlapReply rs = {0};
+	SlapReply rs = {REP_SEARCH};
 	int i, j, changed = 0;
 
 	/* Look for contextCSN from syncprov overlay. If
@@ -2239,9 +2239,6 @@ syncrepl_entry(
 	struct berval	syncUUID_strrep = BER_BVNULL;
 
 	SlapReply	rs_search = {REP_RESULT};
-	SlapReply	rs_delete = {REP_RESULT};
-	SlapReply	rs_add = {REP_RESULT};
-	SlapReply	rs_modify = {REP_RESULT};
 	Filter f = {0};
 	AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
 	int rc = LDAP_SUCCESS;
@@ -2388,6 +2385,7 @@ syncrepl_entry(
 		}
 retry_add:;
 		if ( BER_BVISNULL( &dni.dn ) ) {
+			SlapReply	rs_add = {REP_RESULT};
 
 			op->o_req_dn = entry->e_name;
 			op->o_req_ndn = entry->e_nname;
@@ -2425,7 +2423,7 @@ retry_add:;
 			case LDAP_ALREADY_EXISTS:
 				if ( retry ) {
 					Operation	op2 = *op;
-					SlapReply	rs2 = { 0 };
+					SlapReply	rs2 = { REP_RESULT };
 					slap_callback	cb2 = { 0 };
 
 					op2.o_bd = be;
@@ -2475,6 +2473,7 @@ retry_add:;
 			struct berval noldp, newp;
 			Modifications *mod, **modtail, **ml, *m2;
 			int i, got_replace = 0, just_rename = 0;
+			SlapReply rs_modify = {REP_RESULT};
 
 			op->o_tag = LDAP_REQ_MODRDN;
 			dnRdn( &entry->e_name, &op->orr_newrdn );
@@ -2652,6 +2651,7 @@ retry_add:;
 			}
 			op->o_bd = si->si_wbe;
 retry_modrdn:;
+			rs_reinit( &rs_modify, REP_RESULT );
 			rc = op->o_bd->be_modrdn( op, &rs_modify );
 
 			/* NOTE: noSuchObject should result because the new superior
@@ -2682,6 +2682,8 @@ retry_modrdn:;
 				slap_queue_csn( op, syncCSN );
 		}
 		if ( dni.mods ) {
+			SlapReply rs_modify = {REP_RESULT};
+
 			op->o_tag = LDAP_REQ_MODIFY;
 			op->orm_modlist = dni.mods;
 			op->orm_no_opattrs = 1;
@@ -2712,6 +2714,7 @@ retry_modrdn:;
 		goto done;
 	case LDAP_SYNC_DELETE :
 		if ( !BER_BVISNULL( &dni.dn ) ) {
+			SlapReply	rs_delete = {REP_RESULT};
 			op->o_req_dn = dni.dn;
 			op->o_req_ndn = dni.ndn;
 			op->o_tag = LDAP_REQ_DELETE;
@@ -2731,6 +2734,7 @@ retry_modrdn:;
 					op->o_req_dn = pdn;
 					op->o_req_ndn = pdn;
 					op->o_callback = &cb;
+					rs_reinit( &rs_delete, REP_RESULT );
 					op->o_bd->be_delete( op, &rs_delete );
 				} else {
 					break;
@@ -2789,9 +2793,6 @@ syncrepl_del_nonpresent(
 {
 	Backend* be = op->o_bd;
 	slap_callback	cb = { NULL };
-	SlapReply	rs_search = {REP_RESULT};
-	SlapReply	rs_delete = {REP_RESULT};
-	SlapReply	rs_modify = {REP_RESULT};
 	struct nonpresent_entry *np_list, *np_prev;
 	int rc;
 	AttributeName	an[2];
@@ -2838,6 +2839,8 @@ syncrepl_del_nonpresent(
 		si->si_refreshDelete |= NP_DELETE_ONE;
 
 		for (i=0; uuids[i].bv_val; i++) {
+			SlapReply rs_search = {REP_RESULT};
+
 			op->ors_slimit = 1;
 			uf.f_av_value = uuids[i];
 			filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
@@ -2849,6 +2852,7 @@ syncrepl_del_nonpresent(
 		Filter *cf, *of;
 		Filter mmf[2];
 		AttributeAssertion mmaa;
+		SlapReply rs_search = {REP_RESULT};
 
 		memset( &an[0], 0, 2 * sizeof( AttributeName ) );
 		an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
@@ -2918,6 +2922,8 @@ syncrepl_del_nonpresent(
 
 		np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
 		while ( np_list != NULL ) {
+			SlapReply rs_delete = {REP_RESULT};
+
 			LDAP_LIST_REMOVE( np_list, npe_link );
 			np_prev = np_list;
 			np_list = LDAP_LIST_NEXT( np_list, npe_link );
@@ -2933,6 +2939,7 @@ syncrepl_del_nonpresent(
 				si->si_ridtxt, op->o_req_dn.bv_val, rc );
 
 			if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
+				SlapReply rs_modify = {REP_RESULT};
 				Modifications mod1, mod2;
 				mod1.sml_op = LDAP_MOD_REPLACE;
 				mod1.sml_flags = 0;
@@ -2969,6 +2976,7 @@ syncrepl_del_nonpresent(
 					op->o_req_dn = pdn;
 					op->o_req_ndn = pdn;
 					op->o_callback = &cb;
+					rs_reinit( &rs_delete, REP_RESULT );
 					/* give it a root privil ? */
 					op->o_bd->be_delete( op, &rs_delete );
 				} else {
@@ -3011,7 +3019,6 @@ syncrepl_add_glue_ancestors(
 	struct berval dn = BER_BVNULL;
 	struct berval ndn = BER_BVNULL;
 	Entry	*glue;
-	SlapReply	rs_add = {REP_RESULT};
 	struct berval	ptr, nptr;
 	char		*comma;
 
@@ -3069,6 +3076,8 @@ syncrepl_add_glue_ancestors(
 	}
 
 	while ( ndn.bv_val > e->e_nname.bv_val ) {
+		SlapReply	rs_add = {REP_RESULT};
+
 		glue = entry_alloc();
 		ber_dupbv( &glue->e_name, &dn );
 		ber_dupbv( &glue->e_nname, &ndn );
@@ -3286,6 +3295,7 @@ syncrepl_updateCookie(
 		char txtbuf[SLAP_TEXT_BUFLEN];
 		size_t textlen = sizeof txtbuf;
 		Entry *e = slap_create_context_csn_entry( op->o_bd, NULL );
+		rs_reinit( &rs_modify, REP_RESULT );
 		rc = slap_mods2entry( &mod, &e, 0, 1, &text, txtbuf, textlen);
 		op->ora_e = e;
 		rc = op->o_bd->be_add( op, &rs_modify );
@@ -3587,7 +3597,6 @@ dn_callback(
 			 * in the provider are always propagated.
 			 */
 			if ( dni->new_entry ) {
-				Modifications **modtail, **ml;
 				Attribute *old, *new;
 				struct berval old_rdn, new_rdn;
 				struct berval old_p, new_p;