Program: 152370
Project: 152370
Buggy source:
private int updateRawContact(SQLiteDatabase db,long rawContactId,ContentValues values,boolean callerIsSyncAdapter,boolean callerIsMetadataSyncAdapter){
  final String selection=RawContactsColumns.CONCRETE_ID + "String_Node_Str";
  mSelectionArgs1[0]=Long.toString(rawContactId);
  final ContactsDatabaseHelper dbHelper=mDbHelper.get();
  final boolean requestUndoDelete=flagIsClear(values,RawContacts.DELETED);
  final boolean isAccountNameChanging=values.containsKey(RawContacts.ACCOUNT_NAME);
  final boolean isAccountTypeChanging=values.containsKey(RawContacts.ACCOUNT_TYPE);
  final boolean isDataSetChanging=values.containsKey(RawContacts.DATA_SET);
  final boolean isAccountChanging=isAccountNameChanging || isAccountTypeChanging || isDataSetChanging;
  int previousDeleted=0;
  long accountId=0;
  String oldAccountType=null;
  String oldAccountName=null;
  String oldDataSet=null;
  if (requestUndoDelete || isAccountChanging) {
    Cursor cursor=db.query(RawContactsQuery.TABLE,RawContactsQuery.COLUMNS,selection,mSelectionArgs1,null,null,null);
    try {
      if (cursor.moveToFirst()) {
        previousDeleted=cursor.getInt(RawContactsQuery.DELETED);
        accountId=cursor.getLong(RawContactsQuery.ACCOUNT_ID);
        oldAccountType=cursor.getString(RawContactsQuery.ACCOUNT_TYPE);
        oldAccountName=cursor.getString(RawContactsQuery.ACCOUNT_NAME);
        oldDataSet=cursor.getString(RawContactsQuery.DATA_SET);
      }
    }
  finally {
      cursor.close();
    }
    if (isAccountChanging) {
      final ContentValues originalValues=values;
      values=new ContentValues();
      values.clear();
      values.putAll(originalValues);
      final AccountWithDataSet newAccountWithDataSet=AccountWithDataSet.get(isAccountNameChanging ? values.getAsString(RawContacts.ACCOUNT_NAME) : oldAccountName,isAccountTypeChanging ? values.getAsString(RawContacts.ACCOUNT_TYPE) : oldAccountType,isDataSetChanging ? values.getAsString(RawContacts.DATA_SET) : oldDataSet);
      accountId=dbHelper.getOrCreateAccountIdInTransaction(newAccountWithDataSet);
      values.put(RawContactsColumns.ACCOUNT_ID,accountId);
      values.remove(RawContacts.ACCOUNT_NAME);
      values.remove(RawContacts.ACCOUNT_TYPE);
      values.remove(RawContacts.DATA_SET);
    }
  }
  if (requestUndoDelete) {
    values.put(ContactsContract.RawContacts.AGGREGATION_MODE,ContactsContract.RawContacts.AGGREGATION_MODE_DEFAULT);
  }
  int count=db.update(Tables.RAW_CONTACTS,values,selection,mSelectionArgs1);
  if (count != 0) {
    final AbstractContactAggregator aggregator=mAggregator.get();
    int aggregationMode=getIntValue(values,RawContacts.AGGREGATION_MODE,RawContacts.AGGREGATION_MODE_DEFAULT);
    if (aggregationMode != RawContacts.AGGREGATION_MODE_DEFAULT) {
      aggregator.markForAggregation(rawContactId,aggregationMode,false);
    }
    if (shouldMarkMetadataDirtyForRawContact(values)) {
      mTransactionContext.get().markRawContactMetadataDirty(rawContactId,callerIsMetadataSyncAdapter);
    }
    if (flagExists(values,RawContacts.STARRED)) {
      if (!callerIsSyncAdapter) {
        updateFavoritesMembership(rawContactId,flagIsSet(values,RawContacts.STARRED));
      }
      aggregator.updateStarred(rawContactId);
      aggregator.updatePinned(rawContactId);
    }
 else {
      if (!callerIsSyncAdapter && isAccountChanging) {
        boolean starred=0 != DatabaseUtils.longForQuery(db,SELECTION_STARRED_FROM_RAW_CONTACTS,new String[]{Long.toString(rawContactId)});
        updateFavoritesMembership(rawContactId,starred);
      }
    }
    if (!callerIsSyncAdapter && isAccountChanging) {
      addAutoAddMembership(rawContactId);
    }
    if (values.containsKey(RawContacts.SOURCE_ID)) {
      aggregator.updateLookupKeyForRawContact(db,rawContactId);
    }
    if (requestUndoDelete && previousDeleted == 1) {
      mTransactionContext.get().rawContactInserted(rawContactId,accountId);
    }
    mTransactionContext.get().markRawContactChangedOrDeletedOrInserted(rawContactId);
  }
  return count;
}

