From 395e9b250a1a430e788487f73b292c08fc28540c Mon Sep 17 00:00:00 2001
From: Howard Chu <hyc@openldap.org>
Date: Fri, 3 Jun 2022 00:48:50 +0100
Subject: [PATCH] ITS#9858 back-mdb: fix index reconfig

---
 servers/slapd/back-mdb/attr.c   |  6 +++++-
 servers/slapd/back-mdb/config.c | 24 ++++++++++++------------
 2 files changed, 17 insertions(+), 13 deletions(-)

diff --git a/servers/slapd/back-mdb/attr.c b/servers/slapd/back-mdb/attr.c
index 9567fb522..7219705b2 100644
--- a/servers/slapd/back-mdb/attr.c
+++ b/servers/slapd/back-mdb/attr.c
@@ -446,7 +446,11 @@ fail:
 					/* If this is leftover from a previous add, commit it */
 					if ( b->ai_newmask )
 						b->ai_indexmask = b->ai_newmask;
-					b->ai_newmask = a->ai_newmask;
+					/* If the mask changed, remember it */
+					if ( b->ai_indexmask != a->ai_newmask )
+						b->ai_newmask = a->ai_newmask;
+					else	/* else ignore it */
+						b->ai_newmask = 0;
 					ch_free( a );
 					rc = 0;
 					continue;
diff --git a/servers/slapd/back-mdb/config.c b/servers/slapd/back-mdb/config.c
index 1b3cf98fe..48143ef4d 100644
--- a/servers/slapd/back-mdb/config.c
+++ b/servers/slapd/back-mdb/config.c
@@ -349,7 +349,7 @@ mdb_setup_indexer( struct mdb_info *mdb )
 	MDB_txn *txn;
 	MDB_cursor *curs;
 	MDB_val key, data;
-	int i, rc;
+	int i, rc, changed = 0;
 	unsigned short s;
 
 	rc = mdb_txn_begin( mdb->mi_dbenv, NULL, 0, &txn );
@@ -364,17 +364,6 @@ mdb_setup_indexer( struct mdb_info *mdb )
 	key.mv_size = sizeof( s );
 	key.mv_data = &s;
 
-	/* set indexer task to start at first entry */
-	{
-		ID id = 0;
-		s = 0;			/* key 0 records next entryID to index */
-		data.mv_size = sizeof( ID );
-		data.mv_data = &id;
-		rc = mdb_cursor_put( curs, &key, &data, 0 );
-		if ( rc )
-			goto done;
-	}
-
 	/* record current and new index masks for all new index definitions */
 	{
 		slap_mask_t mask[2];
@@ -389,8 +378,19 @@ mdb_setup_indexer( struct mdb_info *mdb )
 			rc = mdb_cursor_put( curs, &key, &data, 0 );
 			if ( rc )
 				goto done;
+			changed = 1;
 		}
 	}
+
+	/* set indexer task to start at first entry */
+	if ( changed ) {
+		ID id = 0;
+		s = 0;			/* key 0 records next entryID to index */
+		data.mv_size = sizeof( ID );
+		data.mv_data = &id;
+		rc = mdb_cursor_put( curs, &key, &data, 0 );
+	}
+
 done:
 	mdb_cursor_close( curs );
 	if ( !rc )
-- 
2.33.0

