Program: 4664
Project: 4664
Buggy source:
/** 
 * Tries one round of downloading of the given files.  Downloads from all locations until all locations fail or some locations succeed.  Moves incomplete file to the library on success.
 * @return COMPLETE if a file was successfully downloaded.  This canhappen even if the file is corrupt, if the user explicitly approved. CORRUPT_FILE a bytes mismatched when checking overlapping regions of resume or swarm, and the user decided they' did not want the download fragment, which is now quarantined. COULDNT_MOVE_TO_LIBRARY the download completed but the temporary file couldn't be moved to the library OR the download couldn't be written to the incomplete file WAITING_FOR_RETRY if no file was downloaded, but it makes sense  to try again later because some hosts reported busy. The caller should usually wait before retrying. GAVE_UP the download attempt failed, and there are  no more locations to try.
 * @exception InterruptedException if the user stop()'ed this download. (Calls to resume() do not result in InterruptedException.)
 */
private int tryAllDownloads2() throws InterruptedException {
synchronized (this) {
    if (files.size() == 0)     return GAVE_UP;
  }
  incompleteFile=incompleteFileManager.getFile((RemoteFileDesc)files.get(0));
  File sharedDir;
  try {
    sharedDir=SettingsManager.instance().getSaveDirectory();
    completeFile=new File(sharedDir,getFileName());
    String sharedPath=sharedDir.getCanonicalPath();
    String completeFileParentPath=new File(completeFile.getParent()).getCanonicalPath();
    if (!sharedPath.equals(completeFileParentPath))     throw new InvalidPathException();
  }
 catch (  IOException e) {
    return COULDNT_MOVE_TO_LIBRARY;
  }
  totalAlternateLocations=null;
  RemoteFileDesc tempRFD;
  String rfdStr;
  URL rfdURL;
synchronized (this) {
    for (Iterator iter=files.iterator(); iter.hasNext(); ) {
      tempRFD=(RemoteFileDesc)iter.next();
      URN sha1=tempRFD.getSHA1Urn();
      if (sha1 == null)       continue;
      if (totalAlternateLocations == null) {
        totalAlternateLocations=AlternateLocationCollection.createCollection(sha1);
      }
      if (!sha1.equals(totalAlternateLocations.getSHA1Urn())) {
        continue;
      }
      try {
        AlternateLocation location=AlternateLocation.createAlternateLocation(tempRFD);
        totalAlternateLocations.addAlternateLocation(location);
      }
 catch (      IOException e) {
      }
    }
  }
  int status=-1;
  try {
    status=tryAllDownloads3();
  }
 catch (  InterruptedException e) {
  }
  commonOutFile.close();
  if (corruptState != NOT_CORRUPT_STATE) {
synchronized (corruptStateLock) {
      try {
        while (corruptState == CORRUPT_WAITING_STATE) {
          corruptStateLock.wait();
        }
      }
 catch (      InterruptedException ignored) {
      }
    }
    if (corruptState == CORRUPT_STOP_STATE) {
      cleanupCorrupt(incompleteFile,completeFile.getName());
      return CORRUPT_FILE;
    }
 else     if (corruptState == CORRUPT_CONTINUE_STATE) {
      ;
    }
  }
  if (status == -1)   throw new InterruptedException();
  if (status != COMPLETE)   return status;
  URN bucketHash=buckets.getURNForBucket(bucketNumber);
  URN fileHash=null;
  try {
    setState(HASHING);
    fileHash=URN.createSHA1Urn(incompleteFile);
  }
 catch (  IOException ignored) {
  }
  if (bucketHash != null) {
synchronized (corruptStateLock) {
      if (!bucketHash.equals(fileHash)) {
        setState(CORRUPT_FILE);
        promptAboutCorruptDownload();
        debug("String_Node_Str" + fileHash + "String_Node_Str"+ bucketHash);
      }
      try {
        while (corruptState == CORRUPT_WAITING_STATE)         corruptStateLock.wait();
      }
 catch (      InterruptedException ignored2) {
      }
    }
    if (corruptState == CORRUPT_STOP_STATE) {
      cleanupCorrupt(incompleteFile,completeFile.getName());
      return CORRUPT_FILE;
    }
  }
  setState(SAVING);
  completeFile.delete();
  if (!incompleteFile.renameTo(completeFile))   if (!CommonUtils.copy(incompleteFile,completeFile))   return COULDNT_MOVE_TO_LIBRARY;
  incompleteFileManager.removeEntry(incompleteFile);
  if (fileExists(completeFile))   fileManager.removeFileIfShared(completeFile);
  boolean fileAdded=fileManager.addFileIfShared(completeFile,getXMLDocuments());
  if (totalAlternateLocations != null && fileAdded) {
    FileDesc fileDesc=fileManager.getFileDescMatching(completeFile);
    if (fileDesc != null && fileDesc.getSHA1Urn().equals(totalAlternateLocations.getSHA1Urn())) {
      fileDesc.addAlternateLocationCollection(totalAlternateLocations);
      callback.handleSharedFileUpdate(completeFile);
      HeadRequester requester=new HeadRequester(files,fileHash,fileDesc,fileDesc.getAlternateLocationCollection());
      Thread headThread=new Thread(requester,"String_Node_Str");
      headThread.setDaemon(true);
      headThread.start();
    }
  }
  return COMPLETE;
}

