Program: 80496
Project: 80496
Buggy source:
/** 
 * @param fm, null for the root step
 * @param context
 * @return true, if calculating query for the current path is done
 */
private boolean calculateQuery(FieldMapping fm,ClosureQueryContext context){
  boolean ret=true;
  CompoundObjectType compoundType;
  if (fm == null)   compoundType=domainAccessHandler.getCompoundTypeFor(context.domainObjectClass);
 else {
    String classFieldName=fm.getClassFieldName();
    compoundType=MappingUtil.internalDomainAccess.get().getConcreteFieldType(classFieldName);
  }
  Class<? extends Object> pureType=fm != null ? fm.getFieldType() : context.domainObjectClass;
  if (fm != null)   this.addToQuery(fm,context);
  boolean resolveDeep=true;
  boolean walkedToIndex=this.subPathIndex == -1;
  if (walkedToIndex) {
    if (context.getRecursionCount() >= domainAccessHandler.maxRecursionCount)     resolveDeep=false;
  }
  context.path.add(new PathElement(pureType));
  boolean isCollection=Collection.class.isAssignableFrom(pureType);
  if (isCollection) {
    String classFieldName=fm.getClassFieldName();
    compoundType=MappingUtil.internalDomainAccess.get().getFieldComponentType(classFieldName);
  }
  boolean terminatesClause=true;
  if (compoundType != null) {
    ObjectMapping objectMapping=domainAccessHandler.getCompoundObjectMappingFor(compoundType,null);
    Iterator<FieldMapping> it=objectMapping.fieldMappingsIterator();
    boolean subPathWalked=false;
    int idx=0;
    while (it.hasNext()) {
      FieldMapping fMap=it.next();
      idx++;
      if (!walkedToIndex) {
        terminatesClause=false;
        if (idx != this.subPathIndex)         continue;
 else         walkedToIndex=true;
      }
      if (fMap.needsRelation() && resolveDeep) {
        terminatesClause=false;
        boolean needToComeBack=false;
        if (!subPathWalked) {
          if (this.next == null)           this.next=new Step();
          PathElement pe=context.getLastPathElement();
          pe.fieldIndex=idx;
          pe.fieldName=fMap.getFieldName();
          boolean isDone=this.next.calculateQuery(fMap,context);
          if (!isDone) {
            needToComeBack=true;
          }
 else {
            this.next=null;
            subPathWalked=true;
          }
        }
 else {
          needToComeBack=true;
        }
        if (needToComeBack) {
          this.subPathIndex=idx;
          ret=false;
          break;
        }
      }
    }
  }
  context.path.remove(context.path.size() - 1);
  if (!resolveDeep || terminatesClause) {
    String nm=this.buildNodeOrRelationName(context.path,DomainAccessHandler.NodePrefix,context.clauseRepetitionNumber);
    context.queryEndNodes.add(nm);
  }
  return ret;
}

Fixed version (ground truth):
/** 
 * @param fm, null for the root step
 * @param context
 * @return true, if calculating query for the current path is done
 */
private boolean calculateQuery(FieldMapping fm,ClosureQueryContext context){
  boolean ret=true;
  CompoundObjectType compoundType;
  if (fm == null)   compoundType=domainAccessHandler.getCompoundTypeFor(context.domainObjectClass);
 else {
    String classFieldName=fm.getClassFieldName();
    compoundType=MappingUtil.internalDomainAccess.get().getConcreteFieldType(classFieldName);
  }
  Class<? extends Object> pureType=fm != null ? fm.getFieldType() : context.domainObjectClass;
  if (fm != null)   this.addToQuery(fm,context);
  boolean resolveDeep=true;
  boolean walkedToIndex=this.subPathIndex == -1;
  boolean subPathWalked=false;
  if (walkedToIndex) {
    if (context.getRecursionCount() >= domainAccessHandler.maxRecursionCount)     resolveDeep=false;
    if (fm != null)     subPathWalked=true;
  }
  context.path.add(new PathElement(pureType));
  boolean isCollection=Collection.class.isAssignableFrom(pureType);
  if (isCollection) {
    String classFieldName=fm.getClassFieldName();
    compoundType=MappingUtil.internalDomainAccess.get().getFieldComponentType(classFieldName);
  }
  boolean terminatesClause=true;
  if (compoundType != null) {
    ObjectMapping objectMapping=domainAccessHandler.getCompoundObjectMappingFor(compoundType,null);
    Iterator<FieldMapping> it=objectMapping.fieldMappingsIterator();
    int idx=0;
    while (it.hasNext()) {
      FieldMapping fMap=it.next();
      idx++;
      if (!walkedToIndex) {
        if (idx != this.subPathIndex)         continue;
 else         walkedToIndex=true;
      }
      if (fMap.needsRelation() && resolveDeep) {
        boolean needToComeBack=false;
        if (!subPathWalked) {
          terminatesClause=false;
          if (this.next == null)           this.next=new Step();
          PathElement pe=context.getLastPathElement();
          pe.fieldIndex=idx;
          pe.fieldName=fMap.getFieldName();
          boolean isDone=this.next.calculateQuery(fMap,context);
          if (!isDone) {
            needToComeBack=true;
          }
 else {
            this.next=null;
            subPathWalked=true;
          }
        }
 else {
          needToComeBack=true;
        }
        if (needToComeBack) {
          this.subPathIndex=idx;
          ret=false;
          break;
        }
      }
    }
  }
  context.path.remove(context.path.size() - 1);
  if (!resolveDeep || terminatesClause) {
    String nm=this.buildNodeOrRelationName(context.path,DomainAccessHandler.NodePrefix,context.clauseRepetitionNumber);
    context.queryEndNodes.add(nm);
    if (!resolveDeep)     context.recursionExitNodes.add(nm);
  }
  return ret;
}