Program: 90747
Project: 90747
Buggy source:
/** 
 * Adds ServletPortlet servlet or servlet-mapping records to web.xml  for new portlet deployment.
 * @param context Web context or directory under webapps
 * @param contents Current contents of the web.xml file
 * @param elements Elements in web.xml to search for. If found, newelements will be inserted to the contents String. NOTE: First element (elements[0] signals the kind of record to add (servlet or servlet-mapping).
 * @param portletData List of portlet names in order that they appear in portlet.xml TODO: Add security-role-ref param for servlet record
 */
String addRecordsToWebXml(String context,String contents,String[] elements,List portletData){
  StringBuffer results=new StringBuffer(contents);
  int index=-1;
  int len=portletData.size();
  int lenElements=elements.length;
  String before=null;
  String newRecord=null;
  String remainder=null;
  for (int i=0; i < len; i++) {
    for (int j=0; j < lenElements; j++) {
      if ((index=results.lastIndexOf("String_Node_Str" + elements[j] + "String_Node_Str")) != -1) {
        int elementLen=elements[j].length() + 3;
        if (i == 0 & elements[j].equals("String_Node_Str")) {
          index=results.indexOf("String_Node_Str" + elements[j] + "String_Node_Str");
          elementLen=elements[j].length() + 2;
        }
        before=results.substring(0,index + elementLen);
        remainder=results.substring(index + elementLen);
        if (elements[0].equals("String_Node_Str")) {
          newRecord=getServletRecord(context,(PortletDD)portletData.get(i));
        }
 else         if (elements[0].equals("String_Node_Str")) {
          newRecord=getServletMappingRecord((PortletDD)portletData.get(i));
        }
        results=new StringBuffer();
        results.append(before);
        results.append(PlutoAdminConstants.LS);
        results.append(newRecord);
        results.append(remainder);
        break;
      }
    }
  }
  return results.toString();
}

Fixed version (ground truth):
/** 
 * Adds ServletPortlet servlet or servlet-mapping records to web.xml  for new portlet deployment.
 * @param context Web context or directory under webapps
 * @param contents Current contents of the web.xml file
 * @param elements Elements in web.xml to search for. If found, newelements will be inserted to the contents String. NOTE: First element (elements[0] signals the kind of record to add (servlet or servlet-mapping).
 * @param portletData A List of PortletDD items containing the data of portlets to be deployed.
 */
String addRecordsToWebXml(String context,String contents,String[] elements,List portletData){
  String METHOD_NAME="String_Node_Str";
  StringBuffer results=new StringBuffer(contents);
  int index=-1;
  int len=portletData.size();
  int lenElements=elements.length;
  String before=null;
  String newRecord=null;
  String remainder=null;
  String rest=null;
  for (int i=0; i < len; i++) {
    for (int j=0; j < lenElements; j++) {
      if ((index=results.lastIndexOf("String_Node_Str" + elements[j])) != -1) {
        rest=results.substring(index);
        int elementLen=rest.indexOf('>') + 1;
        if (i == 0 & elements[j].equals("String_Node_Str")) {
          index=results.indexOf("String_Node_Str");
          rest=results.substring(index);
          elementLen=rest.indexOf('>') + 1;
        }
        logDebug(METHOD_NAME,"String_Node_Str" + elements[j] + "String_Node_Str"+ elementLen);
        before=results.substring(0,index + elementLen);
        remainder=results.substring(index + elementLen);
        if (elements[0].equals("String_Node_Str")) {
          newRecord=getServletRecord(context,(PortletDD)portletData.get(i));
        }
 else         if (elements[0].equals("String_Node_Str")) {
          newRecord=getServletMappingRecord((PortletDD)portletData.get(i));
        }
        results=new StringBuffer();
        results.append(before);
        results.append(PlutoAdminConstants.LS);
        results.append(newRecord);
        results.append(remainder);
        break;
      }
    }
  }
  return results.toString();
}