Program: 152492
Project: 152492
Buggy source:
/** 
 * Updates a raw contact display name based on data rows, e.g. structured name, organization, email etc.
 */
private void updateRawContactDisplayName(SQLiteDatabase db,long rawContactId){
  int bestDisplayNameSource=DisplayNameSources.UNDEFINED;
  NameSplitter.Name bestName=null;
  String bestDisplayName=null;
  String bestPhoneticName=null;
  int bestPhoneticNameStyle=PhoneticNameStyle.UNDEFINED;
  mSelectionArgs1[0]=String.valueOf(rawContactId);
  Cursor c=db.rawQuery(DisplayNameQuery.RAW_SQL,mSelectionArgs1);
  try {
    while (c.moveToNext()) {
      int mimeType=c.getInt(DisplayNameQuery.MIMETYPE);
      int source=getDisplayNameSource(mimeType);
      if (source < bestDisplayNameSource || source == DisplayNameSources.UNDEFINED) {
        continue;
      }
      if (source == bestDisplayNameSource && c.getInt(DisplayNameQuery.IS_PRIMARY) == 0) {
        continue;
      }
      if (mimeType == mMimeTypeIdStructuredName) {
        NameSplitter.Name name;
        if (bestName != null) {
          name=new NameSplitter.Name();
        }
 else {
          name=mName;
          name.clear();
        }
        name.prefix=c.getString(DisplayNameQuery.PREFIX);
        name.givenNames=c.getString(DisplayNameQuery.GIVEN_NAME);
        name.middleName=c.getString(DisplayNameQuery.MIDDLE_NAME);
        name.familyName=c.getString(DisplayNameQuery.FAMILY_NAME);
        name.suffix=c.getString(DisplayNameQuery.SUFFIX);
        name.fullNameStyle=c.isNull(DisplayNameQuery.FULL_NAME_STYLE) ? FullNameStyle.UNDEFINED : c.getInt(DisplayNameQuery.FULL_NAME_STYLE);
        name.phoneticFamilyName=c.getString(DisplayNameQuery.PHONETIC_FAMILY_NAME);
        name.phoneticMiddleName=c.getString(DisplayNameQuery.PHONETIC_MIDDLE_NAME);
        name.phoneticGivenName=c.getString(DisplayNameQuery.PHONETIC_GIVEN_NAME);
        name.phoneticNameStyle=c.isNull(DisplayNameQuery.PHONETIC_NAME_STYLE) ? PhoneticNameStyle.UNDEFINED : c.getInt(DisplayNameQuery.PHONETIC_NAME_STYLE);
        if (!name.isEmpty()) {
          bestDisplayNameSource=source;
          bestName=name;
        }
      }
 else       if (mimeType == mMimeTypeIdOrganization) {
        mCharArrayBuffer.sizeCopied=0;
        c.copyStringToBuffer(DisplayNameQuery.DATA1,mCharArrayBuffer);
        if (mCharArrayBuffer.sizeCopied != 0) {
          bestDisplayNameSource=source;
          bestDisplayName=new String(mCharArrayBuffer.data,0,mCharArrayBuffer.sizeCopied);
          bestPhoneticName=c.getString(DisplayNameQuery.ORGANIZATION_PHONETIC_NAME);
          bestPhoneticNameStyle=c.isNull(DisplayNameQuery.ORGANIZATION_PHONETIC_NAME_STYLE) ? PhoneticNameStyle.UNDEFINED : c.getInt(DisplayNameQuery.ORGANIZATION_PHONETIC_NAME_STYLE);
        }
 else {
          c.copyStringToBuffer(DisplayNameQuery.TITLE,mCharArrayBuffer);
          if (mCharArrayBuffer.sizeCopied != 0) {
            bestDisplayNameSource=source;
            bestDisplayName=new String(mCharArrayBuffer.data,0,mCharArrayBuffer.sizeCopied);
            bestPhoneticName=null;
            bestPhoneticNameStyle=PhoneticNameStyle.UNDEFINED;
          }
        }
      }
 else {
        mCharArrayBuffer.sizeCopied=0;
        c.copyStringToBuffer(DisplayNameQuery.DATA1,mCharArrayBuffer);
        if (mCharArrayBuffer.sizeCopied != 0) {
          bestDisplayNameSource=source;
          bestDisplayName=new String(mCharArrayBuffer.data,0,mCharArrayBuffer.sizeCopied);
          bestPhoneticName=null;
          bestPhoneticNameStyle=PhoneticNameStyle.UNDEFINED;
        }
      }
    }
  }
  finally {
    c.close();
  }
  String displayNamePrimary;
  String displayNameAlternative;
  String sortKeyPrimary=null;
  String sortKeyAlternative=null;
  int displayNameStyle=FullNameStyle.UNDEFINED;
  if (bestDisplayNameSource == DisplayNameSources.STRUCTURED_NAME) {
    displayNameStyle=bestName.fullNameStyle;
    if (displayNameStyle == FullNameStyle.CJK || displayNameStyle == FullNameStyle.UNDEFINED) {
      displayNameStyle=mNameSplitter.getAdjustedFullNameStyle(displayNameStyle);
      bestName.fullNameStyle=displayNameStyle;
    }
    displayNamePrimary=mNameSplitter.join(bestName,true);
    displayNameAlternative=mNameSplitter.join(bestName,false);
    bestPhoneticName=mNameSplitter.joinPhoneticName(bestName);
    bestPhoneticNameStyle=bestName.phoneticNameStyle;
  }
 else {
    displayNamePrimary=displayNameAlternative=bestDisplayName;
  }
  if (bestPhoneticName != null) {
    sortKeyPrimary=sortKeyAlternative=bestPhoneticName;
    if (bestPhoneticNameStyle == PhoneticNameStyle.UNDEFINED) {
      bestPhoneticNameStyle=mNameSplitter.guessPhoneticNameStyle(bestPhoneticName);
    }
  }
 else {
    if (displayNameStyle == FullNameStyle.UNDEFINED) {
      displayNameStyle=mNameSplitter.guessFullNameStyle(bestDisplayName);
      if (displayNameStyle == FullNameStyle.UNDEFINED || displayNameStyle == FullNameStyle.CJK) {
        displayNameStyle=mNameSplitter.getAdjustedNameStyleBasedOnPhoneticNameStyle(displayNameStyle,bestPhoneticNameStyle);
      }
      displayNameStyle=mNameSplitter.getAdjustedFullNameStyle(displayNameStyle);
    }
    if (displayNameStyle == FullNameStyle.CHINESE) {
      sortKeyPrimary=sortKeyAlternative=mNameSplitter.convertHanziToPinyin(displayNamePrimary);
    }
  }
  if (sortKeyPrimary == null) {
    sortKeyPrimary=displayNamePrimary;
    sortKeyAlternative=displayNameAlternative;
  }
  setDisplayName(rawContactId,bestDisplayNameSource,displayNamePrimary,displayNameAlternative,bestPhoneticName,bestPhoneticNameStyle,sortKeyPrimary,sortKeyAlternative);
}

