Program: 3809
Project: 3809
Buggy source:
private void handleTorrentEvent(TorrentEvent event){
  if (event == TorrentEvent.COMPLETED) {
    lock.writeLock().lock();
    try {
      int seedingTorrents=0;
      int maxSeedingTorrents=torrentSettings.get().getMaxSeedingLimit();
      if (maxSeedingTorrents == Integer.MAX_VALUE) {
        return;
      }
      for (      Torrent torrent : torrents.values()) {
        if (torrent.isFinished()) {
          seedingTorrents++;
        }
      }
      if (seedingTorrents <= maxSeedingTorrents) {
        return;
      }
      List<Torrent> ratioSortedTorrents=new SortedList<Torrent>(torrents.values(),new Comparator<Torrent>(){
        @Override public int compare(        Torrent o1,        Torrent o2){
          int compare=Double.compare(o2.getSeedRatio(),o1.getSeedRatio());
          if (compare == 0) {
            TorrentStatus status1=o1.getStatus();
            TorrentStatus status2=o2.getStatus();
            if (status1 != null && status2 != null) {
              int time1=status1.getSeedingTime();
              int time2=status2.getSeedingTime();
              if (time1 > time2) {
                return -1;
              }
 else               if (time2 > time1) {
                return 1;
              }
 else {
                return 0;
              }
            }
          }
          return compare;
        }
      }
);
      for (int i=0; i < seedingTorrents - maxSeedingTorrents && ratioSortedTorrents.size() > 0; ) {
        Torrent torrent=ratioSortedTorrents.remove(0);
        if (torrent.isFinished()) {
          torrent.stop();
          torrent.removeListener(torrentListener);
          i++;
        }
      }
    }
  finally {
      lock.writeLock().unlock();
    }
  }
}

Fixed version (ground truth):
private void handleTorrentEvent(TorrentEvent event){
  if (event == TorrentEvent.COMPLETED) {
    lock.writeLock().lock();
    try {
      int seedingTorrents=0;
      int maxSeedingTorrents=torrentSettings.get().getMaxSeedingLimit();
      if (maxSeedingTorrents == Integer.MAX_VALUE) {
        return;
      }
      for (      Torrent torrent : torrents.values()) {
        if (torrent.isFinished()) {
          seedingTorrents++;
        }
      }
      if (seedingTorrents <= maxSeedingTorrents) {
        return;
      }
      List<Torrent> ratioSortedTorrents=new ArrayList<Torrent>(torrents.values());
      Collections.sort(ratioSortedTorrents,new Comparator<Torrent>(){
        @Override public int compare(        Torrent o1,        Torrent o2){
          int compare=Double.compare(o2.getSeedRatio(),o1.getSeedRatio());
          if (compare == 0) {
            TorrentStatus status1=o1.getStatus();
            TorrentStatus status2=o2.getStatus();
            if (status1 != null && status2 != null) {
              int time1=status1.getSeedingTime();
              int time2=status2.getSeedingTime();
              if (time1 > time2) {
                return -1;
              }
 else               if (time2 > time1) {
                return 1;
              }
 else {
                return 0;
              }
            }
          }
          return compare;
        }
      }
);
      for (int i=0; i < seedingTorrents - maxSeedingTorrents && ratioSortedTorrents.size() > 0; ) {
        Torrent torrent=ratioSortedTorrents.remove(0);
        if (torrent.isFinished()) {
          torrent.stop();
          torrent.removeListener(torrentListener);
          i++;
        }
      }
    }
  finally {
      lock.writeLock().unlock();
    }
  }
}