Program: 4824
Project: 4824
Buggy source:
/** 
 * Increments the count of uploads in progress for host. If uploader has exceeded its limits, places it in LIMIT_REACHED state. Always accept Browse Host requests, though.... Notifies callback of this. Corollary: wontAccept is true iff hasQueue is true Corollary: (hasQueue==false) => (wontaccept==false) Corollary: (hasQueue==false) => posInQueue == -1
 * @modifies uploader, _callback 
 * @exception if an uploader gets a request too soon, we are going to throwan IOException, which will cause the socket to be closed. 
 * @return 0 if rejected, 1 if queued, 2 if given a slot
 */
private synchronized int insertAndTest(Uploader uploader,String host,Socket socket) throws IOException {
  boolean limitReached=hostLimitReached(host);
  int size=_queuedUploads.size();
  int posInQueue=positionInQueue(socket);
  int maxQueueSize=SettingsManager.instance().getUploadQueueSize();
  boolean wontAccept=size >= maxQueueSize;
  int ret=-1;
  boolean queue=uploader.supportsQueueing();
  Assert.that(maxQueueSize > 0,"String_Node_Str");
  Assert.that(uploader.getState() != Uploader.BROWSE_HOST);
  if (posInQueue == -1) {
    debug(uploader + "String_Node_Str" + maxQueueSize+ "String_Node_Str");
    if (limitReached || wontAccept) {
      debug(uploader + "String_Node_Str" + limitReached+ "String_Node_Str"+ wontAccept);
      uploader.setState(Uploader.LIMIT_REACHED);
      return 0;
    }
    addToQueue(socket);
    posInQueue=size;
    ret=1;
    debug(uploader + "String_Node_Str");
  }
 else {
    KeyValue kv=(KeyValue)_queuedUploads.get(posInQueue);
    Long prev=(Long)kv.getValue();
    if (prev.longValue() + MIN_POLL_TIME > System.currentTimeMillis()) {
      _queuedUploads.remove(posInQueue);
      debug(uploader + "String_Node_Str");
      throw new IOException();
    }
    kv.setValue(new Long(System.currentTimeMillis()));
    debug(uploader + "String_Node_Str");
    ret=1;
  }
  debug(uploader + "String_Node_Str");
  if (!this.isBusy() && posInQueue == 0) {
    ret=2;
    debug(uploader + "String_Node_Str");
    _queuedUploads.remove(0);
  }
  if (!queue) {
    _queuedUploads.remove(posInQueue);
    uploader.setState(Uploader.LIMIT_REACHED);
    ret=0;
  }
  return ret;
}

Fixed version (ground truth):
/** 
 * Increments the count of uploads in progress for host. If uploader has exceeded its limits, places it in LIMIT_REACHED state. Always accept Browse Host requests, though.... Notifies callback of this. Corollary: wontAccept is true iff hasQueue is true Corollary: (hasQueue==false) => (wontaccept==false) Corollary: (hasQueue==false) => posInQueue == -1
 * @modifies uploader, _callback 
 * @exception if an uploader gets a request too soon, we are going to throwan IOException, which will cause the socket to be closed. 
 * @return 0 if rejected, 1 if queued, 2 if given a slot
 */
private synchronized int insertAndTest(Uploader uploader,String host,Socket socket) throws IOException {
  boolean limitReached=hostLimitReached(host);
  int size=_queuedUploads.size();
  int posInQueue=positionInQueue(socket);
  int maxQueueSize=SettingsManager.instance().getUploadQueueSize();
  boolean wontAccept=size >= maxQueueSize;
  int ret=-1;
  boolean queue=uploader.supportsQueueing();
  Assert.that(maxQueueSize > 0,"String_Node_Str");
  Assert.that(uploader.getState() != Uploader.BROWSE_HOST);
  if (posInQueue == -1) {
    debug(uploader + "String_Node_Str" + maxQueueSize+ "String_Node_Str");
    if (limitReached || wontAccept) {
      debug(uploader + "String_Node_Str" + limitReached+ "String_Node_Str"+ wontAccept);
      uploader.setState(Uploader.LIMIT_REACHED);
      return 0;
    }
    addToQueue(socket);
    posInQueue=size;
    ret=1;
    debug(uploader + "String_Node_Str");
  }
 else {
    KeyValue kv=(KeyValue)_queuedUploads.get(posInQueue);
    Long prev=(Long)kv.getValue();
    if (prev.longValue() + MIN_POLL_TIME > System.currentTimeMillis()) {
      _queuedUploads.remove(posInQueue);
      debug(uploader + "String_Node_Str");
      throw new IOException();
    }
    kv.setValue(new Long(System.currentTimeMillis()));
    debug(uploader + "String_Node_Str");
    ret=1;
  }
  debug(uploader + "String_Node_Str");
  if (!this.isBusy() && posInQueue == 0) {
    ret=2;
    debug(uploader + "String_Node_Str");
    _queuedUploads.remove(0);
  }
 else {
    if (!queue) {
      _queuedUploads.remove(posInQueue);
      uploader.setState(Uploader.LIMIT_REACHED);
      ret=0;
    }
  }
  return ret;
}