Program: 152473
Project: 152473
Buggy source:
/** 
 * Loads contacts with specified IDs and returns them in the order of IDs in the supplied list.
 */
private Cursor queryMatchingContacts(SQLiteQueryBuilder qb,SQLiteDatabase db,long contactId,String[] projection,List<MatchScore> bestMatches,int maxSuggestions,String filter){
  StringBuilder sb=new StringBuilder();
  sb.append(Contacts._ID);
  sb.append("String_Node_Str");
  for (int i=0; i < bestMatches.size(); i++) {
    MatchScore matchScore=bestMatches.get(i);
    if (i != 0) {
      sb.append("String_Node_Str");
    }
    sb.append(matchScore.getContactId());
  }
  sb.append("String_Node_Str");
  if (!TextUtils.isEmpty(filter)) {
    sb.append("String_Node_Str" + Contacts._ID + "String_Node_Str");
    mContactsProvider.appendContactFilterAsNestedQuery(sb,filter);
  }
  HashSet<Long> foundIds=new HashSet<Long>();
  Cursor cursor=db.query(qb.getTables(),ContactIdQuery.COLUMNS,sb.toString(),null,null,null,null);
  try {
    while (cursor.moveToNext()) {
      foundIds.add(cursor.getLong(ContactIdQuery._ID));
    }
  }
  finally {
    cursor.close();
  }
  Iterator<MatchScore> iter=bestMatches.iterator();
  while (iter.hasNext()) {
    long id=iter.next().getContactId();
    if (!foundIds.contains(id)) {
      iter.remove();
    }
  }
  if (bestMatches.size() > maxSuggestions) {
    bestMatches=bestMatches.subList(0,maxSuggestions);
  }
  sb.setLength(0);
  sb.append(Contacts._ID);
  sb.append("String_Node_Str");
  for (int i=0; i < bestMatches.size(); i++) {
    MatchScore matchScore=bestMatches.get(i);
    if (i != 0) {
      sb.append("String_Node_Str");
    }
    sb.append(matchScore.getContactId());
  }
  sb.append("String_Node_Str");
  cursor=qb.query(db,projection,sb.toString(),null,null,null,Contacts._ID);
  ArrayList<Long> sortedContactIds=new ArrayList<Long>(bestMatches.size());
  for (  MatchScore matchScore : bestMatches) {
    sortedContactIds.add(matchScore.getContactId());
  }
  Collections.sort(sortedContactIds);
  int[] positionMap=new int[bestMatches.size()];
  for (int i=0; i < positionMap.length; i++) {
    long id=bestMatches.get(i).getContactId();
    positionMap[i]=sortedContactIds.indexOf(id);
  }
  return new ReorderingCursorWrapper(cursor,positionMap);
}

Fixed version (ground truth):
/** 
 * Loads contacts with specified IDs and returns them in the order of IDs in the supplied list.
 */
private Cursor queryMatchingContacts(SQLiteQueryBuilder qb,SQLiteDatabase db,String[] projection,List<MatchScore> bestMatches,int maxSuggestions,String filter){
  StringBuilder sb=new StringBuilder();
  sb.append(Contacts._ID);
  sb.append("String_Node_Str");
  for (int i=0; i < bestMatches.size(); i++) {
    MatchScore matchScore=bestMatches.get(i);
    if (i != 0) {
      sb.append("String_Node_Str");
    }
    sb.append(matchScore.getContactId());
  }
  sb.append("String_Node_Str");
  if (!TextUtils.isEmpty(filter)) {
    sb.append("String_Node_Str" + Contacts._ID + "String_Node_Str");
    mContactsProvider.appendContactFilterAsNestedQuery(sb,filter);
  }
  HashSet<Long> foundIds=new HashSet<Long>();
  Cursor cursor=db.query(qb.getTables(),ContactIdQuery.COLUMNS,sb.toString(),null,null,null,null);
  try {
    while (cursor.moveToNext()) {
      foundIds.add(cursor.getLong(ContactIdQuery._ID));
    }
  }
  finally {
    cursor.close();
  }
  Iterator<MatchScore> iter=bestMatches.iterator();
  while (iter.hasNext()) {
    long id=iter.next().getContactId();
    if (!foundIds.contains(id)) {
      iter.remove();
    }
  }
  if (bestMatches.size() > maxSuggestions) {
    bestMatches=bestMatches.subList(0,maxSuggestions);
  }
  sb.setLength(0);
  sb.append(Contacts._ID);
  sb.append("String_Node_Str");
  for (int i=0; i < bestMatches.size(); i++) {
    MatchScore matchScore=bestMatches.get(i);
    if (i != 0) {
      sb.append("String_Node_Str");
    }
    sb.append(matchScore.getContactId());
  }
  sb.append("String_Node_Str");
  cursor=qb.query(db,projection,sb.toString(),null,null,null,Contacts._ID);
  ArrayList<Long> sortedContactIds=new ArrayList<Long>(bestMatches.size());
  for (  MatchScore matchScore : bestMatches) {
    sortedContactIds.add(matchScore.getContactId());
  }
  Collections.sort(sortedContactIds);
  int[] positionMap=new int[bestMatches.size()];
  for (int i=0; i < positionMap.length; i++) {
    long id=bestMatches.get(i).getContactId();
    positionMap[i]=sortedContactIds.indexOf(id);
  }
  return new ReorderingCursorWrapper(cursor,positionMap);
}