Program: 152396
Project: 152396
Buggy source:
/** 
 * Re-aggregate rawContact of   {@code rawContactId} and all the raw contacts of{@code matchingCandidates} into connected components. This only happens when a givenraw contacts cannot be joined with its best matching contacts directly. Two raw contacts are considered connected if they share at least one email address, phone number or identity. Create new contact for each connected component except the very first one that doesn't contain rawContactId of  {@code rawContactId}.
 */
private void reAggregateRawContacts(TransactionContext txContext,SQLiteDatabase db,long currentCidForRawContact,long rawContactId,long accountId,long currentContactContentsCount,RawContactMatchingCandidates matchingCandidates){
  final Set<Long> allIds=new HashSet<>();
  allIds.add(rawContactId);
  allIds.addAll(matchingCandidates.getRawContactIdSet());
  final Set<Set<Long>> connectedRawContactSets=findConnectedRawContacts(db,allIds);
  final Map<Long,Long> rawContactsToAccounts=matchingCandidates.getRawContactToAccount();
  rawContactsToAccounts.put(rawContactId,accountId);
  ContactAggregatorHelper.mergeComponentsWithDisjointAccounts(connectedRawContactSets,rawContactsToAccounts);
  breakComponentsByExceptions(db,connectedRawContactSets);
  for (  Set<Long> connectedRawContactIds : connectedRawContactSets) {
    Long contactId=null;
    Set<Long> cidsNeedToBeUpdated=new HashSet<>();
    if (connectedRawContactIds.contains(rawContactId)) {
      if (currentCidForRawContact != 0 && (currentContactContentsCount == 0) || canBeReused(db,currentCidForRawContact,connectedRawContactIds)) {
        contactId=currentCidForRawContact;
      }
 else       if (currentCidForRawContact != 0) {
        cidsNeedToBeUpdated.add(currentCidForRawContact);
      }
    }
 else {
      boolean foundContactId=false;
      for (      Long connectedRawContactId : connectedRawContactIds) {
        Long currentContactId=matchingCandidates.getContactId(connectedRawContactId);
        if (!foundContactId && currentContactId != null && canBeReused(db,currentContactId,connectedRawContactIds)) {
          contactId=currentContactId;
          foundContactId=true;
        }
 else {
          cidsNeedToBeUpdated.add(currentContactId);
        }
      }
    }
    createContactForRawContacts(db,txContext,connectedRawContactIds,contactId);
    clearSuperPrimarySetting(db,TextUtils.join("String_Node_Str",connectedRawContactIds));
    for (    Long cid : cidsNeedToBeUpdated) {
      long currentRcCount=0;
      if (cid != 0) {
        mRawContactCountQuery.bindLong(1,cid);
        mRawContactCountQuery.bindLong(2,0);
        currentRcCount=mRawContactCountQuery.simpleQueryForLong();
      }
      if (currentRcCount == 0) {
        ContactsTableUtil.deleteContact(db,cid);
        mAggregatedPresenceDelete.bindLong(1,cid);
        mAggregatedPresenceDelete.execute();
      }
 else {
        updateAggregateData(txContext,cid);
      }
    }
  }
}

Fixed version (ground truth):
/** 
 * Re-aggregate rawContact of   {@code rawContactId} and all the raw contacts of{@code matchingCandidates} into connected components. This only happens when a givenraw contacts cannot be joined with its best matching contacts directly. Two raw contacts are considered connected if they share at least one email address, phone number or identity. Create new contact for each connected component except the very first one that doesn't contain rawContactId of  {@code rawContactId}.
 */
private void reAggregateRawContacts(TransactionContext txContext,SQLiteDatabase db,long currentCidForRawContact,long rawContactId,long accountId,long currentContactContentsCount,RawContactMatchingCandidates matchingCandidates){
  final Set<Long> allIds=new HashSet<>();
  allIds.add(rawContactId);
  allIds.addAll(matchingCandidates.getRawContactIdSet());
  final Set<Set<Long>> connectedRawContactSets=findConnectedRawContacts(db,allIds);
  final Map<Long,Long> rawContactsToAccounts=matchingCandidates.getRawContactToAccount();
  rawContactsToAccounts.put(rawContactId,accountId);
  ContactAggregatorHelper.mergeComponentsWithDisjointAccounts(connectedRawContactSets,rawContactsToAccounts);
  breakComponentsByExceptions(db,connectedRawContactSets);
  for (  Set<Long> connectedRawContactIds : connectedRawContactSets) {
    Long contactId=null;
    Set<Long> cidsNeedToBeUpdated=new HashSet<>();
    if (connectedRawContactIds.contains(rawContactId)) {
      if (currentCidForRawContact != 0 && (currentContactContentsCount == 0) || canBeReused(db,currentCidForRawContact,connectedRawContactIds)) {
        contactId=currentCidForRawContact;
        for (        Long connectedRawContactId : connectedRawContactIds) {
          Long cid=matchingCandidates.getContactId(connectedRawContactId);
          if (cid != null && cid != contactId) {
            cidsNeedToBeUpdated.add(cid);
          }
        }
      }
 else       if (currentCidForRawContact != 0) {
        cidsNeedToBeUpdated.add(currentCidForRawContact);
      }
    }
 else {
      boolean foundContactId=false;
      for (      Long connectedRawContactId : connectedRawContactIds) {
        Long currentContactId=matchingCandidates.getContactId(connectedRawContactId);
        if (!foundContactId && currentContactId != null && canBeReused(db,currentContactId,connectedRawContactIds)) {
          contactId=currentContactId;
          foundContactId=true;
        }
 else {
          cidsNeedToBeUpdated.add(currentContactId);
        }
      }
    }
    clearSuperPrimarySetting(db,TextUtils.join("String_Node_Str",connectedRawContactIds));
    createContactForRawContacts(db,txContext,connectedRawContactIds,contactId);
    for (    Long cid : cidsNeedToBeUpdated) {
      long currentRcCount=0;
      if (cid != 0) {
        mRawContactCountQuery.bindLong(1,cid);
        mRawContactCountQuery.bindLong(2,0);
        currentRcCount=mRawContactCountQuery.simpleQueryForLong();
      }
      if (currentRcCount == 0) {
        ContactsTableUtil.deleteContact(db,cid);
        mAggregatedPresenceDelete.bindLong(1,cid);
        mAggregatedPresenceDelete.execute();
      }
 else {
        updateAggregateData(txContext,cid);
      }
    }
  }
}