Program: 34680
Project: 34680
Buggy source:
private boolean getGroupsByMembership(Entity entity,LdapEntry ldapEntry,String[] bases,int level,List<String> propNames,String groupDN) throws WIMException {
  final String METHODNAME="String_Node_Str";
  boolean nested=(level == 0 && iLdapConfigMgr.getMembershipAttributeScope() == LDAP_DIRECT_GROUP_MEMBERSHIP);
  if (!nested && (iLdapConfigMgr.isRecursiveSearch()))   nested=true;
  boolean isInGrp=false;
  List<String> supportedProps=iLdapConfigMgr.getSupportedProperties(SchemaConstants.DO_GROUP,propNames);
  String mbrshipAttrName=iLdapConfigMgr.getMembershipAttribute();
  Attribute mbrshipAttr=ldapEntry.getAttributes().get(mbrshipAttrName);
  try {
    if (mbrshipAttr == null || (mbrshipAttr.size() == 1 && mbrshipAttr.get(0) == null)) {
      if (iLdapConfigMgr.getLdapType().startsWith("String_Node_Str") && mbrshipAttrName != null && mbrshipAttrName.equalsIgnoreCase(LDAP_ATTR_IBM_ALL_GROUP))       isInGrp=false;
 else       isInGrp=getGroupsByMember(entity,ldapEntry,bases,level,supportedProps,groupDN);
      return isInGrp;
    }
 else     if (mbrshipAttr.size() == 0) {
      isInGrp=false;
      return isInGrp;
    }
    Map<String,Attribute> DNMbrshipMap=null;
    if (nested) {
      DNMbrshipMap=new HashMap<String,Attribute>();
    }
    Set<String> groupsToDo=new HashSet<String>();
    List<String> groupTypes=iLdapConfigMgr.getGroupTypes();
    NamingEnumeration<?> enm=mbrshipAttr.getAll();
    while (enm.hasMoreElements()) {
      String dn=(String)enm.nextElement();
      dn=LdapHelper.getValidDN(dn);
      if (groupDN != null && groupDN.equalsIgnoreCase(dn)) {
        isInGrp=true;
        return isInGrp;
      }
      if (!LdapHelper.isUnderBases(dn,bases)) {
        continue;
      }
      List grpTypes=iLdapConfigMgr.getGroupTypes();
      Set entityType=new HashSet(groupTypes);
      String entityTypeFilter=iLdapConfigMgr.getEntityTypesFilter(entityType);
      LdapEntry grpEntry=null;
      Set grpEntries=null;
      try {
        grpEntries=iLdapConn.searchEntities(dn,entityTypeFilter,null,SearchControls.OBJECT_SCOPE,grpTypes,supportedProps,true,false);
        if (grpEntries != null) {
          Iterator itr=grpEntries.iterator();
          while (itr.hasNext())           grpEntry=(LdapEntry)itr.next();
        }
      }
 catch (      EntityNotFoundException e) {
        if (tc.isDebugEnabled()) {
          Tr.debug(tc,METHODNAME + "String_Node_Str" + dn+ "String_Node_Str");
        }
        continue;
      }
      if (grpEntry != null && !iLdapConfigMgr.isGroup(grpEntry.getType())) {
        if (tc.isDebugEnabled()) {
          Tr.debug(tc,METHODNAME + "String_Node_Str" + dn+ "String_Node_Str");
        }
        continue;
      }
      if (grpEntry != null && entity != null) {
        createEntityFromLdapEntry(entity,SchemaConstants.DO_GROUP,grpEntry,supportedProps);
      }
      if (grpEntry != null && nested) {
        String key=dn.toLowerCase();
        DNMbrshipMap.put(key,grpEntry.getAttributes().get(mbrshipAttrName));
        groupsToDo.add(key);
      }
    }
    Map<String,LdapEntry> dynaGrpMap=null;
    Map<String,LdapURL[]> dynaGrpURLsMap=null;
    boolean findDynaGrp=iLdapConfigMgr.getMembershipAttributeScope() != LDAP_ALL_GROUP_MEMBERSHIP && iLdapConfigMgr.supportDynamicGroup();
    if (findDynaGrp) {
      dynaGrpMap=iLdapConn.getDynamicGroups(bases,supportedProps,true);
      dynaGrpURLsMap=new HashMap<String,LdapURL[]>(dynaGrpMap.size());
      Set<Map.Entry<String,LdapEntry>> dynaGrpMapEntrySet=dynaGrpMap.entrySet();
      for (      Map.Entry<String,LdapEntry> dynaEntry : dynaGrpMapEntrySet) {
        String dynaGrpDn=dynaEntry.getKey();
        String dynaGrpKey=dynaGrpDn.toLowerCase();
        LdapEntry dynaGrpEntry=dynaEntry.getValue();
        Attributes attrs=dynaGrpEntry.getAttributes();
        Attribute dynaMbrAttr=attrs.get(iLdapConfigMgr.getDynamicMemberAttribute(attrs.get(LDAP_ATTR_OBJECTCLASS)));
        if (dynaMbrAttr != null) {
          LdapURL[] ldapURLs=LdapHelper.getLdapURLs(dynaMbrAttr);
          if (ldapURLs != null && ldapURLs.length > 0) {
            dynaGrpURLsMap.put(dynaGrpDn,ldapURLs);
            if (!DNMbrshipMap.containsKey(dynaGrpKey) && iLdapConn.isMemberInURLQuery(ldapURLs,ldapEntry.getDN())) {
              if (groupDN != null && groupDN.equalsIgnoreCase(dynaGrpDn)) {
                isInGrp=true;
                return isInGrp;
              }
              if (entity != null) {
                createEntityFromLdapEntry(entity,SchemaConstants.DO_GROUP,dynaGrpEntry,supportedProps);
              }
              if (nested) {
                DNMbrshipMap.put(dynaGrpKey,dynaGrpEntry.getAttributes().get(mbrshipAttrName));
                groupsToDo.add(dynaGrpKey);
              }
            }
          }
        }
      }
    }
    if (!nested && groupDN != null) {
      return isInGrp;
    }
    while (groupsToDo.size() > 0) {
      Set<String> nextGroups=new HashSet<String>();
      for (      String dn : groupsToDo) {
        Attribute grpMbrshipAttr=DNMbrshipMap.get(dn.toLowerCase());
        if (grpMbrshipAttr != null) {
          NamingEnumeration<?> grpEnum=grpMbrshipAttr.getAll();
          while (grpEnum.hasMoreElements()) {
            String grpDn=(String)grpEnum.nextElement();
            if (grpDn != null) {
              if (groupDN != null && groupDN.equalsIgnoreCase(grpDn)) {
                isInGrp=true;
                return isInGrp;
              }
              if (!LdapHelper.isUnderBases(grpDn,bases)) {
                continue;
              }
              String grpKey=grpDn.toLowerCase();
              if (DNMbrshipMap.containsKey(grpKey)) {
                continue;
              }
              LdapEntry grpEntry=null;
              Set grpEntries=null;
              List grpTypes=iLdapConfigMgr.getGroupTypes();
              Set entityType=new HashSet(groupTypes);
              String entityTypeFilter=iLdapConfigMgr.getEntityTypesFilter(entityType);
              try {
                grpEntries=iLdapConn.searchEntities(grpDn,entityTypeFilter,null,SearchControls.OBJECT_SCOPE,grpTypes,supportedProps,true,false);
                if (grpEntries != null) {
                  Iterator itr=grpEntries.iterator();
                  while (itr.hasNext())                   grpEntry=(LdapEntry)itr.next();
                }
              }
 catch (              EntityNotFoundException e) {
                if (tc.isDebugEnabled()) {
                  Tr.debug(tc,METHODNAME + "String_Node_Str" + grpDn+ "String_Node_Str");
                }
                continue;
              }
              if (grpEntry != null && !iLdapConfigMgr.isGroup(grpEntry.getType())) {
                if (tc.isDebugEnabled()) {
                  Tr.debug(tc,METHODNAME + "String_Node_Str" + grpDn+ "String_Node_Str");
                }
                continue;
              }
              if (grpEntry != null && entity != null) {
                createEntityFromLdapEntry(entity,SchemaConstants.DO_GROUP,grpEntry,supportedProps);
              }
              if (grpEntry != null && nested) {
                DNMbrshipMap.put(grpKey,grpEntry.getAttributes().get(mbrshipAttrName));
                if (!groupsToDo.contains(grpKey)) {
                  nextGroups.add(grpKey);
                }
              }
            }
          }
        }
        if (findDynaGrp) {
          Set<Map.Entry<String,LdapURL[]>> dynaGrpUrlEntrySet=dynaGrpURLsMap.entrySet();
          for (          Map.Entry<String,LdapURL[]> dynaUrlEntry : dynaGrpUrlEntrySet) {
            String dynaGrpDn=dynaUrlEntry.getKey();
            String dynaGrpKey=dynaGrpDn.toLowerCase();
            if (!DNMbrshipMap.containsKey(dynaGrpKey) && iLdapConn.isMemberInURLQuery(dynaUrlEntry.getValue(),dn)) {
              if (groupDN != null && groupDN.equalsIgnoreCase(dynaGrpDn)) {
                isInGrp=true;
                return isInGrp;
              }
              LdapEntry dynaGrpEntry=dynaGrpMap.get(dynaGrpDn);
              if (entity != null) {
                createEntityFromLdapEntry(entity,SchemaConstants.DO_GROUP,dynaGrpEntry,supportedProps);
              }
              DNMbrshipMap.put(dynaGrpKey,dynaGrpEntry.getAttributes().get(mbrshipAttrName));
              if (!groupsToDo.contains(dynaGrpKey)) {
                nextGroups.add(dynaGrpKey);
              }
            }
          }
        }
      }
      groupsToDo=nextGroups;
    }
  }
 catch (  NamingException e) {
    throw new WIMSystemException(WIMMessageKey.NAMING_EXCEPTION,Tr.formatMessage(tc,WIMMessageKey.NAMING_EXCEPTION,WIMMessageHelper.generateMsgParms(e.toString(true))));
  }
  return isInGrp;
}

