Program: 46648
Project: 46648
Buggy source:
/** 
 * This sets up the parameter RETURN_ATTRIBUTE_NAME with the return address  harvested from the reqest. <ul><le>If a "return" parameter is present we check in the metadata for spoofing  and then set up from there </le> <le>If "returnID" is specified we get this from the metadata</le> <le>If nothing is provided we get the default from the metadata (if provided)</le> <le>Otherwise we whine</le> </ul>     
 * @param spName - the name of the Service provider.
 * @param req - The request.
 * @throws WayfException - if we spot spoofing or there is no defaumlt 
 */
private void setupReturnAddress(String spName,HttpServletRequest req) throws WayfException {
  DiscoveryResponse[] discoveryServices;
  Set<XMLObject> objects=new HashSet<XMLObject>();
  String defaultName=null;
  boolean foundSPName=false;
  EntityDescriptor sp=null;
  for (  IdPSiteSet metadataProvider : siteSets) {
    LOG.debug("String_Node_Str" + metadataProvider.getIdentifier() + "String_Node_Str"+ spName);
    if (metadataProvider.containsSP(spName)) {
      foundSPName=true;
      EntityDescriptor entity=metadataProvider.getEntity(spName);
      sp=entity;
      List<RoleDescriptor> roles=entity.getRoleDescriptors();
      for (      RoleDescriptor role : roles) {
        if (role instanceof SPSSODescriptor) {
          Extensions exts=role.getExtensions();
          if (exts != null) {
            objects.addAll(exts.getOrderedChildren());
          }
        }
      }
    }
    break;
  }
  if (!foundSPName) {
    LOG.error("String_Node_Str" + spName + "String_Node_Str");
    throw new WayfException("String_Node_Str" + StringEscapeUtils.escapeHtml(getHostnameByURI(spName)) + "String_Node_Str"+ StringEscapeUtils.escapeHtml(spName)+ "String_Node_Str"+ "String_Node_Str",true);
  }
  discoveryServices=new DiscoveryResponse[objects.size()];
  int dsCount=0;
  for (  XMLObject obj : objects) {
    if (obj instanceof DiscoveryResponse) {
      DiscoveryResponse ds=(DiscoveryResponse)obj;
      discoveryServices[dsCount++]=ds;
      if (ds.isDefault() || null == defaultName) {
        defaultName=ds.getLocation();
      }
    }
  }
  String returnName=req.getParameter(RETURN_PARAM_NAME);
  if (returnName == null || returnName.length() == 0) {
    returnName=getValue(req,RETURN_ATTRIBUTE_NAME);
  }
  String returnIndex=req.getParameter(RETURN_INDEX_NAME);
  if (returnName != null && returnName.length() != 0) {
    String nameNoParam=returnName;
    URL providedReturnURL;
    int index=nameNoParam.indexOf('?');
    boolean found=false;
    if (index >= 0) {
      nameNoParam=nameNoParam.substring(0,index);
    }
    try {
      providedReturnURL=new URL(nameNoParam);
    }
 catch (    MalformedURLException e) {
      throw new WayfException("String_Node_Str" + nameNoParam,e);
    }
    for (    DiscoveryResponse disc : discoveryServices) {
      if (equalsURL(disc,providedReturnURL)) {
        found=true;
        break;
      }
    }
    if (!found) {
      String contactMsg="String_Node_Str";
      ContactPerson person=null;
      try {
        List<ContactPersonTypeEnumeration> cpTypeOrder=new Vector<ContactPersonTypeEnumeration>();
        cpTypeOrder.add(ContactPersonTypeEnumeration.TECHNICAL);
        cpTypeOrder.add(ContactPersonTypeEnumeration.SUPPORT);
        cpTypeOrder.add(ContactPersonTypeEnumeration.ADMINISTRATIVE);
        cpTypeOrder.add(ContactPersonTypeEnumeration.OTHER);
        cpTypeOrder.add(ContactPersonTypeEnumeration.BILLING);
        for (        ContactPerson thisPerson : ((SPSSODescriptor)sp.getRoleDescriptors(SPSSODescriptor.DEFAULT_ELEMENT_NAME).get(0)).getContactPersons()) {
          if (person == null) {
            person=thisPerson;
            continue;
          }
          ;
          if (cpTypeOrder.indexOf(thisPerson) < cpTypeOrder.indexOf(person)) {
            person=thisPerson;
          }
          ;
        }
        ;
        if (person != null) {
          String emailAddrStr=null;
          for (          EmailAddress emailAddr : person.getEmailAddresses()) {
            if (emailAddrStr == null) {
              emailAddrStr=StringEscapeUtils.escapeHtml(emailAddr.getAddress());
              if (emailAddrStr.startsWith(MAILTO_URI_PREFIX)) {
                emailAddrStr=emailAddrStr.substring(MAILTO_URI_PREFIX.length());
              }
              ;
              break;
            }
            ;
          }
          ;
          contactMsg="String_Node_Str" + StringEscapeUtils.escapeHtml(person.getGivenName().getName() + "String_Node_Str" + person.getSurName().getName()) + (person.getCompany() != null ? "String_Node_Str" + StringEscapeUtils.escapeHtml(person.getCompany().getName()) : "String_Node_Str")+ (emailAddrStr != null ? "String_Node_Str" + emailAddrStr + "String_Node_Str"+ emailAddrStr+ "String_Node_Str" : "String_Node_Str")+ "String_Node_Str";
        }
        ;
      }
 catch (      Exception e) {
        LOG.error("String_Node_Str" + spName,e);
      }
      ;
      throw new WayfException("String_Node_Str" + StringEscapeUtils.escapeHtml(getHostnameByURI(spName)) + "String_Node_Str"+ StringEscapeUtils.escapeHtml(spName)+ "String_Node_Str"+ "String_Node_Str"+ StringEscapeUtils.escapeHtml(nameNoParam)+ "String_Node_Str"+ contactMsg,true);
    }
  }
 else   if (returnIndex != null && returnIndex.length() != 0) {
    int index;
    try {
      index=Integer.parseInt(returnIndex);
    }
 catch (    NumberFormatException e) {
      throw new WayfException("String_Node_Str" + returnIndex + "String_Node_Str");
    }
    boolean found=false;
    for (    DiscoveryResponse disc : discoveryServices) {
      if (index == disc.getIndex()) {
        found=true;
        returnName=disc.getLocation();
        break;
      }
    }
    if (!found) {
      throw new WayfException("String_Node_Str" + returnIndex + "String_Node_Str");
    }
  }
 else {
    returnName=defaultName;
  }
  req.setAttribute(RETURN_ATTRIBUTE_NAME,returnName);
}

