Program: 39958
Project: 39958
Buggy source:
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws IOException, ServletException {
  HttpServletRequest r=(HttpServletRequest)request;
  retrievedUsername=r.getHeader(forwardedUser);
  Authentication auth=Hudson.ANONYMOUS;
  if (retrievedUsername != null) {
    if (getLDAPURL() != null) {
      GrantedAuthority[] storedGrants=authContext.get(retrievedUsername);
      if (storedGrants != null && storedGrants.length > 1) {
        authorities=storedGrants;
      }
 else {
        LdapUserDetails userDetails=(LdapUserDetails)loadUserByUsername(retrievedUsername);
        authorities=userDetails.getAuthorities();
        Set<GrantedAuthority> tempLocalAuthorities=new HashSet<GrantedAuthority>(Arrays.asList(authorities));
        tempLocalAuthorities.add(AUTHENTICATED_AUTHORITY);
        authorities=tempLocalAuthorities.toArray(new GrantedAuthority[0]);
      }
    }
 else {
      String groups=r.getHeader(headerGroups);
      List<GrantedAuthority> localAuthorities=new ArrayList<GrantedAuthority>();
      localAuthorities.add(AUTHENTICATED_AUTHORITY);
      if (groups != null) {
        StringTokenizer tokenizer=new StringTokenizer(groups,headerGroupsDelimiter);
        while (tokenizer.hasMoreTokens()) {
          final String token=tokenizer.nextToken().trim();
          localAuthorities.add(new GrantedAuthorityImpl(token));
        }
      }
      authorities=localAuthorities.toArray(new GrantedAuthority[0]);
      SearchTemplate searchTemplate=new UserSearchTemplate(retrievedUsername);
      Set<String> foundAuthorities=proxyTemplate.searchForSingleAttributeValues(searchTemplate,authorities);
      Set<GrantedAuthority> tempLocalAuthorities=new HashSet<GrantedAuthority>();
      String[] authString=foundAuthorities.toArray(new String[0]);
      for (int i=0; i < authString.length; i++) {
        tempLocalAuthorities.add(new GrantedAuthorityImpl(authString[i]));
      }
      authorities=tempLocalAuthorities.toArray(new GrantedAuthority[0]);
      authContext.put(retrievedUsername,authorities);
      auth=new UsernamePasswordAuthenticationToken(retrievedUsername,"String_Node_Str",authorities);
    }
    authContext.put(retrievedUsername,authorities);
    auth=new UsernamePasswordAuthenticationToken(retrievedUsername,"String_Node_Str",authorities);
  }
  SecurityContextHolder.getContext().setAuthentication(auth);
  chain.doFilter(r,response);
}

Fixed version (ground truth):
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws IOException, ServletException {
  HttpServletRequest r=(HttpServletRequest)request;
  retrievedUsername=r.getHeader(forwardedUser);
  Authentication auth=Hudson.ANONYMOUS;
  if (retrievedUsername != null) {
    if (getLDAPURL() != null) {
      GrantedAuthority[] storedGrants=authContext.get(retrievedUsername);
      if (storedGrants != null && storedGrants.length > 1) {
        authorities=storedGrants;
      }
 else {
        try {
          LdapUserDetails userDetails=(LdapUserDetails)loadUserByUsername(retrievedUsername);
          authorities=userDetails.getAuthorities();
          Set<GrantedAuthority> tempLocalAuthorities=new HashSet<GrantedAuthority>(Arrays.asList(authorities));
          authorities=tempLocalAuthorities.toArray(new GrantedAuthority[0]);
        }
 catch (        UsernameNotFoundException e) {
          LOGGER.log(Level.WARNING,"String_Node_Str" + e.getMessage());
        }
 finally {
          Set<GrantedAuthority> tempLocalAuthorities=new HashSet<GrantedAuthority>();
          tempLocalAuthorities.add(AUTHENTICATED_AUTHORITY);
          authorities=tempLocalAuthorities.toArray(new GrantedAuthority[0]);
        }
      }
    }
 else {
      String groups=r.getHeader(headerGroups);
      List<GrantedAuthority> localAuthorities=new ArrayList<GrantedAuthority>();
      localAuthorities.add(AUTHENTICATED_AUTHORITY);
      if (groups != null) {
        StringTokenizer tokenizer=new StringTokenizer(groups,headerGroupsDelimiter);
        while (tokenizer.hasMoreTokens()) {
          final String token=tokenizer.nextToken().trim();
          localAuthorities.add(new GrantedAuthorityImpl(token));
        }
      }
      authorities=localAuthorities.toArray(new GrantedAuthority[0]);
      SearchTemplate searchTemplate=new UserSearchTemplate(retrievedUsername);
      Set<String> foundAuthorities=proxyTemplate.searchForSingleAttributeValues(searchTemplate,authorities);
      Set<GrantedAuthority> tempLocalAuthorities=new HashSet<GrantedAuthority>();
      String[] authString=foundAuthorities.toArray(new String[0]);
      for (int i=0; i < authString.length; i++) {
        tempLocalAuthorities.add(new GrantedAuthorityImpl(authString[i]));
      }
      authorities=tempLocalAuthorities.toArray(new GrantedAuthority[0]);
      authContext.put(retrievedUsername,authorities);
      auth=new UsernamePasswordAuthenticationToken(retrievedUsername,"String_Node_Str",authorities);
    }
    authContext.put(retrievedUsername,authorities);
    auth=new UsernamePasswordAuthenticationToken(retrievedUsername,"String_Node_Str",authorities);
  }
  SecurityContextHolder.getContext().setAuthentication(auth);
  chain.doFilter(r,response);
}