Program: 8277
Project: 8277
Buggy source:
/** 
 * Since the introduction of multithreaded import, we expect calling functions to handle the key sync i,eContactSyncAdapterService.requestSync()
 * @param entries      keys to import
 * @param num          number of keys to import
 * @param keyServerUri contains uri of keyserver to import from, if it is an import from cloud
 * @param progressable Allows multi-threaded import to supply a progressable that ignores theprogress of a single key being imported
 */
@NonNull public ImportKeyResult serialKeyRingImport(Iterator<ParcelableKeyRing> entries,int num,String keyServerUri,Progressable progressable,Proxy proxy){
  if (progressable != null) {
    progressable.setProgress(R.string.progress_importing,0,100);
  }
  OperationLog log=new OperationLog();
  log.add(LogType.MSG_IMPORT,0,num);
  if (entries == null || !entries.hasNext()) {
    return new ImportKeyResult(ImportKeyResult.RESULT_FAIL_NOTHING,log);
  }
  int newKeys=0, updatedKeys=0, badKeys=0, secret=0;
  ArrayList<Long> importedMasterKeyIds=new ArrayList<>();
  boolean cancelled=false;
  int position=0;
  double progSteps=100.0 / num;
  KeybaseKeyserver keybaseServer=null;
  HkpKeyserver keyServer=null;
  while (entries.hasNext()) {
    ParcelableKeyRing entry=entries.next();
    if (checkCancelled()) {
      cancelled=true;
      break;
    }
    try {
      UncachedKeyRing key=null;
      if (entry.mBytes != null) {
        key=UncachedKeyRing.decodeFromData(entry.mBytes);
      }
 else {
        if (keyServerUri != null && (entry.mKeyIdHex != null || entry.mExpectedFingerprint != null)) {
          if (keyServer == null) {
            log.add(LogType.MSG_IMPORT_KEYSERVER,1,keyServerUri);
            keyServer=new HkpKeyserver(keyServerUri);
          }
          try {
            byte[] data;
            if (entry.mExpectedFingerprint != null) {
              log.add(LogType.MSG_IMPORT_FETCH_KEYSERVER,2,"String_Node_Str" + entry.mExpectedFingerprint.substring(24));
              data=keyServer.get("String_Node_Str" + entry.mExpectedFingerprint,proxy).getBytes();
            }
 else {
              log.add(LogType.MSG_IMPORT_FETCH_KEYSERVER,2,entry.mKeyIdHex);
              data=keyServer.get(entry.mKeyIdHex,proxy).getBytes();
            }
            key=UncachedKeyRing.decodeFromData(data);
            if (key != null) {
              log.add(LogType.MSG_IMPORT_FETCH_KEYSERVER_OK,3);
            }
 else {
              log.add(LogType.MSG_IMPORT_FETCH_ERROR_DECODE,3);
            }
          }
 catch (          Keyserver.QueryFailedException e) {
            Log.e(Constants.TAG,"String_Node_Str",e);
            log.add(LogType.MSG_IMPORT_FETCH_KEYSERVER_ERROR,3,e.getMessage());
          }
        }
        if (entry.mKeybaseName != null) {
          if (keybaseServer == null) {
            keybaseServer=new KeybaseKeyserver();
          }
          try {
            log.add(LogType.MSG_IMPORT_FETCH_KEYBASE,2,entry.mKeybaseName);
            byte[] data=keybaseServer.get(entry.mKeybaseName,proxy).getBytes();
            UncachedKeyRing keybaseKey=UncachedKeyRing.decodeFromData(data);
            if (key != null && keybaseKey != null) {
              log.add(LogType.MSG_IMPORT_MERGE,3);
              keybaseKey=key.merge(keybaseKey,log,4);
              if (keybaseKey != null) {
                key=keybaseKey;
              }
 else {
                log.add(LogType.MSG_IMPORT_MERGE_ERROR,4);
              }
            }
 else             if (keybaseKey != null) {
              key=keybaseKey;
            }
          }
 catch (          Keyserver.QueryFailedException e) {
            Log.e(Constants.TAG,"String_Node_Str",e);
            log.add(LogType.MSG_IMPORT_FETCH_KEYSERVER_ERROR,3,e.getMessage());
          }
        }
      }
      if (key == null) {
        log.add(LogType.MSG_IMPORT_FETCH_ERROR,2);
        badKeys+=1;
        continue;
      }
      if (entry.mExpectedFingerprint != null) {
        if (!key.containsSubkey(entry.mExpectedFingerprint)) {
          log.add(LogType.MSG_IMPORT_FINGERPRINT_ERROR,2);
          badKeys+=1;
          continue;
        }
 else {
          log.add(LogType.MSG_IMPORT_FINGERPRINT_OK,2);
        }
      }
      if (checkCancelled()) {
        cancelled=true;
        break;
      }
      SaveKeyringResult result;
      mProviderHelper.clearLog();
      if (key.isSecret()) {
        result=mProviderHelper.saveSecretKeyRing(key,new ProgressScaler(progressable,(int)(position * progSteps),(int)((position + 1) * progSteps),100));
      }
 else {
        result=mProviderHelper.savePublicKeyRing(key,new ProgressScaler(progressable,(int)(position * progSteps),(int)((position + 1) * progSteps),100));
      }
      if (!result.success()) {
        badKeys+=1;
      }
 else {
        if (result.updated()) {
          updatedKeys+=1;
          importedMasterKeyIds.add(key.getMasterKeyId());
        }
 else {
          newKeys+=1;
          if (key.isSecret()) {
            secret+=1;
          }
          importedMasterKeyIds.add(key.getMasterKeyId());
        }
        if (entry.mBytes == null) {
          mProviderHelper.renewKeyLastUpdatedTime(key.getMasterKeyId(),GregorianCalendar.getInstance().getTimeInMillis(),TimeUnit.MILLISECONDS);
        }
      }
      log.add(result,2);
    }
 catch (    IOException|PgpGeneralException e) {
      Log.e(Constants.TAG,"String_Node_Str",e);
      ++badKeys;
    }
    position++;
  }
  if (secret > 0) {
    setPreventCancel();
    ConsolidateResult result=mProviderHelper.consolidateDatabaseStep1(progressable);
    log.add(result,1);
  }
  long[] importedMasterKeyIdsArray=new long[importedMasterKeyIds.size()];
  for (int i=0; i < importedMasterKeyIds.size(); ++i) {
    importedMasterKeyIdsArray[i]=importedMasterKeyIds.get(i);
  }
  int resultType=0;
  if (cancelled) {
    log.add(LogType.MSG_OPERATION_CANCELLED,1);
    resultType|=ImportKeyResult.RESULT_CANCELLED;
  }
  if (badKeys == 0 && newKeys == 0 && updatedKeys == 0) {
    resultType=ImportKeyResult.RESULT_FAIL_NOTHING;
  }
 else {
    if (newKeys > 0) {
      resultType|=ImportKeyResult.RESULT_OK_NEWKEYS;
    }
    if (updatedKeys > 0) {
      resultType|=ImportKeyResult.RESULT_OK_UPDATED;
    }
    if (badKeys > 0) {
      resultType|=ImportKeyResult.RESULT_WITH_ERRORS;
      if (newKeys == 0 && updatedKeys == 0) {
        resultType|=ImportKeyResult.RESULT_ERROR;
      }
    }
    if (log.containsWarnings()) {
      resultType|=ImportKeyResult.RESULT_WARNINGS;
    }
  }
  if ((newKeys > 0 || updatedKeys > 0) && badKeys > 0) {
    log.add(LogType.MSG_IMPORT_PARTIAL,1);
  }
 else   if (newKeys > 0 || updatedKeys > 0) {
    log.add(LogType.MSG_IMPORT_SUCCESS,1);
  }
 else {
    log.add(LogType.MSG_IMPORT_ERROR,1);
  }
  return new ImportKeyResult(resultType,log,newKeys,updatedKeys,badKeys,secret,importedMasterKeyIdsArray);
}