Fixed version (ground truth):
private int updateRawContact(SQLiteDatabase db,long rawContactId,ContentValues values,boolean callerIsSyncAdapter,boolean callerIsMetadataSyncAdapter){
  final String selection=RawContactsColumns.CONCRETE_ID + "String_Node_Str";
  mSelectionArgs1[0]=Long.toString(rawContactId);
  final ContactsDatabaseHelper dbHelper=mDbHelper.get();
  final boolean requestUndoDelete=flagIsClear(values,RawContacts.DELETED);
  final boolean isAccountNameChanging=values.containsKey(RawContacts.ACCOUNT_NAME);
  final boolean isAccountTypeChanging=values.containsKey(RawContacts.ACCOUNT_TYPE);
  final boolean isDataSetChanging=values.containsKey(RawContacts.DATA_SET);
  final boolean isAccountChanging=isAccountNameChanging || isAccountTypeChanging || isDataSetChanging;
  final boolean isBackupIdChanging=values.containsKey(RawContacts.BACKUP_ID);
  int previousDeleted=0;
  long accountId=0;
  String oldAccountType=null;
  String oldAccountName=null;
  String oldDataSet=null;
  if (requestUndoDelete || isAccountChanging) {
    Cursor cursor=db.query(RawContactsQuery.TABLE,RawContactsQuery.COLUMNS,selection,mSelectionArgs1,null,null,null);
    try {
      if (cursor.moveToFirst()) {
        previousDeleted=cursor.getInt(RawContactsQuery.DELETED);
        accountId=cursor.getLong(RawContactsQuery.ACCOUNT_ID);
        oldAccountType=cursor.getString(RawContactsQuery.ACCOUNT_TYPE);
        oldAccountName=cursor.getString(RawContactsQuery.ACCOUNT_NAME);
        oldDataSet=cursor.getString(RawContactsQuery.DATA_SET);
      }
    }
  finally {
      cursor.close();
    }
    if (isAccountChanging) {
      final ContentValues originalValues=values;
      values=new ContentValues();
      values.clear();
      values.putAll(originalValues);
      final AccountWithDataSet newAccountWithDataSet=AccountWithDataSet.get(isAccountNameChanging ? values.getAsString(RawContacts.ACCOUNT_NAME) : oldAccountName,isAccountTypeChanging ? values.getAsString(RawContacts.ACCOUNT_TYPE) : oldAccountType,isDataSetChanging ? values.getAsString(RawContacts.DATA_SET) : oldDataSet);
      accountId=dbHelper.getOrCreateAccountIdInTransaction(newAccountWithDataSet);
      values.put(RawContactsColumns.ACCOUNT_ID,accountId);
      values.remove(RawContacts.ACCOUNT_NAME);
      values.remove(RawContacts.ACCOUNT_TYPE);
      values.remove(RawContacts.DATA_SET);
    }
  }
  if (requestUndoDelete) {
    values.put(ContactsContract.RawContacts.AGGREGATION_MODE,ContactsContract.RawContacts.AGGREGATION_MODE_DEFAULT);
  }
  int count=db.update(Tables.RAW_CONTACTS,values,selection,mSelectionArgs1);
  if (count != 0) {
    final AbstractContactAggregator aggregator=mAggregator.get();
    int aggregationMode=getIntValue(values,RawContacts.AGGREGATION_MODE,RawContacts.AGGREGATION_MODE_DEFAULT);
    if (aggregationMode != RawContacts.AGGREGATION_MODE_DEFAULT) {
      aggregator.markForAggregation(rawContactId,aggregationMode,false);
    }
    if (shouldMarkMetadataDirtyForRawContact(values)) {
      mTransactionContext.get().markRawContactMetadataDirty(rawContactId,callerIsMetadataSyncAdapter);
    }
    if (isBackupIdChanging) {
      Cursor cursor=db.query(Tables.RAW_CONTACTS,new String[]{RawContactsColumns.CONCRETE_METADATA_DIRTY},selection,mSelectionArgs1,null,null,null);
      int metadataDirty=0;
      try {
        if (cursor.moveToFirst()) {
          metadataDirty=cursor.getInt(0);
        }
      }
  finally {
        cursor.close();
      }
      if (metadataDirty == 1) {
        mTransactionContext.get().markRawContactMetadataDirty(rawContactId,callerIsMetadataSyncAdapter);
      }
 else {
        mTransactionContext.get().markBackupIdChangedRawContact(rawContactId);
      }
    }
    if (flagExists(values,RawContacts.STARRED)) {
      if (!callerIsSyncAdapter) {
        updateFavoritesMembership(rawContactId,flagIsSet(values,RawContacts.STARRED));
      }
      aggregator.updateStarred(rawContactId);
      aggregator.updatePinned(rawContactId);
    }
 else {
      if (!callerIsSyncAdapter && isAccountChanging) {
        boolean starred=0 != DatabaseUtils.longForQuery(db,SELECTION_STARRED_FROM_RAW_CONTACTS,new String[]{Long.toString(rawContactId)});
        updateFavoritesMembership(rawContactId,starred);
      }
    }
    if (!callerIsSyncAdapter && isAccountChanging) {
      addAutoAddMembership(rawContactId);
    }
    if (values.containsKey(RawContacts.SOURCE_ID)) {
      aggregator.updateLookupKeyForRawContact(db,rawContactId);
    }
    if (requestUndoDelete && previousDeleted == 1) {
      mTransactionContext.get().rawContactInserted(rawContactId,accountId);
    }
    mTransactionContext.get().markRawContactChangedOrDeletedOrInserted(rawContactId);
  }
  return count;
}