Program: 146588
Project: 146588
Buggy source:
/** 
 * Recursive helper function for expandReference that performs the search for all repeated nodes that match the pattern of the 'ref' argument.
 * @param sourceRef      original path we're matching against
 * @param sourceInstance original node obtained from sourceRef
 * @param workingRef     explicit path that refers to the current node
 * @param refs           Accumulator vector to collect matching paths. Containedreferences are unambiguous. Template nodes won't be included when matching INDEX_UNBOUND, but will be when INDEX_TEMPLATE is explicitly set.
 */
private void expandReferenceAccumulator(TreeReference sourceRef,DataInstance sourceInstance,TreeReference workingRef,Vector<TreeReference> refs,boolean includeTemplates){
  int depth=workingRef.size();
  if (depth == sourceRef.size()) {
    refs.addElement(workingRef);
    return;
  }
  String name=sourceRef.getName(depth);
  int mult=sourceRef.getMultiplicity(depth);
  Vector<XPathExpression> predicates=sourceRef.getPredicate(depth);
  Vector<XPathExpression> originalPredicates=predicates;
  if (predicates != null) {
    Vector<XPathExpression> predCopy=new Vector<>(predicates.size());
    for (    XPathExpression xpe : predicates) {
      predCopy.addElement(xpe);
    }
    predicates=predCopy;
  }
  AbstractTreeElement node=sourceInstance.resolveReference(workingRef);
  this.openBulkTrace();
  Collection<TreeReference> childSet=node.tryBatchChildFetch(name,mult,predicates,this);
  this.reportBulkTraceResults(originalPredicates,predicates,childSet);
  this.closeTrace();
  if (childSet == null) {
    childSet=loadReferencesChildren(node,name,mult,includeTemplates);
  }
  QueryContext subContext=queryContext.checkForDerivativeContextAndReturn(childSet == null ? 0 : childSet.size());
  if (this.getOriginalContextForPropogation() == null && subContext != queryContext) {
    subContext.setHackyOriginalContextBody(new CurrentModelQuerySet(childSet));
  }
  int[] positionContext=new int[predicates == null ? 0 : predicates.size()];
  for (  TreeReference refToExpand : childSet) {
    boolean passedAll=true;
    if (predicates != null && predicates.size() > 0) {
      int predIndex=-1;
      for (      XPathExpression predExpr : predicates) {
        predIndex++;
        positionContext[predIndex]++;
        EvaluationContext evalContext=rescope(refToExpand,positionContext[predIndex],subContext);
        Object o=predExpr.eval(sourceInstance,evalContext);
        o=FunctionUtils.unpack(o);
        boolean passed=false;
        if (o instanceof Double) {
          int intVal=FunctionUtils.toInt(o).intValue();
          passed=(intVal == positionContext[predIndex]);
        }
 else         if (o instanceof Boolean) {
          passed=(Boolean)o;
        }
        if (!passed) {
          passedAll=false;
          break;
        }
      }
    }
    if (passedAll) {
      expandReferenceAccumulator(sourceRef,sourceInstance,refToExpand,refs,includeTemplates);
    }
  }
}

Fixed version (ground truth):
/** 
 * Recursive helper function for expandReference that performs the search for all repeated nodes that match the pattern of the 'ref' argument.
 * @param sourceRef      original path we're matching against
 * @param sourceInstance original node obtained from sourceRef
 * @param workingRef     explicit path that refers to the current node
 * @param refs           Accumulator vector to collect matching paths. Containedreferences are unambiguous. Template nodes won't be included when matching INDEX_UNBOUND, but will be when INDEX_TEMPLATE is explicitly set.
 */
private void expandReferenceAccumulator(TreeReference sourceRef,DataInstance sourceInstance,TreeReference workingRef,Vector<TreeReference> refs,boolean includeTemplates){
  int depth=workingRef.size();
  if (depth == sourceRef.size()) {
    refs.addElement(workingRef);
    return;
  }
  String name=sourceRef.getName(depth);
  int mult=sourceRef.getMultiplicity(depth);
  Vector<XPathExpression> predicates=sourceRef.getPredicate(depth);
  Vector<XPathExpression> originalPredicates=predicates;
  if (predicates != null) {
    Vector<XPathExpression> predCopy=new Vector<>(predicates.size());
    for (    XPathExpression xpe : predicates) {
      predCopy.addElement(xpe);
    }
    predicates=predCopy;
  }
  AbstractTreeElement node=sourceInstance.resolveReference(workingRef,this);
  this.openBulkTrace();
  Collection<TreeReference> childSet=node.tryBatchChildFetch(name,mult,predicates,this);
  this.reportBulkTraceResults(originalPredicates,predicates,childSet);
  this.closeTrace();
  if (childSet == null) {
    childSet=loadReferencesChildren(node,name,mult,includeTemplates);
  }
  QueryContext subContext=queryContext.checkForDerivativeContextAndReturn(childSet == null ? 0 : childSet.size());
  if (this.getOriginalContextForPropogation() == null && subContext != queryContext) {
    subContext.setHackyOriginalContextBody(new CurrentModelQuerySet(childSet));
  }
  int[] positionContext=new int[predicates == null ? 0 : predicates.size()];
  for (  TreeReference refToExpand : childSet) {
    boolean passedAll=true;
    if (predicates != null && predicates.size() > 0) {
      int predIndex=-1;
      for (      XPathExpression predExpr : predicates) {
        predIndex++;
        positionContext[predIndex]++;
        EvaluationContext evalContext=rescope(refToExpand,positionContext[predIndex],subContext);
        Object o=predExpr.eval(sourceInstance,evalContext);
        o=FunctionUtils.unpack(o);
        boolean passed=false;
        if (o instanceof Double) {
          int intVal=FunctionUtils.toInt(o).intValue();
          passed=(intVal == positionContext[predIndex]);
        }
 else         if (o instanceof Boolean) {
          passed=(Boolean)o;
        }
        if (!passed) {
          passedAll=false;
          break;
        }
      }
    }
    if (passedAll) {
      expandReferenceAccumulator(sourceRef,sourceInstance,refToExpand,refs,includeTemplates);
    }
  }
}