From 768473fc196acf4c588d7893ba8929c59e68190e Mon Sep 17 00:00:00 2001
From: Noriko Hosoi <nhosoi@redhat.com>
Date: Sun, 3 May 2015 22:59:02 -0700
Subject: [PATCH] Ticket #48109 - substring index with nssubstrbegin: 1 is not
 being used with filters like (attr=x*)

Description: In the commit ae69e4aa1dcec1c5f79af502b77d9e19a3d348ee,
when pblock is passed to slapi_attr_assertion2keys_sub_sv_pb from the
caller, the original slapdplugin is overwritten in the function.  In
the case, the original slapdplugin has to be backed up and restored.

https://fedorahosted.org/389/ticket/48109
---
 ldap/servers/slapd/plugin_syntax.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/ldap/servers/slapd/plugin_syntax.c b/ldap/servers/slapd/plugin_syntax.c
index fa05b0b..06a9514 100644
--- a/ldap/servers/slapd/plugin_syntax.c
+++ b/ldap/servers/slapd/plugin_syntax.c
@@ -924,6 +924,7 @@ slapi_attr_assertion2keys_sub_sv_pb(
 	int			rc;
 	Slapi_PBlock		pipb;
 	struct slapdplugin	*pi = NULL;
+	struct slapdplugin	*origpi = NULL;
 	IFP a2k_fn = NULL;
 
 	LDAPDebug( LDAP_DEBUG_FILTER,
@@ -943,8 +944,11 @@ slapi_attr_assertion2keys_sub_sv_pb(
 	if (NULL == pb) {
 		pblock_init( &pipb );
 		pb = &pipb;
+	} else {
+		/* back up the original slapdplugin if any */
+		slapi_pblock_get(pb, SLAPI_PLUGIN, &origpi);
 	}
-	slapi_pblock_set( pb, SLAPI_PLUGIN, pi );
+	slapi_pblock_set(pb, SLAPI_PLUGIN, pi);
 
 	rc = -1;	/* means no assertion2keys function */
 	*ivals = NULL;
@@ -952,6 +956,11 @@ slapi_attr_assertion2keys_sub_sv_pb(
 		rc = (*a2k_fn)( pb, initial, any, final, ivals );
 	}
 
+	if (pb != &pipb) {
+		/* restore the original slapdplugin if pb is not local. */
+		slapi_pblock_set(pb, SLAPI_PLUGIN, origpi);
+	}
+
 	LDAPDebug( LDAP_DEBUG_FILTER,
 	    "<= slapi_attr_assertion2keys_sub_sv %d\n", rc, 0, 0 );
 	return( rc );
-- 
1.9.3