Fixed version (ground truth):
private boolean getGroupsByMembership(Entity entity,LdapEntry ldapEntry,String[] bases,int level,List<String> propNames,String groupDN) throws WIMException {
  final String METHODNAME="String_Node_Str";
  boolean nested=(level == 0 && iLdapConfigMgr.getMembershipAttributeScope() == LDAP_DIRECT_GROUP_MEMBERSHIP);
  if (!nested && (iLdapConfigMgr.isRecursiveSearch()))   nested=true;
  boolean isInGrp=false;
  List<String> supportedProps=iLdapConfigMgr.getSupportedProperties(SchemaConstants.DO_GROUP,propNames);
  String mbrshipAttrName=iLdapConfigMgr.getMembershipAttribute();
  Attribute mbrshipAttr=ldapEntry.getAttributes().get(mbrshipAttrName);
  try {
    if (mbrshipAttr == null || (mbrshipAttr.size() == 1 && mbrshipAttr.get(0) == null)) {
      if (LdapConstants.IDS_LDAP_SERVER.equalsIgnoreCase(iLdapConfigMgr.getLdapType()) && mbrshipAttrName != null && mbrshipAttrName.equalsIgnoreCase(LDAP_ATTR_IBM_ALL_GROUP)) {
        isInGrp=false;
      }
 else {
        isInGrp=getGroupsByMember(entity,ldapEntry,bases,level,supportedProps,groupDN);
      }
      return isInGrp;
    }
 else     if (mbrshipAttr.size() == 0) {
      isInGrp=false;
      return isInGrp;
    }
    Map<String,Attribute> DNMbrshipMap=null;
    if (nested) {
      DNMbrshipMap=new HashMap<String,Attribute>();
    }
    Set<String> groupsToDo=new HashSet<String>();
    List<String> groupTypes=iLdapConfigMgr.getGroupTypes();
    NamingEnumeration<?> enm=mbrshipAttr.getAll();
    while (enm.hasMoreElements()) {
      String dn=(String)enm.nextElement();
      dn=LdapHelper.getValidDN(dn);
      if (groupDN != null && groupDN.equalsIgnoreCase(dn)) {
        isInGrp=true;
        return isInGrp;
      }
      if (!LdapHelper.isUnderBases(dn,bases)) {
        continue;
      }
      List grpTypes=iLdapConfigMgr.getGroupTypes();
      Set entityType=new HashSet(groupTypes);
      String entityTypeFilter=iLdapConfigMgr.getEntityTypesFilter(entityType);
      LdapEntry grpEntry=null;
      Set grpEntries=null;
      try {
        grpEntries=iLdapConn.searchEntities(dn,entityTypeFilter,null,SearchControls.OBJECT_SCOPE,grpTypes,supportedProps,true,false);
        if (grpEntries != null) {
          Iterator itr=grpEntries.iterator();
          while (itr.hasNext())           grpEntry=(LdapEntry)itr.next();
        }
      }
 catch (      EntityNotFoundException e) {
        if (tc.isDebugEnabled()) {
          Tr.debug(tc,METHODNAME + "String_Node_Str" + dn+ "String_Node_Str");
        }
        continue;
      }
      if (grpEntry != null && !iLdapConfigMgr.isGroup(grpEntry.getType())) {
        if (tc.isDebugEnabled()) {
          Tr.debug(tc,METHODNAME + "String_Node_Str" + dn+ "String_Node_Str");
        }
        continue;
      }
      if (grpEntry != null && entity != null) {
        createEntityFromLdapEntry(entity,SchemaConstants.DO_GROUP,grpEntry,supportedProps);
      }
      if (grpEntry != null && nested) {
        String key=dn.toLowerCase();
        DNMbrshipMap.put(key,grpEntry.getAttributes().get(mbrshipAttrName));
        groupsToDo.add(key);
      }
    }
    Map<String,LdapEntry> dynaGrpMap=null;
    Map<String,LdapURL[]> dynaGrpURLsMap=null;
    boolean findDynaGrp=iLdapConfigMgr.getMembershipAttributeScope() != LDAP_ALL_GROUP_MEMBERSHIP && iLdapConfigMgr.supportDynamicGroup();
    if (findDynaGrp) {
      dynaGrpMap=iLdapConn.getDynamicGroups(bases,supportedProps,true);
      dynaGrpURLsMap=new HashMap<String,LdapURL[]>(dynaGrpMap.size());
      Set<Map.Entry<String,LdapEntry>> dynaGrpMapEntrySet=dynaGrpMap.entrySet();
      for (      Map.Entry<String,LdapEntry> dynaEntry : dynaGrpMapEntrySet) {
        String dynaGrpDn=dynaEntry.getKey();
        String dynaGrpKey=dynaGrpDn.toLowerCase();
        LdapEntry dynaGrpEntry=dynaEntry.getValue();
        Attributes attrs=dynaGrpEntry.getAttributes();
        Attribute dynaMbrAttr=attrs.get(iLdapConfigMgr.getDynamicMemberAttribute(attrs.get(LDAP_ATTR_OBJECTCLASS)));
        if (dynaMbrAttr != null) {
          LdapURL[] ldapURLs=LdapHelper.getLdapURLs(dynaMbrAttr);
          if (ldapURLs != null && ldapURLs.length > 0) {
            dynaGrpURLsMap.put(dynaGrpDn,ldapURLs);
            if (!DNMbrshipMap.containsKey(dynaGrpKey) && iLdapConn.isMemberInURLQuery(ldapURLs,ldapEntry.getDN())) {
              if (groupDN != null && groupDN.equalsIgnoreCase(dynaGrpDn)) {
                isInGrp=true;
                return isInGrp;
              }
              if (entity != null) {
                createEntityFromLdapEntry(entity,SchemaConstants.DO_GROUP,dynaGrpEntry,supportedProps);
              }
              if (nested) {
                DNMbrshipMap.put(dynaGrpKey,dynaGrpEntry.getAttributes().get(mbrshipAttrName));
                groupsToDo.add(dynaGrpKey);
              }
            }
          }
        }
      }
    }
    if (!nested && groupDN != null) {
      return isInGrp;
    }
    while (groupsToDo.size() > 0) {
      Set<String> nextGroups=new HashSet<String>();
      for (      String dn : groupsToDo) {
        Attribute grpMbrshipAttr=DNMbrshipMap.get(dn.toLowerCase());
        if (grpMbrshipAttr != null) {
          NamingEnumeration<?> grpEnum=grpMbrshipAttr.getAll();
          while (grpEnum.hasMoreElements()) {
            String grpDn=(String)grpEnum.nextElement();
            if (grpDn != null) {
              if (groupDN != null && groupDN.equalsIgnoreCase(grpDn)) {
                isInGrp=true;
                return isInGrp;
              }
              if (!LdapHelper.isUnderBases(grpDn,bases)) {
                continue;
              }
              String grpKey=grpDn.toLowerCase();
              if (DNMbrshipMap.containsKey(grpKey)) {
                continue;
              }
              LdapEntry grpEntry=null;
              Set grpEntries=null;
              List grpTypes=iLdapConfigMgr.getGroupTypes();
              Set entityType=new HashSet(groupTypes);
              String entityTypeFilter=iLdapConfigMgr.getEntityTypesFilter(entityType);
              try {
                grpEntries=iLdapConn.searchEntities(grpDn,entityTypeFilter,null,SearchControls.OBJECT_SCOPE,grpTypes,supportedProps,true,false);
                if (grpEntries != null) {
                  Iterator itr=grpEntries.iterator();
                  while (itr.hasNext())                   grpEntry=(LdapEntry)itr.next();
                }
              }
 catch (              EntityNotFoundException e) {
                if (tc.isDebugEnabled()) {
                  Tr.debug(tc,METHODNAME + "String_Node_Str" + grpDn+ "String_Node_Str");
                }
                continue;
              }
              if (grpEntry != null && !iLdapConfigMgr.isGroup(grpEntry.getType())) {
                if (tc.isDebugEnabled()) {
                  Tr.debug(tc,METHODNAME + "String_Node_Str" + grpDn+ "String_Node_Str");
                }
                continue;
              }
              if (grpEntry != null && entity != null) {
                createEntityFromLdapEntry(entity,SchemaConstants.DO_GROUP,grpEntry,supportedProps);
              }
              if (grpEntry != null && nested) {
                DNMbrshipMap.put(grpKey,grpEntry.getAttributes().get(mbrshipAttrName));
                if (!groupsToDo.contains(grpKey)) {
                  nextGroups.add(grpKey);
                }
              }
            }
          }
        }
        if (findDynaGrp) {
          Set<Map.Entry<String,LdapURL[]>> dynaGrpUrlEntrySet=dynaGrpURLsMap.entrySet();
          for (          Map.Entry<String,LdapURL[]> dynaUrlEntry : dynaGrpUrlEntrySet) {
            String dynaGrpDn=dynaUrlEntry.getKey();
            String dynaGrpKey=dynaGrpDn.toLowerCase();
            if (!DNMbrshipMap.containsKey(dynaGrpKey) && iLdapConn.isMemberInURLQuery(dynaUrlEntry.getValue(),dn)) {
              if (groupDN != null && groupDN.equalsIgnoreCase(dynaGrpDn)) {
                isInGrp=true;
                return isInGrp;
              }
              LdapEntry dynaGrpEntry=dynaGrpMap.get(dynaGrpDn);
              if (entity != null) {
                createEntityFromLdapEntry(entity,SchemaConstants.DO_GROUP,dynaGrpEntry,supportedProps);
              }
              DNMbrshipMap.put(dynaGrpKey,dynaGrpEntry.getAttributes().get(mbrshipAttrName));
              if (!groupsToDo.contains(dynaGrpKey)) {
                nextGroups.add(dynaGrpKey);
              }
            }
          }
        }
      }
      groupsToDo=nextGroups;
    }
  }
 catch (  NamingException e) {
    throw new WIMSystemException(WIMMessageKey.NAMING_EXCEPTION,Tr.formatMessage(tc,WIMMessageKey.NAMING_EXCEPTION,WIMMessageHelper.generateMsgParms(e.toString(true))));
  }
  return isInGrp;
}