Fixed version (ground truth):
/** 
 * Tries one round of downloading of the given files.  Downloads from all locations until all locations fail or some locations succeed.  Moves incomplete file to the library on success.
 * @return COMPLETE if a file was successfully downloaded.  This canhappen even if the file is corrupt, if the user explicitly approved. CORRUPT_FILE a bytes mismatched when checking overlapping regions of resume or swarm, and the user decided they' did not want the download fragment, which is now quarantined. COULDNT_MOVE_TO_LIBRARY the download completed but the temporary file couldn't be moved to the library OR the download couldn't be written to the incomplete file WAITING_FOR_RETRY if no file was downloaded, but it makes sense  to try again later because some hosts reported busy. The caller should usually wait before retrying. GAVE_UP the download attempt failed, and there are  no more locations to try.
 * @exception InterruptedException if the user stop()'ed this download. (Calls to resume() do not result in InterruptedException.)
 */
private int tryAllDownloads2() throws InterruptedException {
synchronized (this) {
    if (files.size() == 0)     return GAVE_UP;
  }
  incompleteFile=incompleteFileManager.getFile((RemoteFileDesc)files.get(0));
  File sharedDir;
  try {
    sharedDir=SettingsManager.instance().getSaveDirectory();
    completeFile=new File(sharedDir,getFileName());
    String sharedPath=sharedDir.getCanonicalPath();
    String completeFileParentPath=new File(completeFile.getParent()).getCanonicalPath();
    if (!sharedPath.equals(completeFileParentPath))     throw new InvalidPathException();
  }
 catch (  IOException e) {
    return COULDNT_MOVE_TO_LIBRARY;
  }
  totalAlternateLocations=null;
  RemoteFileDesc tempRFD;
  String rfdStr;
  URL rfdURL;
synchronized (this) {
    for (Iterator iter=files.iterator(); iter.hasNext(); ) {
      tempRFD=(RemoteFileDesc)iter.next();
      URN sha1=tempRFD.getSHA1Urn();
      if (sha1 == null)       continue;
      if (totalAlternateLocations == null) {
        totalAlternateLocations=AlternateLocationCollection.createCollection(sha1);
      }
      if (!sha1.equals(totalAlternateLocations.getSHA1Urn())) {
        continue;
      }
      try {
        AlternateLocation location=AlternateLocation.createAlternateLocation(tempRFD);
        totalAlternateLocations.addAlternateLocation(location);
      }
 catch (      IOException e) {
      }
    }
  }
  int status=-1;
  try {
    status=tryAllDownloads3();
  }
 catch (  InterruptedException e) {
  }
  commonOutFile.close();
  if (corruptState != NOT_CORRUPT_STATE) {
synchronized (corruptStateLock) {
      try {
        while (corruptState == CORRUPT_WAITING_STATE) {
          corruptStateLock.wait();
        }
      }
 catch (      InterruptedException ignored) {
      }
    }
    if (corruptState == CORRUPT_STOP_STATE) {
      cleanupCorrupt(incompleteFile,completeFile.getName());
      return CORRUPT_FILE;
    }
 else     if (corruptState == CORRUPT_CONTINUE_STATE) {
      ;
    }
  }
  if (status == -1)   throw new InterruptedException();
  if (status != COMPLETE)   return status;
  URN bucketHash=buckets.getURNForBucket(bucketNumber);
  URN fileHash=null;
  try {
    setState(HASHING);
    fileHash=URN.createSHA1Urn(incompleteFile);
  }
 catch (  IOException ignored) {
  }
  if (bucketHash != null) {
synchronized (corruptStateLock) {
      if (!bucketHash.equals(fileHash)) {
        setState(CORRUPT_FILE);
        promptAboutCorruptDownload();
        debug("String_Node_Str" + fileHash + "String_Node_Str"+ bucketHash);
      }
      try {
        while (corruptState == CORRUPT_WAITING_STATE)         corruptStateLock.wait();
      }
 catch (      InterruptedException ignored2) {
      }
    }
    if (corruptState == CORRUPT_STOP_STATE) {
      cleanupCorrupt(incompleteFile,completeFile.getName());
      return CORRUPT_FILE;
    }
  }
  setState(SAVING);
  completeFile.delete();
  if (!incompleteFile.renameTo(completeFile))   if (!CommonUtils.copy(incompleteFile,completeFile))   return COULDNT_MOVE_TO_LIBRARY;
  incompleteFileManager.removeEntry(incompleteFile);
  if (fileExists(completeFile))   fileManager.removeFileIfShared(completeFile);
  boolean fileAdded=fileManager.addFileIfShared(completeFile,getXMLDocuments());
  if (totalAlternateLocations != null && fileAdded) {
    FileDesc fileDesc=fileManager.getFileDescMatching(completeFile);
    if (fileDesc != null && fileDesc.getSHA1Urn().equals(totalAlternateLocations.getSHA1Urn())) {
      fileDesc.addAlternateLocationCollection(totalAlternateLocations);
      callback.handleSharedFileUpdate(completeFile);
      HashSet set=null;
synchronized (this) {
        set=new HashSet(files);
      }
      HeadRequester requester=new HeadRequester(set,fileHash,fileDesc,fileDesc.getAlternateLocationCollection());
      Thread headThread=new Thread(requester,"String_Node_Str");
      headThread.setDaemon(true);
      headThread.start();
    }
  }
  return COMPLETE;
}