Program: 8370
Project: 8370
Buggy source:
public static void writeKeysToContacts(Context context){
  ContentResolver resolver=context.getContentResolver();
  Cursor cursor=resolver.query(KeychainContract.KeyRings.buildUnifiedKeyRingsUri(),KEYS_TO_CONTACT_PROJECTION,null,null,null);
  if (cursor != null) {
    while (cursor.moveToNext()) {
      String[] userId=PgpKeyHelper.splitUserId(cursor.getString(0));
      String fingerprint=PgpKeyHelper.convertFingerprintToHex(cursor.getBlob(1));
      String keyIdShort=PgpKeyHelper.convertKeyIdToHexShort(cursor.getLong(2));
      long masterKeyId=cursor.getLong(3);
      int rawContactId=-1;
      Cursor raw=resolver.query(ContactsContract.RawContacts.CONTENT_URI,RAW_CONTACT_ID_PROJECTION,FIND_RAW_CONTACT_SELECTION,new String[]{Constants.PACKAGE_NAME,fingerprint},null,null);
      if (raw != null) {
        if (raw.moveToNext()) {
          rawContactId=raw.getInt(0);
        }
        raw.close();
      }
      ArrayList<ContentProviderOperation> ops=new ArrayList<ContentProviderOperation>();
      if (rawContactId == -1) {
        ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI).withValue(ContactsContract.RawContacts.ACCOUNT_NAME,context.getString(R.string.app_name)).withValue(ContactsContract.RawContacts.ACCOUNT_TYPE,Constants.PACKAGE_NAME).withValue(ContactsContract.RawContacts.SOURCE_ID,fingerprint).build());
        if (userId[0] != null) {
          ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,0).withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE).withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,userId[0]).build());
        }
        if (userId[1] != null) {
          ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,0).withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE).withValue(ContactsContract.CommonDataKinds.Email.DATA,userId[1]).build());
        }
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,0).withValue(ContactsContract.Data.MIMETYPE,Constants.CUSTOM_CONTACT_DATA_MIME_TYPE).withValue(ContactsContract.Data.DATA1,String.format(context.getString(R.string.contact_show_key),keyIdShort)).withValue(ContactsContract.Data.DATA2,masterKeyId).build());
      }
      try {
        resolver.applyBatch(ContactsContract.AUTHORITY,ops);
      }
 catch (      RemoteException e) {
        e.printStackTrace();
      }
catch (      OperationApplicationException e) {
        e.printStackTrace();
      }
    }
    cursor.close();
  }
}

Fixed version (ground truth):
public static void writeKeysToContacts(Context context){
  ContentResolver resolver=context.getContentResolver();
  Cursor cursor=resolver.query(KeychainContract.KeyRings.buildUnifiedKeyRingsUri(),KEYS_TO_CONTACT_PROJECTION,null,null,null);
  if (cursor != null) {
    while (cursor.moveToNext()) {
      String[] userId=KeyRing.splitUserId(cursor.getString(0));
      String fingerprint=PgpKeyHelper.convertFingerprintToHex(cursor.getBlob(1));
      String keyIdShort=PgpKeyHelper.convertKeyIdToHexShort(cursor.getLong(2));
      long masterKeyId=cursor.getLong(3);
      int rawContactId=-1;
      Cursor raw=resolver.query(ContactsContract.RawContacts.CONTENT_URI,RAW_CONTACT_ID_PROJECTION,FIND_RAW_CONTACT_SELECTION,new String[]{Constants.PACKAGE_NAME,fingerprint},null,null);
      if (raw != null) {
        if (raw.moveToNext()) {
          rawContactId=raw.getInt(0);
        }
        raw.close();
      }
      ArrayList<ContentProviderOperation> ops=new ArrayList<ContentProviderOperation>();
      if (rawContactId == -1) {
        ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI).withValue(ContactsContract.RawContacts.ACCOUNT_NAME,context.getString(R.string.app_name)).withValue(ContactsContract.RawContacts.ACCOUNT_TYPE,Constants.PACKAGE_NAME).withValue(ContactsContract.RawContacts.SOURCE_ID,fingerprint).build());
        if (userId[0] != null) {
          ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,0).withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE).withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,userId[0]).build());
        }
        if (userId[1] != null) {
          ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,0).withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE).withValue(ContactsContract.CommonDataKinds.Email.DATA,userId[1]).build());
        }
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,0).withValue(ContactsContract.Data.MIMETYPE,Constants.CUSTOM_CONTACT_DATA_MIME_TYPE).withValue(ContactsContract.Data.DATA1,String.format(context.getString(R.string.contact_show_key),keyIdShort)).withValue(ContactsContract.Data.DATA2,masterKeyId).build());
      }
      try {
        resolver.applyBatch(ContactsContract.AUTHORITY,ops);
      }
 catch (      RemoteException e) {
        e.printStackTrace();
      }
catch (      OperationApplicationException e) {
        e.printStackTrace();
      }
    }
    cursor.close();
  }
}