Program: 23223
Project: 23223
Buggy source:
private void sortElements(ElementDefinitionHolder edh,ElementDefinitionComparer cmp,List<String> errors) throws FHIRException {
  if (edh.getChildren().size() == 1)   edh.getChildren().get(0).baseIndex=cmp.find(edh.getChildren().get(0).getSelf().getPath());
 else   Collections.sort(edh.getChildren(),cmp);
  cmp.checkForErrors(errors);
  for (  ElementDefinitionHolder child : edh.getChildren()) {
    if (child.getChildren().size() > 0) {
      ElementDefinition ed=cmp.snapshot.get(child.getBaseIndex());
      ElementDefinitionComparer ccmp;
      if (ed.getType().isEmpty() || isAbstract(ed.getType().get(0).getCode()) || ed.getType().get(0).getCode().equals(ed.getPath())) {
        ccmp=new ElementDefinitionComparer(true,cmp.snapshot,cmp.base,cmp.prefixLength,cmp.name);
      }
 else       if (ed.getType().get(0).getCode().equals("String_Node_Str") && child.getSelf().getType().size() == 1 && child.getSelf().getType().get(0).hasProfile()) {
        StructureDefinition profile=context.fetchResource(StructureDefinition.class,child.getSelf().getType().get(0).getProfile());
        if (profile == null)         ccmp=null;
 else         ccmp=new ElementDefinitionComparer(true,profile.getSnapshot().getElement(),ed.getType().get(0).getCode(),child.getSelf().getPath().length(),cmp.name);
      }
 else       if (ed.getType().size() == 1 && !ed.getType().get(0).getCode().equals("String_Node_Str")) {
        StructureDefinition profile=context.fetchResource(StructureDefinition.class,"String_Node_Str" + ed.getType().get(0).getCode());
        if (profile == null)         throw new FHIRException("String_Node_Str" + "String_Node_Str" + ed.getType().get(0).getCode() + "String_Node_Str"+ ed.getPath());
        ccmp=new ElementDefinitionComparer(false,profile.getSnapshot().getElement(),ed.getType().get(0).getCode(),child.getSelf().getPath().length(),cmp.name);
      }
 else       if (child.getSelf().getType().size() == 1) {
        StructureDefinition profile=context.fetchResource(StructureDefinition.class,"String_Node_Str" + child.getSelf().getType().get(0).getCode());
        if (profile == null)         throw new FHIRException("String_Node_Str" + "String_Node_Str" + ed.getType().get(0).getCode() + "String_Node_Str"+ ed.getPath());
        ccmp=new ElementDefinitionComparer(false,profile.getSnapshot().getElement(),child.getSelf().getType().get(0).getCode(),child.getSelf().getPath().length(),cmp.name);
      }
 else       if (ed.getPath().endsWith("String_Node_Str") && !child.getSelf().getPath().endsWith("String_Node_Str")) {
        String edLastNode=ed.getPath().replaceAll("String_Node_Str","String_Node_Str");
        String childLastNode=child.getSelf().getPath().replaceAll("String_Node_Str","String_Node_Str");
        String p=childLastNode.substring(edLastNode.length() - 3);
        StructureDefinition sd=context.fetchResource(StructureDefinition.class,"String_Node_Str" + p);
        if (sd == null)         throw new Error("String_Node_Str" + p);
        ccmp=new ElementDefinitionComparer(false,sd.getSnapshot().getElement(),p,child.getSelf().getPath().length(),cmp.name);
      }
 else       if (child.getSelf().hasType() && child.getSelf().getType().get(0).getCode().equals("String_Node_Str")) {
        for (        TypeRefComponent t : child.getSelf().getType()) {
          if (!t.getCode().equals("String_Node_Str")) {
            throw new Error("String_Node_Str" + ed.getPath() + "String_Node_Str"+ typeCode(ed.getType())+ "String_Node_Str");
          }
        }
        StructureDefinition profile=context.fetchResource(StructureDefinition.class,"String_Node_Str" + ed.getType().get(0).getCode());
        ccmp=new ElementDefinitionComparer(false,profile.getSnapshot().getElement(),ed.getType().get(0).getCode(),child.getSelf().getPath().length(),cmp.name);
      }
 else       if (!child.getSelf().hasType() && ed.getType().get(0).getCode().equals("String_Node_Str")) {
        for (        TypeRefComponent t : ed.getType()) {
          if (!t.getCode().equals("String_Node_Str")) {
            throw new Error("String_Node_Str" + ed.getPath() + "String_Node_Str"+ typeCode(ed.getType())+ "String_Node_Str");
          }
        }
        StructureDefinition profile=context.fetchResource(StructureDefinition.class,"String_Node_Str" + ed.getType().get(0).getCode());
        ccmp=new ElementDefinitionComparer(false,profile.getSnapshot().getElement(),ed.getType().get(0).getCode(),child.getSelf().getPath().length(),cmp.name);
      }
 else {
        throw new Error("String_Node_Str" + ed.getPath() + "String_Node_Str"+ typeCode(ed.getType())+ "String_Node_Str");
      }
      if (ccmp != null)       sortElements(child,ccmp,errors);
    }
  }
}

