Program: 185089
Project: 185089
Buggy source:
public static P canonicalise(P p,List<FieldRef> fieldRefs){
  P newP=Context.getWmlObjectFactory().createP();
  int depth=0;
  R newR=Context.getWmlObjectFactory().createR();
  RPr fieldRPr=null;
  FieldRef currentField=null;
  boolean seenSeparate=false;
  for (  Object o : p.getContent()) {
    if (o instanceof R) {
      R existingRun=(R)o;
      for (      Object o2 : existingRun.getContent()) {
        if (isCharType(o2,STFldCharType.BEGIN)) {
          seenSeparate=false;
          depth++;
          if (depth == 1) {
            newR=Context.getWmlObjectFactory().createR();
            newR.getContent().add(o2);
            currentField=new FieldRef();
            fieldRefs.add(currentField);
            currentField.setParent(newP);
            currentField.setBeginRun(newR);
          }
        }
 else         if (isCharType(o2,STFldCharType.END)) {
          if (!seenSeparate) {
            R separateR=Context.getWmlObjectFactory().createR();
            FldChar fldChar=Context.getWmlObjectFactory().createFldChar();
            fldChar.setFldCharType(STFldCharType.SEPARATE);
            newR.getContent().add(fldChar);
            newP.getContent().add(separateR);
            newR=Context.getWmlObjectFactory().createR();
            currentField.setResultsSlot(newR);
          }
          depth--;
          if (depth == 0) {
            newP.getContent().add(newR);
            newR=Context.getWmlObjectFactory().createR();
            newR.getContent().add(o2);
            newP.getContent().add(newR);
            currentField.setEndRun(newR);
            newR=Context.getWmlObjectFactory().createR();
          }
 else {
            newR.getContent().add(o2);
          }
        }
 else         if (isCharType(o2,STFldCharType.SEPARATE)) {
          seenSeparate=true;
          newR.getContent().add(o2);
          if (depth == 1) {
            newP.getContent().add(newR);
            newR=Context.getWmlObjectFactory().createR();
            newR.setRPr(fieldRPr);
            currentField.setResultsSlot(newR);
          }
        }
 else         if (o2 instanceof JAXBElement && ((JAXBElement)o2).getName().equals(_RInstrText_QNAME)) {
          currentField.setInstrText((JAXBElement<Text>)o2);
          newR.getContent().add(o2);
          fieldRPr=existingRun.getRPr();
          newR.setRPr(fieldRPr);
        }
 else {
          newR.getContent().add(o2);
          newR.setRPr(existingRun.getRPr());
          newP.getContent().add(newR);
          newR=Context.getWmlObjectFactory().createR();
        }
      }
    }
 else {
      if (newR.getContent().size() > 0) {
        newP.getContent().add(newR);
        newR=Context.getWmlObjectFactory().createR();
      }
      newP.getContent().add(o);
    }
  }
  if (newR.getContent().size() > 0 && !newP.getContent().contains(newR)) {
    newP.getContent().add(newR);
  }
  return newP;
}

Fixed version (ground truth):
public static P canonicalise(P p,List<FieldRef> fieldRefs){
  P newP=Context.getWmlObjectFactory().createP();
  newP.setPPr(p.getPPr());
  int depth=0;
  R newR=Context.getWmlObjectFactory().createR();
  RPr fieldRPr=null;
  FieldRef currentField=null;
  boolean seenSeparate=false;
  for (  Object o : p.getContent()) {
    if (o instanceof R) {
      R existingRun=(R)o;
      for (      Object o2 : existingRun.getContent()) {
        if (isCharType(o2,STFldCharType.BEGIN)) {
          seenSeparate=false;
          depth++;
          if (depth == 1) {
            newR=Context.getWmlObjectFactory().createR();
            newR.getContent().add(o2);
            currentField=new FieldRef();
            fieldRefs.add(currentField);
            currentField.setParent(newP);
            currentField.setBeginRun(newR);
          }
        }
 else         if (isCharType(o2,STFldCharType.END)) {
          if (!seenSeparate) {
            R separateR=Context.getWmlObjectFactory().createR();
            FldChar fldChar=Context.getWmlObjectFactory().createFldChar();
            fldChar.setFldCharType(STFldCharType.SEPARATE);
            newR.getContent().add(fldChar);
            newP.getContent().add(separateR);
            newR=Context.getWmlObjectFactory().createR();
            currentField.setResultsSlot(newR);
          }
          depth--;
          if (depth == 0) {
            newP.getContent().add(newR);
            newR=Context.getWmlObjectFactory().createR();
            newR.getContent().add(o2);
            newP.getContent().add(newR);
            currentField.setEndRun(newR);
            newR=Context.getWmlObjectFactory().createR();
          }
 else {
            newR.getContent().add(o2);
          }
        }
 else         if (isCharType(o2,STFldCharType.SEPARATE)) {
          seenSeparate=true;
          newR.getContent().add(o2);
          if (depth == 1) {
            newP.getContent().add(newR);
            newR=Context.getWmlObjectFactory().createR();
            newR.setRPr(fieldRPr);
            currentField.setResultsSlot(newR);
          }
        }
 else         if (o2 instanceof JAXBElement && ((JAXBElement)o2).getName().equals(_RInstrText_QNAME)) {
          currentField.setInstrText((JAXBElement<Text>)o2);
          newR.getContent().add(o2);
          fieldRPr=existingRun.getRPr();
          newR.setRPr(fieldRPr);
        }
 else {
          newR.getContent().add(o2);
          newR.setRPr(existingRun.getRPr());
          newP.getContent().add(newR);
          newR=Context.getWmlObjectFactory().createR();
        }
      }
    }
 else {
      if (newR.getContent().size() > 0) {
        newP.getContent().add(newR);
        newR=Context.getWmlObjectFactory().createR();
      }
      newP.getContent().add(o);
    }
  }
  if (newR.getContent().size() > 0 && !newP.getContent().contains(newR)) {
    newP.getContent().add(newR);
  }
  return newP;
}