Fixed version (ground truth):
/** 
 * This sets up the parameter RETURN_ATTRIBUTE_NAME with the return address  harvested from the reqest. <ul><le>If a "return" parameter is present we check in the metadata for spoofing  and then set up from there </le> <le>If "returnID" is specified we get this from the metadata</le> <le>If nothing is provided we get the default from the metadata (if provided)</le> <le>Otherwise we whine</le> </ul>     
 * @param spName - the name of the Service provider.
 * @param req - The request.
 * @throws WayfException - if we spot spoofing or there is no defaumlt 
 */
private void setupReturnAddress(String spName,HttpServletRequest req) throws WayfException {
  DiscoveryResponse[] discoveryServices;
  Set<XMLObject> objects=new HashSet<XMLObject>();
  String defaultName=null;
  boolean foundSPName=false;
  EntityDescriptor sp=null;
  for (  IdPSiteSet metadataProvider : siteSets) {
    LOG.debug("String_Node_Str" + metadataProvider.getIdentifier() + "String_Node_Str"+ spName);
    if (metadataProvider.containsSP(spName)) {
      foundSPName=true;
      EntityDescriptor entity=metadataProvider.getEntity(spName);
      sp=entity;
      List<RoleDescriptor> roles=entity.getRoleDescriptors();
      for (      RoleDescriptor role : roles) {
        if (role instanceof SPSSODescriptor) {
          Extensions exts=role.getExtensions();
          if (exts != null) {
            objects.addAll(exts.getOrderedChildren());
          }
        }
      }
      break;
    }
  }
  if (!foundSPName) {
    LOG.error("String_Node_Str" + spName + "String_Node_Str");
    throw new WayfException("String_Node_Str" + StringEscapeUtils.escapeHtml(getHostnameByURI(spName)) + "String_Node_Str"+ StringEscapeUtils.escapeHtml(spName)+ "String_Node_Str"+ "String_Node_Str",true);
  }
  discoveryServices=new DiscoveryResponse[objects.size()];
  int dsCount=0;
  for (  XMLObject obj : objects) {
    if (obj instanceof DiscoveryResponse) {
      DiscoveryResponse ds=(DiscoveryResponse)obj;
      discoveryServices[dsCount++]=ds;
      if (ds.isDefault() || null == defaultName) {
        defaultName=ds.getLocation();
      }
    }
  }
  String returnName=req.getParameter(RETURN_PARAM_NAME);
  if (returnName == null || returnName.length() == 0) {
    returnName=getValue(req,RETURN_ATTRIBUTE_NAME);
  }
  String returnIndex=req.getParameter(RETURN_INDEX_NAME);
  if (returnName != null && returnName.length() != 0) {
    String nameNoParam=returnName;
    URL providedReturnURL;
    int index=nameNoParam.indexOf('?');
    boolean found=false;
    if (index >= 0) {
      nameNoParam=nameNoParam.substring(0,index);
    }
    try {
      providedReturnURL=new URL(nameNoParam);
    }
 catch (    MalformedURLException e) {
      throw new WayfException("String_Node_Str" + nameNoParam,e);
    }
    for (    DiscoveryResponse disc : discoveryServices) {
      if (equalsURL(disc,providedReturnURL)) {
        found=true;
        break;
      }
    }
    if (!found) {
      String contactMsg="String_Node_Str";
      ContactPerson person=null;
      try {
        List<ContactPersonTypeEnumeration> cpTypeOrder=new Vector<ContactPersonTypeEnumeration>();
        cpTypeOrder.add(ContactPersonTypeEnumeration.TECHNICAL);
        cpTypeOrder.add(ContactPersonTypeEnumeration.SUPPORT);
        cpTypeOrder.add(ContactPersonTypeEnumeration.ADMINISTRATIVE);
        cpTypeOrder.add(ContactPersonTypeEnumeration.OTHER);
        cpTypeOrder.add(ContactPersonTypeEnumeration.BILLING);
        for (        ContactPerson thisPerson : ((SPSSODescriptor)sp.getRoleDescriptors(SPSSODescriptor.DEFAULT_ELEMENT_NAME).get(0)).getContactPersons()) {
          if (person == null) {
            person=thisPerson;
            continue;
          }
          ;
          if (cpTypeOrder.indexOf(thisPerson) < cpTypeOrder.indexOf(person)) {
            person=thisPerson;
          }
          ;
        }
        ;
        if (person != null) {
          String emailAddrStr=null;
          for (          EmailAddress emailAddr : person.getEmailAddresses()) {
            if (emailAddrStr == null) {
              emailAddrStr=StringEscapeUtils.escapeHtml(emailAddr.getAddress());
              if (emailAddrStr.startsWith(MAILTO_URI_PREFIX)) {
                emailAddrStr=emailAddrStr.substring(MAILTO_URI_PREFIX.length());
              }
              ;
              break;
            }
            ;
          }
          ;
          contactMsg="String_Node_Str" + StringEscapeUtils.escapeHtml(person.getGivenName().getName() + "String_Node_Str" + person.getSurName().getName()) + (person.getCompany() != null ? "String_Node_Str" + StringEscapeUtils.escapeHtml(person.getCompany().getName()) : "String_Node_Str")+ (emailAddrStr != null ? "String_Node_Str" + emailAddrStr + "String_Node_Str"+ emailAddrStr+ "String_Node_Str" : "String_Node_Str")+ "String_Node_Str";
        }
        ;
      }
 catch (      Exception e) {
        LOG.error("String_Node_Str" + spName,e);
      }
      ;
      throw new WayfException("String_Node_Str" + StringEscapeUtils.escapeHtml(getHostnameByURI(spName)) + "String_Node_Str"+ StringEscapeUtils.escapeHtml(spName)+ "String_Node_Str"+ "String_Node_Str"+ StringEscapeUtils.escapeHtml(nameNoParam)+ "String_Node_Str"+ contactMsg,true);
    }
  }
 else   if (returnIndex != null && returnIndex.length() != 0) {
    int index;
    try {
      index=Integer.parseInt(returnIndex);
    }
 catch (    NumberFormatException e) {
      throw new WayfException("String_Node_Str" + returnIndex + "String_Node_Str");
    }
    boolean found=false;
    for (    DiscoveryResponse disc : discoveryServices) {
      if (index == disc.getIndex()) {
        found=true;
        returnName=disc.getLocation();
        break;
      }
    }
    if (!found) {
      throw new WayfException("String_Node_Str" + returnIndex + "String_Node_Str");
    }
  }
 else {
    returnName=defaultName;
  }
  req.setAttribute(RETURN_ATTRIBUTE_NAME,returnName);
}