From 6ccc0974e1c9429f7407241b3705230109613278 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ond=C5=99ej=20Kuzn=C3=ADk?= <ondra@mistotebe.net>
Date: Thu, 13 Jan 2022 09:21:21 +0000
Subject: [PATCH] ITS#8039 Free resinfo even if opcookie is the last owner

---
 servers/slapd/overlays/syncprov.c | 27 +++++++++++++++++----------
 1 file changed, 17 insertions(+), 10 deletions(-)

diff --git a/servers/slapd/overlays/syncprov.c b/servers/slapd/overlays/syncprov.c
index 7a5a637dc..41a409dcd 100644
--- a/servers/slapd/overlays/syncprov.c
+++ b/servers/slapd/overlays/syncprov.c
@@ -843,24 +843,27 @@ again:
 static void free_resinfo( syncres *sr )
 {
 	syncres **st;
+	resinfo *ri = sr->s_info;
 	int freeit = 0;
-	ldap_pvt_thread_mutex_lock( &sr->s_info->ri_mutex );
+
+	ldap_pvt_thread_mutex_lock( &ri->ri_mutex );
 	for (st = &sr->s_info->ri_list; *st; st = &(*st)->s_rilist) {
 		if (*st == sr) {
 			*st = sr->s_rilist;
+			if ( !sr->s_info->ri_list )
+				freeit = 1;
+			sr->s_info = NULL;
 			break;
 		}
 	}
-	if ( !sr->s_info->ri_list )
-		freeit = 1;
-	ldap_pvt_thread_mutex_unlock( &sr->s_info->ri_mutex );
+	ldap_pvt_thread_mutex_unlock( &ri->ri_mutex );
 	if ( freeit ) {
-		ldap_pvt_thread_mutex_destroy( &sr->s_info->ri_mutex );
-		if ( sr->s_info->ri_e )
-			entry_free( sr->s_info->ri_e );
-		if ( !BER_BVISNULL( &sr->s_info->ri_cookie ))
-			ch_free( sr->s_info->ri_cookie.bv_val );
-		ch_free( sr->s_info );
+		ldap_pvt_thread_mutex_destroy( &ri->ri_mutex );
+		if ( ri->ri_e )
+			entry_free( ri->ri_e );
+		if ( !BER_BVISNULL( &ri->ri_cookie ))
+			ch_free( ri->ri_cookie.bv_val );
+		ch_free( ri );
 	}
 }
 
@@ -1546,6 +1549,10 @@ syncprov_op_cleanup( Operation *op, SlapReply *rs )
 	if ( !BER_BVISNULL( &opc->sdn ))
 		op->o_tmpfree( opc->sdn.bv_val, op->o_tmpmemctx );
 	op->o_callback = cb->sc_next;
+
+	if ( opc->ssres.s_info ) {
+		free_resinfo( &opc->ssres );
+	}
 	op->o_tmpfree(cb, op->o_tmpmemctx);
 
 	return 0;
-- 
2.33.0

