Program: 51170
Project: 51170
Buggy source:
/** 
 * Finds a root member of a hierarchy with a given name.
 * @param hierarchy
 * @param memberName
 * @return Member, or null if not found
 */
public static Member lookupHierarchyRootMember(SchemaReader reader,Hierarchy hierarchy,Id.Segment memberName,MatchType matchType){
  List<Member> rootMembers=reader.getHierarchyRootMembers(hierarchy);
  Member searchMember=null;
  if (matchType != MatchType.EXACT && !hierarchy.hasAll() && !rootMembers.isEmpty()) {
    searchMember=hierarchy.createMember(null,rootMembers.get(0).getLevel(),memberName.name,null);
  }
  int bestMatch=-1;
  int k=-1;
  for (  Member rootMember : rootMembers) {
    ++k;
    int rc;
    if (matchType == MatchType.EXACT || hierarchy.hasAll()) {
      rc=rootMember.getName().compareToIgnoreCase(memberName.name);
    }
 else {
      rc=FunUtil.compareSiblingMembers(rootMember,searchMember);
    }
    if (rc == 0) {
      return rootMember;
    }
    if (!hierarchy.hasAll()) {
      if (matchType == MatchType.BEFORE) {
        if (rc < 0 && (bestMatch == -1 || FunUtil.compareSiblingMembers(rootMember,rootMembers.get(bestMatch)) > 0)) {
          bestMatch=k;
        }
      }
 else       if (matchType == MatchType.AFTER) {
        if (rc > 0 && (bestMatch == -1 || FunUtil.compareSiblingMembers(rootMember,rootMembers.get(bestMatch)) < 0)) {
          bestMatch=k;
        }
      }
    }
  }
  if (matchType != MatchType.EXACT && bestMatch != -1) {
    return rootMembers.get(bestMatch);
  }
  return (rootMembers.size() == 1 && rootMembers.get(0).isAll()) ? reader.lookupMemberChildByName(rootMembers.get(0),memberName,matchType) : null;
}

Fixed version (ground truth):
/** 
 * Finds a root member of a hierarchy with a given name.
 * @param hierarchy
 * @param memberName
 * @return Member, or null if not found
 */
public static Member lookupHierarchyRootMember(SchemaReader reader,Hierarchy hierarchy,Id.Segment memberName,MatchType matchType){
  List<Member> rootMembers=reader.getHierarchyRootMembers(hierarchy);
  Member searchMember=null;
  if (!matchType.isExact() && !hierarchy.hasAll() && !rootMembers.isEmpty()) {
    searchMember=hierarchy.createMember(null,rootMembers.get(0).getLevel(),memberName.name,null);
  }
  int bestMatch=-1;
  int k=-1;
  for (  Member rootMember : rootMembers) {
    ++k;
    int rc;
    if (matchType.isExact() || hierarchy.hasAll()) {
      rc=rootMember.getName().compareToIgnoreCase(memberName.name);
    }
 else {
      rc=FunUtil.compareSiblingMembers(rootMember,searchMember);
    }
    if (rc == 0) {
      return rootMember;
    }
    if (!hierarchy.hasAll()) {
      if (matchType == MatchType.BEFORE) {
        if (rc < 0 && (bestMatch == -1 || FunUtil.compareSiblingMembers(rootMember,rootMembers.get(bestMatch)) > 0)) {
          bestMatch=k;
        }
      }
 else       if (matchType == MatchType.AFTER) {
        if (rc > 0 && (bestMatch == -1 || FunUtil.compareSiblingMembers(rootMember,rootMembers.get(bestMatch)) < 0)) {
          bestMatch=k;
        }
      }
    }
  }
  if (matchType == MatchType.EXACT_SCHEMA) {
    return null;
  }
  if (matchType != MatchType.EXACT && bestMatch != -1) {
    return rootMembers.get(bestMatch);
  }
  return (rootMembers.size() == 1 && rootMembers.get(0).isAll()) ? reader.lookupMemberChildByName(rootMembers.get(0),memberName,matchType) : null;
}