Program: 66373
Project: 66373
Buggy source:
/** 
 * @throws IOException 
 * @throws DataException 
 */
private void prepareSortedStacks() throws DataException, IOException {
  allSortedFactRows=new ArrayList();
  int levelSize=0;
  int measureSize=0;
  while (true) {
    int maxLevelCount=-1;
    int aggregationIndex=-1;
    int[] levelSortType=null;
    for (int i=0; i < aggregationCalculators.length; i++) {
      if (sortedFactRows[i] == null && ((aggregationCalculators[i].aggregation.getLevels() != null && aggregationCalculators[i].aggregation.getLevels().length > maxLevelCount) || (aggregationCalculators[i].aggregation.getLevels() == null && maxLevelCount == -1))) {
        aggregationIndex=i;
        if (aggregationCalculators[i].aggregation.getLevels() != null)         maxLevelCount=aggregationCalculators[i].aggregation.getLevels().length;
 else         maxLevelCount=0;
        levelSortType=aggregationCalculators[i].aggregation.getSortTypes();
      }
    }
    if (aggregationIndex == -1) {
      break;
    }
    if (memoryCacheSize != 0) {
      if (levelSize == 0)       levelSize=getLevelSize(aggregationCalculators[aggregationIndex].aggregation.getLevels());
 else {
        if (aggregationCalculators[aggregationIndex].aggregation.getLevels() != null)         levelSize+=SizeOfUtil.getArraySize(aggregationCalculators[aggregationIndex].aggregation.getLevels().length);
      }
      if (measureSize == 0)       measureSize=getMeasureSize();
 else {
        if (dataSet4Aggregation.getMetaInfo().getMeasureInfos() != null)         measureSize+=SizeOfUtil.getArraySize(dataSet4Aggregation.getMetaInfo().getMeasureInfos().length);
      }
    }
    Comparator comparator=new Row4AggregationComparator(levelSortType);
    DiskSortedStack diskSortedStack=new DiskSortedStack(100,false,comparator,Row4Aggregation.getCreator());
    if (memoryCacheSize == 0) {
      diskSortedStack.setBufferSize(10000);
      diskSortedStack.setUseMemoryOnly(true);
    }
    DiskSortedStackWrapper diskSortedStackReader=new DiskSortedStackWrapper(diskSortedStack,levelIndex[aggregationIndex]);
    this.allSortedFactRows.add(diskSortedStackReader);
    for (int i=0; i < aggregationCalculators.length; i++) {
      if (sortedFactRows[i] == null && cover(levelIndex[aggregationIndex],levelIndex[i])) {
        sortedFactRows[i]=diskSortedStackReader;
      }
    }
  }
  if (memoryCacheSize > 0) {
    int rowSize=16 + (4 + (levelSize + measureSize) - 1) / 8 * 8;
    int bufferSize=(int)(this.memoryCacheSize * 4 / 5 / rowSize);
    for (int i=0; i < allSortedFactRows.size(); i++) {
      DiskSortedStackWrapper diskSortedStackReader=(DiskSortedStackWrapper)allSortedFactRows.get(i);
      diskSortedStackReader.getDiskSortedStack().setBufferSize(bufferSize);
    }
  }
}

Fixed version (ground truth):
/** 
 * @throws IOException 
 * @throws DataException 
 */
private void prepareSortedStacks() throws DataException, IOException {
  allSortedFactRows=new ArrayList();
  int levelSize=0;
  int measureSize=0;
  while (true) {
    int maxLevelCount=-1;
    int aggregationIndex=-1;
    int[] levelSortType=null;
    for (int i=0; i < aggregationCalculators.length; i++) {
      if (sortedFactRows[i] == null && ((aggregationCalculators[i].aggregation.getLevels() != null && aggregationCalculators[i].aggregation.getLevels().length > maxLevelCount) || (aggregationCalculators[i].aggregation.getLevels() == null && maxLevelCount == -1))) {
        aggregationIndex=i;
        if (aggregationCalculators[i].aggregation.getLevels() != null)         maxLevelCount=aggregationCalculators[i].aggregation.getLevels().length;
 else         maxLevelCount=0;
        levelSortType=aggregationCalculators[i].aggregation.getSortTypes();
      }
    }
    if (aggregationIndex == -1) {
      break;
    }
    if (memoryCacheSize != 0) {
      if (levelSize == 0)       levelSize=getLevelSize(aggregationCalculators[aggregationIndex].aggregation.getLevels());
 else {
        if (aggregationCalculators[aggregationIndex].aggregation.getLevels() != null)         levelSize+=SizeOfUtil.getArraySize(aggregationCalculators[aggregationIndex].aggregation.getLevels().length);
      }
      if (measureSize == 0)       measureSize=getMeasureSize();
 else {
        if (dataSet4Aggregation.getMetaInfo().getMeasureInfos() != null)         measureSize+=SizeOfUtil.getArraySize(dataSet4Aggregation.getMetaInfo().getMeasureInfos().length);
      }
    }
    Comparator comparator=new Row4AggregationComparator(levelSortType);
    DiskSortedStack diskSortedStack=new DiskSortedStack(100,false,comparator,Row4Aggregation.getCreator());
    if (memoryCacheSize == 0) {
      diskSortedStack.setBufferSize(10000);
      diskSortedStack.setUseMemoryOnly(true);
    }
    DiskSortedStackWrapper diskSortedStackReader=new DiskSortedStackWrapper(diskSortedStack,levelIndex[aggregationIndex]);
    this.allSortedFactRows.add(diskSortedStackReader);
    for (int i=0; i < aggregationCalculators.length; i++) {
      if (sortedFactRows[i] == null && cover(levelIndex[aggregationIndex],levelIndex[i])) {
        sortedFactRows[i]=diskSortedStackReader;
      }
    }
  }
  mergeRow4Aggregations=new MergeRow4Aggregation[allSortedFactRows.size()];
  int bufferSize=10000;
  if (memoryCacheSize > 0) {
    int rowSize=16 + (4 + (levelSize + measureSize) - 1) / 8 * 8;
    bufferSize=(int)(this.memoryCacheSize * 4 / 5 / rowSize);
    if (this.simpleFunc == null)     bufferSize/=5;
    for (int i=0; i < allSortedFactRows.size(); i++) {
      DiskSortedStackWrapper diskSortedStackReader=(DiskSortedStackWrapper)allSortedFactRows.get(i);
      diskSortedStackReader.getDiskSortedStack().setBufferSize(bufferSize);
    }
  }
  for (int i=0; i < allSortedFactRows.size(); i++) {
    mergeRow4Aggregations[i]=new MergeRow4Aggregation(bufferSize,simpleFunc,measureIndexes4Merge,parameterColIndex4Merge);
  }
}