Fixed version (ground truth):
/** 
 * Updates a raw contact display name based on data rows, e.g. structured name, organization, email etc.
 */
private void updateRawContactDisplayName(SQLiteDatabase db,long rawContactId){
  int bestDisplayNameSource=DisplayNameSources.UNDEFINED;
  NameSplitter.Name bestName=null;
  String bestDisplayName=null;
  String bestPhoneticName=null;
  int bestPhoneticNameStyle=PhoneticNameStyle.UNDEFINED;
  mSelectionArgs1[0]=String.valueOf(rawContactId);
  Cursor c=db.rawQuery(DisplayNameQuery.RAW_SQL,mSelectionArgs1);
  try {
    while (c.moveToNext()) {
      int mimeType=c.getInt(DisplayNameQuery.MIMETYPE);
      int source=getDisplayNameSource(mimeType);
      if (source < bestDisplayNameSource || source == DisplayNameSources.UNDEFINED) {
        continue;
      }
      if (source == bestDisplayNameSource && c.getInt(DisplayNameQuery.IS_PRIMARY) == 0) {
        continue;
      }
      if (mimeType == mMimeTypeIdStructuredName) {
        NameSplitter.Name name;
        if (bestName != null) {
          name=new NameSplitter.Name();
        }
 else {
          name=mName;
          name.clear();
        }
        name.prefix=c.getString(DisplayNameQuery.PREFIX);
        name.givenNames=c.getString(DisplayNameQuery.GIVEN_NAME);
        name.middleName=c.getString(DisplayNameQuery.MIDDLE_NAME);
        name.familyName=c.getString(DisplayNameQuery.FAMILY_NAME);
        name.suffix=c.getString(DisplayNameQuery.SUFFIX);
        name.fullNameStyle=c.isNull(DisplayNameQuery.FULL_NAME_STYLE) ? FullNameStyle.UNDEFINED : c.getInt(DisplayNameQuery.FULL_NAME_STYLE);
        name.phoneticFamilyName=c.getString(DisplayNameQuery.PHONETIC_FAMILY_NAME);
        name.phoneticMiddleName=c.getString(DisplayNameQuery.PHONETIC_MIDDLE_NAME);
        name.phoneticGivenName=c.getString(DisplayNameQuery.PHONETIC_GIVEN_NAME);
        name.phoneticNameStyle=c.isNull(DisplayNameQuery.PHONETIC_NAME_STYLE) ? PhoneticNameStyle.UNDEFINED : c.getInt(DisplayNameQuery.PHONETIC_NAME_STYLE);
        if (!name.isEmpty()) {
          bestDisplayNameSource=source;
          bestName=name;
        }
      }
 else       if (mimeType == mMimeTypeIdOrganization) {
        mCharArrayBuffer.sizeCopied=0;
        c.copyStringToBuffer(DisplayNameQuery.DATA1,mCharArrayBuffer);
        if (mCharArrayBuffer.sizeCopied != 0) {
          bestDisplayNameSource=source;
          bestDisplayName=new String(mCharArrayBuffer.data,0,mCharArrayBuffer.sizeCopied);
          bestPhoneticName=c.getString(DisplayNameQuery.ORGANIZATION_PHONETIC_NAME);
          bestPhoneticNameStyle=c.isNull(DisplayNameQuery.ORGANIZATION_PHONETIC_NAME_STYLE) ? PhoneticNameStyle.UNDEFINED : c.getInt(DisplayNameQuery.ORGANIZATION_PHONETIC_NAME_STYLE);
        }
 else {
          c.copyStringToBuffer(DisplayNameQuery.TITLE,mCharArrayBuffer);
          if (mCharArrayBuffer.sizeCopied != 0) {
            bestDisplayNameSource=source;
            bestDisplayName=new String(mCharArrayBuffer.data,0,mCharArrayBuffer.sizeCopied);
            bestPhoneticName=null;
            bestPhoneticNameStyle=PhoneticNameStyle.UNDEFINED;
          }
        }
      }
 else {
        mCharArrayBuffer.sizeCopied=0;
        c.copyStringToBuffer(DisplayNameQuery.DATA1,mCharArrayBuffer);
        if (mCharArrayBuffer.sizeCopied != 0) {
          bestDisplayNameSource=source;
          bestDisplayName=new String(mCharArrayBuffer.data,0,mCharArrayBuffer.sizeCopied);
          bestPhoneticName=null;
          bestPhoneticNameStyle=PhoneticNameStyle.UNDEFINED;
        }
      }
    }
  }
  finally {
    c.close();
  }
  String displayNamePrimary;
  String displayNameAlternative;
  String sortKeyPrimary=null;
  String sortKeyAlternative=null;
  int displayNameStyle=FullNameStyle.UNDEFINED;
  if (bestDisplayNameSource == DisplayNameSources.STRUCTURED_NAME) {
    displayNameStyle=bestName.fullNameStyle;
    if (displayNameStyle == FullNameStyle.CJK || displayNameStyle == FullNameStyle.UNDEFINED) {
      displayNameStyle=mNameSplitter.getAdjustedFullNameStyle(displayNameStyle);
      bestName.fullNameStyle=displayNameStyle;
    }
    displayNamePrimary=mNameSplitter.join(bestName,true);
    displayNameAlternative=mNameSplitter.join(bestName,false);
    bestPhoneticName=mNameSplitter.joinPhoneticName(bestName);
    bestPhoneticNameStyle=bestName.phoneticNameStyle;
  }
 else {
    displayNamePrimary=displayNameAlternative=bestDisplayName;
  }
  if (bestPhoneticName != null) {
    sortKeyPrimary=sortKeyAlternative=bestPhoneticName;
    if (bestPhoneticNameStyle == PhoneticNameStyle.UNDEFINED) {
      bestPhoneticNameStyle=mNameSplitter.guessPhoneticNameStyle(bestPhoneticName);
    }
  }
 else {
    if (displayNameStyle == FullNameStyle.UNDEFINED) {
      displayNameStyle=mNameSplitter.guessFullNameStyle(bestDisplayName);
      if (displayNameStyle == FullNameStyle.UNDEFINED || displayNameStyle == FullNameStyle.CJK) {
        displayNameStyle=mNameSplitter.getAdjustedNameStyleBasedOnPhoneticNameStyle(displayNameStyle,bestPhoneticNameStyle);
      }
      displayNameStyle=mNameSplitter.getAdjustedFullNameStyle(displayNameStyle);
    }
    if (displayNameStyle == FullNameStyle.CHINESE) {
      sortKeyPrimary=sortKeyAlternative=ContactLocaleUtils.getSortKey(displayNamePrimary,FullNameStyle.CHINESE);
    }
  }
  if (sortKeyPrimary == null) {
    sortKeyPrimary=displayNamePrimary;
    sortKeyAlternative=displayNameAlternative;
  }
  setDisplayName(rawContactId,bestDisplayNameSource,displayNamePrimary,displayNameAlternative,bestPhoneticName,bestPhoneticNameStyle,sortKeyPrimary,sortKeyAlternative);
}