Index: core/src/test/org/apache/ldap/server/jndi/SearchContextTest.java =================================================================== --- core/src/test/org/apache/ldap/server/jndi/SearchContextTest.java (revision 280882) +++ core/src/test/org/apache/ldap/server/jndi/SearchContextTest.java (working copy) @@ -261,4 +261,33 @@ assertTrue( map.containsKey( "ou=subtest,ou=testing01,ou=system" ) ); } + + public void testSearchFilterArgs() throws NamingException + { + SearchControls controls = new SearchControls(); + + controls.setSearchScope( SearchControls.ONELEVEL_SCOPE ); + + controls.setDerefLinkFlag( false ); + + sysRoot.addToEnvironment( DerefAliasesEnum.JNDI_PROP, DerefAliasesEnum.NEVERDEREFALIASES.getName() ); + + HashMap map = new HashMap(); + + NamingEnumeration list = sysRoot.search( "", "(| (ou={0}) (ou={1}))", new Object[] {"testing00", "testing01"}, controls ); + + while ( list.hasMore() ) + { + SearchResult result = ( SearchResult ) list.next(); + + map.put( result.getName(), result.getAttributes() ); + } + + assertEquals( "Expected number of results returned was incorrect!", 2, map.size() ); + + assertTrue( map.containsKey( "ou=testing00,ou=system" ) ); + + assertTrue( map.containsKey( "ou=testing01,ou=system" ) ); + } + } Index: core/src/main/java/org/apache/ldap/server/jndi/ServerDirContext.java =================================================================== --- core/src/main/java/org/apache/ldap/server/jndi/ServerDirContext.java (revision 280882) +++ core/src/main/java/org/apache/ldap/server/jndi/ServerDirContext.java (working copy) @@ -631,32 +631,44 @@ public NamingEnumeration search( Name name, String filterExpr, Object[] filterArgs, SearchControls cons ) throws NamingException { int start; + int index; StringBuffer buf = new StringBuffer( filterExpr ); // Scan until we hit the end of the string buffer for ( int ii = 0; ii < buf.length(); ii++ ) { - // Advance until we hit the start of a variable - while ( '{' != buf.charAt( ii ) ) + try { - ii++; + // Advance until we hit the start of a variable + while ( ii < buf.length() && '{' != buf.charAt( ii ) ) + { + ii++; + } + + // Record start of variable at '{' + start = ii; + + // Advance to the end of a variable at '}' + while ( '}' != buf.charAt( ii ) ) + { + ii++; + } } - - // Record start of variable at '{' - start = ii; - - // Advance to the end of a variable at '}' - while ( '}' != buf.charAt( ii ) ) + catch (IndexOutOfBoundsException e) { - ii++; + // End of filter so done. + break; } + // Parse index + index = Integer.parseInt(buf.substring(start + 1, ii)); + /* * Replace the '{ i }' with the string representation of the value * held in the filterArgs array at index index. */ - buf.replace( start, ii + 1, filterArgs[ii].toString() ); + buf.replace( start, ii + 1, filterArgs[index].toString() ); } return search( name, buf.toString(), cons );