Program: 152477
Project: 152477
Buggy source:
/** 
 * Given a specific raw contact, finds all matching aggregate contacts and chooses the one with the highest match score.  If no such contact is found, creates a new contact.
 */
private synchronized void aggregateContact(SQLiteDatabase db,long rawContactId,String accountType,String accountName,long currentContactId,MatchCandidateList candidates,ContactMatcher matcher,ContentValues values){
  int aggregationMode=RawContacts.AGGREGATION_MODE_DEFAULT;
  Integer aggModeObject=mRawContactsMarkedForAggregation.remove(rawContactId);
  if (aggModeObject != null) {
    aggregationMode=aggModeObject;
  }
  long contactId=-1;
  long contactIdToSplit=-1;
  if (aggregationMode == RawContacts.AGGREGATION_MODE_DEFAULT) {
    candidates.clear();
    matcher.clear();
    contactId=pickBestMatchBasedOnExceptions(db,rawContactId,matcher);
    if (contactId == -1) {
      contactId=pickBestMatchBasedOnData(db,rawContactId,candidates,matcher);
      if (contactId != -1 && contactId != currentContactId && containsRawContactsFromAccount(db,contactId,accountType,accountName)) {
        contactIdToSplit=contactId;
        contactId=-1;
      }
    }
  }
 else   if (aggregationMode == RawContacts.AGGREGATION_MODE_DISABLED) {
    return;
  }
  long currentContactContentsCount=0;
  if (currentContactId != 0) {
    mRawContactCountQuery.bindLong(1,currentContactId);
    mRawContactCountQuery.bindLong(2,rawContactId);
    currentContactContentsCount=mRawContactCountQuery.simpleQueryForLong();
  }
  if (contactId == -1 && currentContactId != 0 && (currentContactContentsCount == 0 || aggregationMode == RawContacts.AGGREGATION_MODE_SUSPENDED)) {
    contactId=currentContactId;
  }
  if (contactId == currentContactId) {
    markAggregated(rawContactId);
  }
 else   if (contactId == -1) {
    createNewContactForRawContact(db,rawContactId);
    if (currentContactContentsCount > 0) {
      updateAggregateData(currentContactId);
    }
  }
 else {
    if (currentContactContentsCount == 0) {
      mContactDelete.bindLong(1,currentContactId);
      mContactDelete.execute();
      mAggregatedPresenceDelete.bindLong(1,currentContactId);
      mAggregatedPresenceDelete.execute();
    }
    setContactIdAndMarkAggregated(rawContactId,contactId);
    computeAggregateData(db,contactId,mContactUpdate);
    mContactUpdate.bindLong(ContactReplaceSqlStatement.CONTACT_ID,contactId);
    mContactUpdate.execute();
    mDbHelper.updateContactVisible(contactId);
    updateAggregatedPresence(contactId);
  }
  if (contactIdToSplit != -1) {
    splitAutomaticallyAggregatedRawContacts(db,contactIdToSplit);
  }
}

Fixed version (ground truth):
/** 
 * Given a specific raw contact, finds all matching aggregate contacts and chooses the one with the highest match score.  If no such contact is found, creates a new contact.
 */
private synchronized void aggregateContact(SQLiteDatabase db,long rawContactId,String accountType,String accountName,long currentContactId,MatchCandidateList candidates,ContactMatcher matcher,ContentValues values){
  int aggregationMode=RawContacts.AGGREGATION_MODE_DEFAULT;
  Integer aggModeObject=mRawContactsMarkedForAggregation.remove(rawContactId);
  if (aggModeObject != null) {
    aggregationMode=aggModeObject;
  }
  long contactId=-1;
  long contactIdToSplit=-1;
  if (aggregationMode == RawContacts.AGGREGATION_MODE_DEFAULT) {
    candidates.clear();
    matcher.clear();
    contactId=pickBestMatchBasedOnExceptions(db,rawContactId,matcher);
    if (contactId == -1) {
      contactId=pickBestMatchBasedOnData(db,rawContactId,candidates,matcher);
      if (contactId != -1 && contactId != currentContactId && containsRawContactsFromAccount(db,contactId,accountType,accountName)) {
        contactIdToSplit=contactId;
        contactId=-1;
      }
    }
  }
 else   if (aggregationMode == RawContacts.AGGREGATION_MODE_DISABLED) {
    return;
  }
  long currentContactContentsCount=0;
  if (currentContactId != 0) {
    mRawContactCountQuery.bindLong(1,currentContactId);
    mRawContactCountQuery.bindLong(2,rawContactId);
    currentContactContentsCount=mRawContactCountQuery.simpleQueryForLong();
  }
  if (contactId == -1 && currentContactId != 0 && (currentContactContentsCount == 0 || aggregationMode == RawContacts.AGGREGATION_MODE_SUSPENDED)) {
    contactId=currentContactId;
  }
  if (contactId == currentContactId) {
    markAggregated(rawContactId);
  }
 else   if (contactId == -1) {
    createNewContactForRawContact(db,rawContactId);
    if (currentContactContentsCount > 0) {
      updateAggregateData(currentContactId);
    }
  }
 else {
    if (currentContactContentsCount == 0) {
      mContactDelete.bindLong(1,currentContactId);
      mContactDelete.execute();
      mAggregatedPresenceDelete.bindLong(1,currentContactId);
      mAggregatedPresenceDelete.execute();
    }
    setContactIdAndMarkAggregated(rawContactId,contactId);
    computeAggregateData(db,contactId,mContactUpdate);
    mContactUpdate.bindLong(ContactReplaceSqlStatement.CONTACT_ID,contactId);
    mContactUpdate.execute();
    mDbHelper.updateContactVisible(contactId);
    updateAggregatedStatusUpdate(contactId);
  }
  if (contactIdToSplit != -1) {
    splitAutomaticallyAggregatedRawContacts(db,contactIdToSplit);
  }
}