Program: 4826
Project: 4826
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;
  }
  RemoteFileDesc rfd=null;
synchronized (this) {
    rfd=(RemoteFileDesc)files.get(0);
  }
  int fileSize=rfd.getSize();
  String filename=rfd.getFileName();
  File incompleteFile=incompleteFileManager.getFile(rfd);
  File sharedDir;
  File completeFile;
  try {
    sharedDir=SettingsManager.instance().getSaveDirectory();
    completeFile=new File(sharedDir,filename);
    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=new AlternateLocationCollection();
  RemoteFileDesc tempRFD;
  String rfdStr;
  URL rfdURL;
synchronized (this) {
    for (Iterator iter=files.iterator(); iter.hasNext(); ) {
      tempRFD=(RemoteFileDesc)iter.next();
      if (tempRFD.getSHA1Urn() == null)       continue;
      try {
        totalAlternateLocations.addAlternateLocation(AlternateLocation.createAlternateLocation(tempRFD));
      }
 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 {
    fileHash=URN.createSHA1Urn(incompleteFile);
  }
 catch (  IOException ignored) {
  }
  if (bucketHash != null) {
synchronized (corruptStateLock) {
      if (!bucketHash.equals(fileHash)) {
        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;
    }
  }
  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.addAlternateLocationCollection(totalAlternateLocations);
      callback.handleSharedFileUpdate(completeFile);
      HeadRequester requester=new HeadRequester(files,fileHash,fileDesc,totalAlternateLocations);
      Thread headThread=new Thread(requester);
      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;
  }
  RemoteFileDesc rfd=null;
synchronized (this) {
    rfd=(RemoteFileDesc)files.get(0);
  }
  int fileSize=rfd.getSize();
  String filename=rfd.getFileName();
  incompleteFile=incompleteFileManager.getFile(rfd);
  File sharedDir;
  try {
    sharedDir=SettingsManager.instance().getSaveDirectory();
    completeFile=new File(sharedDir,filename);
    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=new AlternateLocationCollection();
  RemoteFileDesc tempRFD;
  String rfdStr;
  URL rfdURL;
synchronized (this) {
    for (Iterator iter=files.iterator(); iter.hasNext(); ) {
      tempRFD=(RemoteFileDesc)iter.next();
      if (tempRFD.getSHA1Urn() == null)       continue;
      try {
        totalAlternateLocations.addAlternateLocation(AlternateLocation.createAlternateLocation(tempRFD));
      }
 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 {
    fileHash=URN.createSHA1Urn(incompleteFile);
  }
 catch (  IOException ignored) {
  }
  if (bucketHash != null) {
synchronized (corruptStateLock) {
      if (!bucketHash.equals(fileHash)) {
        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;
    }
  }
  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.addAlternateLocationCollection(totalAlternateLocations);
      callback.handleSharedFileUpdate(completeFile);
      HeadRequester requester=new HeadRequester(files,fileHash,fileDesc,totalAlternateLocations);
      Thread headThread=new Thread(requester);
      headThread.setDaemon(true);
      headThread.start();
    }
  }
  return COMPLETE;
}