Fixed version (ground truth):
/** 
 * Since the introduction of multithreaded import, we expect calling functions to handle the key sync i,eContactSyncAdapterService.requestSync()
 * @param entries      keys to import
 * @param num          number of keys to import
 * @param keyServerUri contains uri of keyserver to import from, if it is an import from cloud
 * @param progressable Allows multi-threaded import to supply a progressable that ignores theprogress of a single key being imported
 */
@NonNull public ImportKeyResult serialKeyRingImport(Iterator<ParcelableKeyRing> entries,int num,String keyServerUri,Progressable progressable,Proxy proxy){
  if (progressable != null) {
    progressable.setProgress(R.string.progress_importing,0,100);
  }
  OperationLog log=new OperationLog();
  log.add(LogType.MSG_IMPORT,0,num);
  if (entries == null || !entries.hasNext()) {
    return new ImportKeyResult(ImportKeyResult.RESULT_FAIL_NOTHING,log);
  }
  int newKeys=0, updatedKeys=0, badKeys=0, secret=0;
  ArrayList<Long> importedMasterKeyIds=new ArrayList<>();
  boolean cancelled=false;
  int position=0;
  double progSteps=100.0 / num;
  KeybaseKeyserver keybaseServer=null;
  HkpKeyserver keyServer=null;
  while (entries.hasNext()) {
    ParcelableKeyRing entry=entries.next();
    if (checkCancelled()) {
      cancelled=true;
      break;
    }
    try {
      UncachedKeyRing key=null;
      if (entry.mBytes != null) {
        key=UncachedKeyRing.decodeFromData(entry.mBytes);
      }
 else {
        if (keyServerUri != null && (entry.mKeyIdHex != null || entry.mExpectedFingerprint != null)) {
          if (keyServer == null) {
            log.add(LogType.MSG_IMPORT_KEYSERVER,1,keyServerUri);
            keyServer=new HkpKeyserver(keyServerUri);
          }
          try {
            byte[] data;
            if (entry.mExpectedFingerprint != null) {
              log.add(LogType.MSG_IMPORT_FETCH_KEYSERVER,2,"String_Node_Str" + entry.mExpectedFingerprint.substring(24));
              data=keyServer.get("String_Node_Str" + entry.mExpectedFingerprint,proxy).getBytes();
            }
 else {
              log.add(LogType.MSG_IMPORT_FETCH_KEYSERVER,2,entry.mKeyIdHex);
              data=keyServer.get(entry.mKeyIdHex,proxy).getBytes();
            }
            key=UncachedKeyRing.decodeFromData(data);
            if (key != null) {
              log.add(LogType.MSG_IMPORT_FETCH_KEYSERVER_OK,3);
            }
 else {
              log.add(LogType.MSG_IMPORT_FETCH_ERROR_DECODE,3);
            }
          }
 catch (          Keyserver.QueryFailedException e) {
            Log.d(Constants.TAG,"String_Node_Str",e);
            log.add(LogType.MSG_IMPORT_FETCH_KEYSERVER_ERROR,3,e.getMessage());
          }
        }
        if (entry.mKeybaseName != null) {
          if (keybaseServer == null) {
            keybaseServer=new KeybaseKeyserver();
          }
          try {
            log.add(LogType.MSG_IMPORT_FETCH_KEYBASE,2,entry.mKeybaseName);
            byte[] data=keybaseServer.get(entry.mKeybaseName,proxy).getBytes();
            UncachedKeyRing keybaseKey=UncachedKeyRing.decodeFromData(data);
            if (key != null && keybaseKey != null) {
              log.add(LogType.MSG_IMPORT_MERGE,3);
              keybaseKey=key.merge(keybaseKey,log,4);
              if (keybaseKey != null) {
                key=keybaseKey;
              }
 else {
                log.add(LogType.MSG_IMPORT_MERGE_ERROR,4);
              }
            }
 else             if (keybaseKey != null) {
              key=keybaseKey;
            }
          }
 catch (          Keyserver.QueryFailedException e) {
            Log.e(Constants.TAG,"String_Node_Str",e);
            log.add(LogType.MSG_IMPORT_FETCH_KEYSERVER_ERROR,3,e.getMessage());
          }
        }
      }
      if (key == null) {
        log.add(LogType.MSG_IMPORT_FETCH_ERROR,2);
        badKeys+=1;
        continue;
      }
      if (entry.mExpectedFingerprint != null) {
        if (!key.containsSubkey(entry.mExpectedFingerprint)) {
          log.add(LogType.MSG_IMPORT_FINGERPRINT_ERROR,2);
          badKeys+=1;
          continue;
        }
 else {
          log.add(LogType.MSG_IMPORT_FINGERPRINT_OK,2);
        }
      }
      if (checkCancelled()) {
        cancelled=true;
        break;
      }
      SaveKeyringResult result;
      mProviderHelper.clearLog();
      if (key.isSecret()) {
        result=mProviderHelper.saveSecretKeyRing(key,new ProgressScaler(progressable,(int)(position * progSteps),(int)((position + 1) * progSteps),100));
      }
 else {
        result=mProviderHelper.savePublicKeyRing(key,new ProgressScaler(progressable,(int)(position * progSteps),(int)((position + 1) * progSteps),100));
      }
      if (!result.success()) {
        badKeys+=1;
      }
 else {
        if (result.updated()) {
          updatedKeys+=1;
          importedMasterKeyIds.add(key.getMasterKeyId());
        }
 else {
          newKeys+=1;
          if (key.isSecret()) {
            secret+=1;
          }
          importedMasterKeyIds.add(key.getMasterKeyId());
        }
        if (entry.mBytes == null) {
          mProviderHelper.renewKeyLastUpdatedTime(key.getMasterKeyId(),GregorianCalendar.getInstance().getTimeInMillis(),TimeUnit.MILLISECONDS);
        }
      }
      log.add(result,2);
    }
 catch (    IOException|PgpGeneralException e) {
      Log.e(Constants.TAG,"String_Node_Str",e);
      ++badKeys;
    }
    position++;
  }
  if (secret > 0) {
    setPreventCancel();
    ConsolidateResult result=mProviderHelper.consolidateDatabaseStep1(progressable);
    log.add(result,1);
  }
  long[] importedMasterKeyIdsArray=new long[importedMasterKeyIds.size()];
  for (int i=0; i < importedMasterKeyIds.size(); ++i) {
    importedMasterKeyIdsArray[i]=importedMasterKeyIds.get(i);
  }
  int resultType=0;
  if (cancelled) {
    log.add(LogType.MSG_OPERATION_CANCELLED,1);
    resultType|=ImportKeyResult.RESULT_CANCELLED;
  }
  if (badKeys == 0 && newKeys == 0 && updatedKeys == 0) {
    resultType=ImportKeyResult.RESULT_FAIL_NOTHING;
  }
 else {
    if (newKeys > 0) {
      resultType|=ImportKeyResult.RESULT_OK_NEWKEYS;
    }
    if (updatedKeys > 0) {
      resultType|=ImportKeyResult.RESULT_OK_UPDATED;
    }
    if (badKeys > 0) {
      resultType|=ImportKeyResult.RESULT_WITH_ERRORS;
      if (newKeys == 0 && updatedKeys == 0) {
        resultType|=ImportKeyResult.RESULT_ERROR;
      }
    }
    if (log.containsWarnings()) {
      resultType|=ImportKeyResult.RESULT_WARNINGS;
    }
  }
  if ((newKeys > 0 || updatedKeys > 0) && badKeys > 0) {
    log.add(LogType.MSG_IMPORT_PARTIAL,1);
  }
 else   if (newKeys > 0 || updatedKeys > 0) {
    log.add(LogType.MSG_IMPORT_SUCCESS,1);
  }
 else {
    log.add(LogType.MSG_IMPORT_ERROR,1);
  }
  return new ImportKeyResult(resultType,log,newKeys,updatedKeys,badKeys,secret,importedMasterKeyIdsArray);
}