Program: 4260
Project: 4260
Buggy source:
/** 
 * Converts the passed responses to QueryReplies. Each QueryReply can accomodate atmost 255 responses. Not all the responses may get included in QueryReplies in case the query request came from a far away host. <p> NOTE: This method doesnt have any side effect,  and does not modify the state of this object
 * @param responses The responses to be converted
 * @param queryRequest The query request corresponding to which we aregenerating query replies.
 * @param REPLY_LIMIT the maximum number of responses to have in each reply.
 * @return Iterator (on QueryReply) over the Query Replies
 */
private Iterator responsesToQueryReplies(Response[] responses,QueryRequest queryRequest,final int REPLY_LIMIT){
  List queryReplies=new LinkedList();
  byte[] guid=queryRequest.getGUID();
  byte ttl=(byte)(queryRequest.getHops() + 1);
  UploadManager um=RouterService.getUploadManager();
  long speed=um.measuredUploadSpeed();
  boolean measuredSpeed=true;
  if (speed == -1) {
    speed=ConnectionSettings.CONNECTION_SPEED.getValue();
    measuredSpeed=false;
  }
  int numResponses=responses.length;
  int index=0;
  int numHops=queryRequest.getHops();
  if (REPLY_LIMIT > 1 && numHops > 2 && numResponses > HIGH_HOPS_RESPONSE_LIMIT) {
    int j=(int)(Math.random() * numResponses) % (numResponses - HIGH_HOPS_RESPONSE_LIMIT);
    Response[] newResponses=new Response[HIGH_HOPS_RESPONSE_LIMIT];
    for (int i=0; i < 10; i++, j++) {
      newResponses[i]=responses[j];
    }
    responses=newResponses;
    numResponses=responses.length;
  }
  while (numResponses > 0) {
    int arraySize;
    if (numResponses < REPLY_LIMIT) {
      arraySize=numResponses;
    }
 else     arraySize=REPLY_LIMIT;
    Response[] res;
    if ((index == 0) && (arraySize < REPLY_LIMIT)) {
      res=responses;
    }
 else {
      res=new Response[arraySize];
      for (int i=0; i < arraySize; i++) {
        res[i]=responses[index];
        index++;
      }
    }
    numResponses-=arraySize;
    boolean busy=!um.isServiceable();
    boolean uploaded=um.hadSuccesfulUpload();
    boolean mcast=queryRequest.isMulticast() && (queryRequest.getTTL() + queryRequest.getHops()) == 1;
    final boolean fwTransfer=queryRequest.canDoFirewalledTransfer() && UDPService.instance().canDoFWT() && !RouterService.acceptedIncomingConnection();
    if (mcast) {
      ttl=1;
    }
    List replies=createQueryReply(guid,ttl,speed,res,_clientGUID,busy,uploaded,measuredSpeed,mcast,fwTransfer);
    queryReplies.addAll(replies);
  }
  return queryReplies.iterator();
}

Fixed version (ground truth):
/** 
 * Converts the passed responses to QueryReplies. Each QueryReply can accomodate atmost 255 responses. Not all the responses may get included in QueryReplies in case the query request came from a far away host. <p> NOTE: This method doesnt have any side effect,  and does not modify the state of this object
 * @param responses The responses to be converted
 * @param queryRequest The query request corresponding to which we aregenerating query replies.
 * @param REPLY_LIMIT the maximum number of responses to have in each reply.
 * @return Iterable of QueryReply
 */
private Iterable<QueryReply> responsesToQueryReplies(Response[] responses,QueryRequest queryRequest,final int REPLY_LIMIT){
  List<QueryReply> queryReplies=new LinkedList<QueryReply>();
  byte[] guid=queryRequest.getGUID();
  byte ttl=(byte)(queryRequest.getHops() + 1);
  UploadManager um=RouterService.getUploadManager();
  long speed=um.measuredUploadSpeed();
  boolean measuredSpeed=true;
  if (speed == -1) {
    speed=ConnectionSettings.CONNECTION_SPEED.getValue();
    measuredSpeed=false;
  }
  int numResponses=responses.length;
  int index=0;
  int numHops=queryRequest.getHops();
  if (REPLY_LIMIT > 1 && numHops > 2 && numResponses > HIGH_HOPS_RESPONSE_LIMIT) {
    int j=(int)(Math.random() * numResponses) % (numResponses - HIGH_HOPS_RESPONSE_LIMIT);
    Response[] newResponses=new Response[HIGH_HOPS_RESPONSE_LIMIT];
    for (int i=0; i < 10; i++, j++) {
      newResponses[i]=responses[j];
    }
    responses=newResponses;
    numResponses=responses.length;
  }
  while (numResponses > 0) {
    int arraySize;
    if (numResponses < REPLY_LIMIT) {
      arraySize=numResponses;
    }
 else     arraySize=REPLY_LIMIT;
    Response[] res;
    if ((index == 0) && (arraySize < REPLY_LIMIT)) {
      res=responses;
    }
 else {
      res=new Response[arraySize];
      for (int i=0; i < arraySize; i++) {
        res[i]=responses[index];
        index++;
      }
    }
    numResponses-=arraySize;
    boolean busy=!um.isServiceable();
    boolean uploaded=um.hadSuccesfulUpload();
    boolean mcast=queryRequest.isMulticast() && (queryRequest.getTTL() + queryRequest.getHops()) == 1;
    final boolean fwTransfer=queryRequest.canDoFirewalledTransfer() && UDPService.instance().canDoFWT() && !RouterService.acceptedIncomingConnection();
    if (mcast) {
      ttl=1;
    }
    List<QueryReply> replies=createQueryReply(guid,ttl,speed,res,_clientGUID,busy,uploaded,measuredSpeed,mcast,fwTransfer);
    queryReplies.addAll(replies);
  }
  return queryReplies;
}