Program: 104149
Project: 104149
Buggy source:
/** 
 * Returns the last <tt>count</tt> messages. No progress firing as this method is supposed to be used in message windows and is supposed to be as quick as it can.
 * @param count int
 * @return QueryResultSet
 * @throws RuntimeException
 */
public synchronized QueryResultSet findLast(int count) throws RuntimeException {
  Vector filelist=filterFilesByDate(this.historyImpl.getFileList(),null,null);
  TreeSet result=new TreeSet(new HistoryRecordComparator());
  int leftCount=count;
  int currentFile=filelist.size() - 1;
  while (leftCount > 0 && currentFile >= 0) {
    Document doc=this.historyImpl.getDocumentForFile((String)filelist.get(currentFile));
    if (doc == null) {
      currentFile--;
      continue;
    }
    List nodes=new ArrayList();
    NodeList nodesList=doc.getElementsByTagName("String_Node_Str");
    for (int i=0; i < nodesList.getLength(); i++) {
      nodes.add(nodesList.item(i));
    }
    List lNodes=null;
    if (nodes.size() > leftCount) {
      lNodes=nodes.subList(nodes.size() - leftCount,nodes.size());
      leftCount=0;
    }
 else {
      lNodes=nodes;
      leftCount-=nodes.size();
    }
    Iterator i=lNodes.iterator();
    while (i.hasNext()) {
      Node node=(Node)i.next();
      NodeList propertyNodes=node.getChildNodes();
      String ts=node.getAttributes().getNamedItem("String_Node_Str").getNodeValue();
      Date timestamp=new Date(Long.parseLong(ts));
      ArrayList nameVals=new ArrayList();
      boolean isRecordOK=true;
      int len=propertyNodes.getLength();
      for (int j=0; j < len; j++) {
        Node propertyNode=propertyNodes.item(j);
        if (propertyNode.getNodeType() == Node.ELEMENT_NODE) {
          Node nodeValue=propertyNode.getFirstChild();
          if (nodeValue != null) {
            nameVals.add(propertyNode.getNodeName());
            nameVals.add(nodeValue.getNodeValue());
          }
 else           isRecordOK=false;
        }
      }
      if (!isRecordOK)       continue;
      String[] propertyNames=new String[nameVals.size() / 2];
      String[] propertyValues=new String[propertyNames.length];
      for (int j=0; j < propertyNames.length; j++) {
        propertyNames[j]=(String)nameVals.get(j * 2);
        propertyValues[j]=(String)nameVals.get(j * 2 + 1);
      }
      HistoryRecord record=new HistoryRecord(propertyNames,propertyValues,timestamp);
      result.add(record);
    }
    currentFile--;
  }
  return new OrderedQueryResultSet(result);
}

Fixed version (ground truth):
/** 
 * Returns the last <tt>count</tt> messages. No progress firing as this method is supposed to be used in message windows and is supposed to be as quick as it can.
 * @param count int
 * @return QueryResultSet
 * @throws RuntimeException
 */
public synchronized QueryResultSet findLast(int count) throws RuntimeException {
  Vector<String> filelist=filterFilesByDate(this.historyImpl.getFileList(),null,null);
  TreeSet<HistoryRecord> result=new TreeSet<HistoryRecord>(new HistoryRecordComparator());
  int leftCount=count;
  int currentFile=filelist.size() - 1;
  while (leftCount > 0 && currentFile >= 0) {
    Document doc=this.historyImpl.getDocumentForFile((String)filelist.get(currentFile));
    if (doc == null) {
      currentFile--;
      continue;
    }
    List<Node> nodes=new ArrayList<Node>();
    NodeList nodesList=doc.getElementsByTagName("String_Node_Str");
    for (int i=0; i < nodesList.getLength(); i++) {
      nodes.add(nodesList.item(i));
    }
    List<Node> lNodes=null;
    if (nodes.size() > leftCount) {
      lNodes=nodes.subList(nodes.size() - leftCount,nodes.size());
      leftCount=0;
    }
 else {
      lNodes=nodes;
      leftCount-=nodes.size();
    }
    Iterator<Node> i=lNodes.iterator();
    while (i.hasNext()) {
      Node node=(Node)i.next();
      NodeList propertyNodes=node.getChildNodes();
      String ts=node.getAttributes().getNamedItem("String_Node_Str").getNodeValue();
      Date timestamp=new Date(Long.parseLong(ts));
      ArrayList<String> nameVals=new ArrayList<String>();
      boolean isRecordOK=true;
      int len=propertyNodes.getLength();
      for (int j=0; j < len; j++) {
        Node propertyNode=propertyNodes.item(j);
        if (propertyNode.getNodeType() == Node.ELEMENT_NODE) {
          Node nodeValue=propertyNode.getFirstChild();
          if (nodeValue != null) {
            nameVals.add(propertyNode.getNodeName());
            nameVals.add(nodeValue.getNodeValue());
          }
 else           isRecordOK=false;
        }
      }
      if (!isRecordOK)       continue;
      String[] propertyNames=new String[nameVals.size() / 2];
      String[] propertyValues=new String[propertyNames.length];
      for (int j=0; j < propertyNames.length; j++) {
        propertyNames[j]=(String)nameVals.get(j * 2);
        propertyValues[j]=(String)nameVals.get(j * 2 + 1);
      }
      HistoryRecord record=new HistoryRecord(propertyNames,propertyValues,timestamp);
      result.add(record);
    }
    currentFile--;
  }
  return new OrderedQueryResultSet(result);
}