Program: 8351
Project: 8351
Buggy source:
private ExportResult exportKeyRings(OperationLog log,long[] masterKeyIds,boolean exportSecret,OutputStream outStream){
  int okSecret=0, okPublic=0, progress=0;
  try {
    String selection=null, ids[]=null;
    if (masterKeyIds != null) {
      ids=new String[masterKeyIds.length];
      StringBuilder placeholders=new StringBuilder("String_Node_Str");
      for (int i=0; i < masterKeyIds.length; i++) {
        ids[i]=Long.toString(masterKeyIds[i]);
        if (i != 0) {
          placeholders.append("String_Node_Str");
        }
      }
      selection=Tables.KEY_RINGS_PUBLIC + "String_Node_Str" + KeyRings.MASTER_KEY_ID+ "String_Node_Str"+ placeholders+ "String_Node_Str";
    }
    Cursor cursor=mProviderHelper.getContentResolver().query(KeyRings.buildUnifiedKeyRingsUri(),new String[]{KeyRings.MASTER_KEY_ID,KeyRings.PUBKEY_DATA,KeyRings.PRIVKEY_DATA,KeyRings.HAS_ANY_SECRET},selection,ids,Tables.KEYS + "String_Node_Str" + KeyRings.MASTER_KEY_ID);
    if (cursor == null || !cursor.moveToFirst()) {
      log.add(LogType.MSG_EXPORT_ERROR_DB,1);
      return new ExportResult(ExportResult.RESULT_ERROR,log,okPublic,okSecret);
    }
    int numKeys=cursor.getCount();
    updateProgress(mContext.getResources().getQuantityString(R.plurals.progress_exporting_key,numKeys),0,numKeys);
    while (!cursor.isAfterLast()) {
      ArmoredOutputStream arOutStream=new ArmoredOutputStream(outStream);
      String version=PgpHelper.getVersionForHeader(mContext);
      if (version != null) {
        arOutStream.setHeader("String_Node_Str",version);
      }
      long keyId=cursor.getLong(0);
      log.add(LogType.MSG_EXPORT_PUBLIC,1,KeyFormattingUtils.beautifyKeyId(keyId));
{
        byte[] data=cursor.getBlob(1);
        arOutStream.write(data);
        arOutStream.close();
        okPublic+=1;
      }
      if (exportSecret && cursor.getInt(3) > 0) {
        log.add(LogType.MSG_EXPORT_SECRET,2,KeyFormattingUtils.beautifyKeyId(keyId));
        byte[] data=cursor.getBlob(2);
        arOutStream.write(data);
        okSecret+=1;
      }
      updateProgress(progress++,numKeys);
      cursor.moveToNext();
    }
    updateProgress(R.string.progress_done,numKeys,numKeys);
  }
 catch (  IOException e) {
    log.add(LogType.MSG_EXPORT_ERROR_IO,1);
    return new ExportResult(ExportResult.RESULT_ERROR,log,okPublic,okSecret);
  }
  log.add(LogType.MSG_EXPORT_SUCCESS,1);
  return new ExportResult(ExportResult.RESULT_OK,log,okPublic,okSecret);
}

Fixed version (ground truth):
private ExportResult exportKeyRings(OperationLog log,long[] masterKeyIds,boolean exportSecret,OutputStream outStream){
  if (!BufferedOutputStream.class.isInstance(outStream)) {
    outStream=new BufferedOutputStream(outStream);
  }
  int okSecret=0, okPublic=0, progress=0;
  try {
    String selection=null, ids[]=null;
    if (masterKeyIds != null) {
      ids=new String[masterKeyIds.length];
      StringBuilder placeholders=new StringBuilder("String_Node_Str");
      for (int i=0; i < masterKeyIds.length; i++) {
        ids[i]=Long.toString(masterKeyIds[i]);
        if (i != 0) {
          placeholders.append("String_Node_Str");
        }
      }
      selection=Tables.KEY_RINGS_PUBLIC + "String_Node_Str" + KeyRings.MASTER_KEY_ID+ "String_Node_Str"+ placeholders+ "String_Node_Str";
    }
    Cursor cursor=mProviderHelper.getContentResolver().query(KeyRings.buildUnifiedKeyRingsUri(),new String[]{KeyRings.MASTER_KEY_ID,KeyRings.PUBKEY_DATA,KeyRings.PRIVKEY_DATA,KeyRings.HAS_ANY_SECRET},selection,ids,Tables.KEYS + "String_Node_Str" + KeyRings.MASTER_KEY_ID);
    if (cursor == null || !cursor.moveToFirst()) {
      log.add(LogType.MSG_EXPORT_ERROR_DB,1);
      return new ExportResult(ExportResult.RESULT_ERROR,log,okPublic,okSecret);
    }
    int numKeys=cursor.getCount();
    updateProgress(mContext.getResources().getQuantityString(R.plurals.progress_exporting_key,numKeys),0,numKeys);
    while (!cursor.isAfterLast()) {
      ArmoredOutputStream arOutStream=new ArmoredOutputStream(outStream);
      String version=PgpHelper.getVersionForHeader(mContext);
      if (version != null) {
        arOutStream.setHeader("String_Node_Str",version);
      }
      long keyId=cursor.getLong(0);
      log.add(LogType.MSG_EXPORT_PUBLIC,1,KeyFormattingUtils.beautifyKeyId(keyId));
{
        byte[] data=cursor.getBlob(1);
        arOutStream.write(data);
        arOutStream.close();
        okPublic+=1;
      }
      if (exportSecret && cursor.getInt(3) > 0) {
        log.add(LogType.MSG_EXPORT_SECRET,2,KeyFormattingUtils.beautifyKeyId(keyId));
        byte[] data=cursor.getBlob(2);
        arOutStream.write(data);
        okSecret+=1;
      }
      updateProgress(progress++,numKeys);
      cursor.moveToNext();
    }
    updateProgress(R.string.progress_done,numKeys,numKeys);
  }
 catch (  IOException e) {
    log.add(LogType.MSG_EXPORT_ERROR_IO,1);
    return new ExportResult(ExportResult.RESULT_ERROR,log,okPublic,okSecret);
  }
  log.add(LogType.MSG_EXPORT_SUCCESS,1);
  return new ExportResult(ExportResult.RESULT_OK,log,okPublic,okSecret);
}