Program: 152422
Project: 152422
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(TransactionContext txContext,SQLiteDatabase db,long rawContactId,String accountType,String accountName,String dataSet,long currentContactId,MatchCandidateList candidates,ContactMatcher matcher){
  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) {
      if (currentContactId == 0 || mDbHelper.isContactInDefaultDirectory(db,currentContactId)) {
        contactId=pickBestMatchBasedOnData(db,rawContactId,candidates,matcher);
      }
      if (contactId != -1 && contactId != currentContactId && containsRawContactsFromAccount(db,contactId,accountType,accountName,dataSet)) {
        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(txContext,db,rawContactId);
    if (currentContactContentsCount > 0) {
      updateAggregateData(txContext,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(txContext,contactId);
    updateAggregatedStatusUpdate(contactId);
  }
  if (contactIdToSplit != -1) {
    splitAutomaticallyAggregatedRawContacts(txContext,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(TransactionContext txContext,SQLiteDatabase db,long rawContactId,String accountType,String accountName,String dataSet,long currentContactId,MatchCandidateList candidates,ContactMatcher matcher){
  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) {
      if (currentContactId == 0 || mDbHelper.isContactInDefaultDirectory(db,currentContactId)) {
        contactId=pickBestMatchBasedOnData(db,rawContactId,candidates,matcher);
      }
      if (contactId != -1 && contactId != currentContactId && containsRawContactsFromAccount(db,contactId,accountType,accountName,dataSet)) {
        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(txContext,db,rawContactId);
    if (currentContactContentsCount > 0) {
      updateAggregateData(txContext,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(txContext,contactId);
    updateAggregatedStatusUpdate(contactId);
    if (currentContactId != 0) {
      updateAggregateData(txContext,currentContactId);
    }
  }
  if (contactIdToSplit != -1) {
    splitAutomaticallyAggregatedRawContacts(txContext,db,contactIdToSplit);
  }
}