Program: 36391
Project: 36391
Buggy source:
/** 
 * @param ctx - the context to search from
 * @param user - the input username
 * @param credential - the bind credential
 * @param baseDN - base DN to search the ctx from
 * @param filter - the search filter string
 * @return the userDN string for the successful authentication 
 * @throws NamingException
 */
protected String bindDNAuthentication(InitialLdapContext ctx,String user,Object credential,String baseDN,String filter) throws NamingException {
  SearchControls constraints=new SearchControls();
  constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
  constraints.setTimeLimit(searchTimeLimit);
  String attrList[]={distinguishedNameAttribute};
  constraints.setReturningAttributes(attrList);
  NamingEnumeration results=null;
  Object[] filterArgs={user};
  LdapContext ldapCtx=ctx;
  boolean referralsLeft=true;
  SearchResult sr=null;
  while (referralsLeft) {
    try {
      results=ldapCtx.search(baseDN,filter,filterArgs,constraints);
      while (results.hasMore()) {
        sr=(SearchResult)results.next();
        break;
      }
      referralsLeft=false;
    }
 catch (    ReferralException e) {
      ldapCtx=(LdapContext)e.getReferralContext();
      if (results != null) {
        results.close();
      }
    }
  }
  if (sr == null) {
    results.close();
    throw PicketBoxMessages.MESSAGES.failedToFindBaseContextDN(baseDN);
  }
  String name=sr.getName();
  String userDN=null;
  Attributes attrs=sr.getAttributes();
  if (attrs != null) {
    Attribute dn=attrs.get(distinguishedNameAttribute);
    if (dn != null) {
      userDN=(String)dn.get();
    }
  }
  if (userDN == null) {
    if (sr.isRelative() == true) {
      userDN=name + ("String_Node_Str".equals(baseDN) ? "String_Node_Str" : "String_Node_Str" + baseDN);
    }
 else {
      throw PicketBoxMessages.MESSAGES.unableToFollowReferralForAuth(name);
    }
  }
  results.close();
  results=null;
  if (isPasswordValidated) {
    InitialLdapContext userCtx=constructInitialLdapContext(userDN,credential);
    userCtx.close();
  }
  return userDN;
}

Fixed version (ground truth):
/** 
 * @param ctx - the context to search from
 * @param user - the input username
 * @param credential - the bind credential
 * @param baseDN - base DN to search the ctx from
 * @param filter - the search filter string
 * @return the userDN string for the successful authentication 
 * @throws NamingException
 */
protected String bindDNAuthentication(InitialLdapContext ctx,String user,Object credential,String baseDN,String filter) throws NamingException {
  SearchControls constraints=new SearchControls();
  constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
  constraints.setTimeLimit(searchTimeLimit);
  String attrList[]={distinguishedNameAttribute};
  constraints.setReturningAttributes(attrList);
  NamingEnumeration results=null;
  Object[] filterArgs={user};
  LdapContext ldapCtx=ctx;
  boolean referralsLeft=true;
  SearchResult sr=null;
  while (referralsLeft) {
    try {
      results=ldapCtx.search(baseDN,filter,filterArgs,constraints);
      while (results.hasMore()) {
        sr=(SearchResult)results.next();
        break;
      }
      referralsLeft=false;
    }
 catch (    ReferralException e) {
      ldapCtx=(LdapContext)e.getReferralContext();
      if (results != null) {
        results.close();
      }
    }
  }
  if (sr == null) {
    results.close();
    throw PicketBoxMessages.MESSAGES.failedToFindBaseContextDN(baseDN);
  }
  String name=sr.getName();
  String userDN=null;
  Attributes attrs=sr.getAttributes();
  if (attrs != null) {
    Attribute dn=attrs.get(distinguishedNameAttribute);
    if (dn != null) {
      userDN=(String)dn.get();
    }
  }
  if (userDN == null) {
    if (sr.isRelative() == true) {
      userDN=new CompositeName(name).get(0) + ("String_Node_Str".equals(baseDN) ? "String_Node_Str" : "String_Node_Str" + baseDN);
    }
 else {
      throw PicketBoxMessages.MESSAGES.unableToFollowReferralForAuth(name);
    }
  }
  results.close();
  results=null;
  if (isPasswordValidated) {
    InitialLdapContext userCtx=constructInitialLdapContext(userDN,credential);
    userCtx.close();
  }
  return userDN;
}