Fixed version (ground truth):
private void sortElements(ElementDefinitionHolder edh,ElementDefinitionComparer cmp,List<String> errors) throws FHIRException {
  if (edh.getChildren().size() == 1)   edh.getChildren().get(0).baseIndex=cmp.find(edh.getChildren().get(0).getSelf().getPath());
 else   Collections.sort(edh.getChildren(),cmp);
  cmp.checkForErrors(errors);
  for (  ElementDefinitionHolder child : edh.getChildren()) {
    if (child.getChildren().size() > 0) {
      ElementDefinition ed=cmp.snapshot.get(child.getBaseIndex());
      ElementDefinitionComparer ccmp;
      if (ed.getType().isEmpty() || isAbstract(ed.getType().get(0).getCode()) || ed.getType().get(0).getCode().equals(ed.getPath())) {
        ccmp=new ElementDefinitionComparer(true,cmp.snapshot,cmp.base,cmp.prefixLength,cmp.name);
      }
 else       if (ed.getType().get(0).getCode().equals("String_Node_Str") && child.getSelf().getType().size() == 1 && child.getSelf().getType().get(0).hasProfile()) {
        StructureDefinition profile=context.fetchResource(StructureDefinition.class,child.getSelf().getType().get(0).getProfile());
        if (profile == null)         ccmp=null;
 else         ccmp=new ElementDefinitionComparer(true,profile.getSnapshot().getElement(),ed.getType().get(0).getCode(),child.getSelf().getPath().length(),cmp.name);
      }
 else       if (ed.getType().size() == 1 && !ed.getType().get(0).getCode().equals("String_Node_Str")) {
        StructureDefinition profile=context.fetchResource(StructureDefinition.class,"String_Node_Str" + ed.getType().get(0).getCode());
        if (profile == null)         throw new FHIRException("String_Node_Str" + "String_Node_Str" + ed.getType().get(0).getCode() + "String_Node_Str"+ ed.getPath());
        ccmp=new ElementDefinitionComparer(false,profile.getSnapshot().getElement(),ed.getType().get(0).getCode(),child.getSelf().getPath().length(),cmp.name);
      }
 else       if (child.getSelf().getType().size() == 1) {
        StructureDefinition profile=context.fetchResource(StructureDefinition.class,"String_Node_Str" + child.getSelf().getType().get(0).getCode());
        if (profile == null)         throw new FHIRException("String_Node_Str" + "String_Node_Str" + ed.getType().get(0).getCode() + "String_Node_Str"+ ed.getPath());
        ccmp=new ElementDefinitionComparer(false,profile.getSnapshot().getElement(),child.getSelf().getType().get(0).getCode(),child.getSelf().getPath().length(),cmp.name);
      }
 else       if (ed.getPath().endsWith("String_Node_Str") && !child.getSelf().getPath().endsWith("String_Node_Str")) {
        String edLastNode=ed.getPath().replaceAll("String_Node_Str","String_Node_Str");
        String childLastNode=child.getSelf().getPath().replaceAll("String_Node_Str","String_Node_Str");
        String p=childLastNode.substring(edLastNode.length() - 3);
        if (isPrimitive(Utilities.uncapitalize(p)))         p=Utilities.uncapitalize(p);
        StructureDefinition sd=context.fetchResource(StructureDefinition.class,"String_Node_Str" + p);
        if (sd == null)         throw new Error("String_Node_Str" + p);
        ccmp=new ElementDefinitionComparer(false,sd.getSnapshot().getElement(),p,child.getSelf().getPath().length(),cmp.name);
      }
 else       if (child.getSelf().hasType() && child.getSelf().getType().get(0).getCode().equals("String_Node_Str")) {
        for (        TypeRefComponent t : child.getSelf().getType()) {
          if (!t.getCode().equals("String_Node_Str")) {
            throw new Error("String_Node_Str" + ed.getPath() + "String_Node_Str"+ typeCode(ed.getType())+ "String_Node_Str");
          }
        }
        StructureDefinition profile=context.fetchResource(StructureDefinition.class,"String_Node_Str" + ed.getType().get(0).getCode());
        ccmp=new ElementDefinitionComparer(false,profile.getSnapshot().getElement(),ed.getType().get(0).getCode(),child.getSelf().getPath().length(),cmp.name);
      }
 else       if (!child.getSelf().hasType() && ed.getType().get(0).getCode().equals("String_Node_Str")) {
        for (        TypeRefComponent t : ed.getType()) {
          if (!t.getCode().equals("String_Node_Str")) {
            throw new Error("String_Node_Str" + ed.getPath() + "String_Node_Str"+ typeCode(ed.getType())+ "String_Node_Str");
          }
        }
        StructureDefinition profile=context.fetchResource(StructureDefinition.class,"String_Node_Str" + ed.getType().get(0).getCode());
        ccmp=new ElementDefinitionComparer(false,profile.getSnapshot().getElement(),ed.getType().get(0).getCode(),child.getSelf().getPath().length(),cmp.name);
      }
 else {
        throw new Error("String_Node_Str" + ed.getPath() + "String_Node_Str"+ typeCode(ed.getType())+ "String_Node_Str");
      }
      if (ccmp != null)       sortElements(child,ccmp,errors);
    }
  }
}