Program: 40969
Project: 40969
Buggy source:
/** 
 * This method finishes a pass. All code is updated; instructions are reordered and old labels may not be valid. If no patches were issued, we don't need to do anything at all; this case is detected quickly and no updates are made.
 * @return true iff non-trivial patches were applied
 */
public boolean applyPatches(){
  verifyState(DURING_PASS);
  state=EMITTING_CODE;
  if (patchCount == 0) {
    state=BEFORE_END_PASS;
    return false;
  }
  Output w=new Output(nextLabel);
  int[] oldInstructionsToNew=new int[instructions.length];
  w.additionalHandlers=noHandlers;
  w.originalBytecode=0;
  for (Patch p=methodStartPatches; p != null; p=p.next) {
    p.emitTo(w);
  }
  ExceptionHandler[] methodHandlers=makeExceptionArray(methodHandlerPatches);
  if (methodHandlers.length > 0) {
    w.codeChanged=true;
  }
  for (int i=0; i < instructions.length; i++) {
    ExceptionHandler[] basicHandlers=mergeHandlers(handlers[i],methodHandlers);
    HandlerPatch hp=instructionHandlerPatches[i];
    w.emitLabel(i);
    w.originalBytecode=instructionsToBytecodes[i];
    w.additionalHandlers=basicHandlers;
    for (Patch p=beforePatches[i]; p != null; p=p.next) {
      p.emitTo(w);
    }
    w.additionalHandlers=mergeHandlers(makeExceptionArray(hp),basicHandlers);
    Patch replace=replacementPatches[i];
    if (replace == null) {
      oldInstructionsToNew[i]=w.newInstructions.size();
      w.internalEmitInstruction(instructions[i]);
    }
 else {
      w.codeChanged=true;
      oldInstructionsToNew[i]=-1;
      replace.emitTo(w);
    }
    w.additionalHandlers=basicHandlers;
    for (Patch p=afterPatches[i]; p != null; p=p.next) {
      p.emitTo(w);
    }
    if (hp != null) {
      w.codeChanged=true;
      GotoInstruction branchOver=GotoInstruction.make(i + 1);
      w.internalEmitInstruction(branchOver);
      for (HandlerPatch hpIterator=hp; hpIterator != null; hpIterator=hpIterator.next) {
        w.additionalHandlers=mergeHandlers(makeExceptionArray(hpIterator.next),basicHandlers);
        w.emitLabel(hpIterator.label);
        hpIterator.patch.emitTo(w);
        w.internalEmitInstruction(branchOver);
      }
    }
  }
  w.originalBytecode=0;
  for (HandlerPatch hpIterator=methodHandlerPatches; hpIterator != null; hpIterator=hpIterator.next) {
    w.additionalHandlers=makeExceptionArray(hpIterator.next);
    w.emitLabel(hpIterator.label);
    hpIterator.patch.emitTo(w);
  }
  w.additionalHandlers=noHandlers;
  for (Patch p=afterMethodPatches; p != null; p=p.next) {
    p.emitTo(w);
  }
  state=BEFORE_END_PASS;
  if (!w.codeChanged) {
    return false;
  }
  instructions=new Instruction[w.newInstructions.size()];
  handlers=new ExceptionHandler[instructions.length][];
  instructionsToBytecodes=new int[instructions.length];
  w.newInstructions.toArray(instructions);
  w.newInstructionHandlers.toArray(handlers);
  System.arraycopy(w.instructionsToBytecodes,0,instructionsToBytecodes,0,instructionsToBytecodes.length);
  int[] labelDefs=w.labelDefs;
  int[] newInstructionsToOld=new int[instructions.length];
  for (int i=0; i < instructions.length; i++) {
    instructions[i]=instructions[i].redirectTargets(labelDefs);
    newInstructionsToOld[i]=-1;
  }
  HashSet<ExceptionHandler> adjustedHandlers=null;
  for (int i=0; i < handlers.length; i++) {
    ExceptionHandler[] hs=handlers[i];
    if (hs.length > 0 && (i == 0 || hs != handlers[i - 1])) {
      if (adjustedHandlers == null) {
        adjustedHandlers=new HashSet<ExceptionHandler>();
      }
      for (int j=0; j < hs.length; j++) {
        ExceptionHandler h=hs[j];
        if (!adjustedHandlers.contains(h)) {
          adjustedHandlers.add(h);
          h.handler=labelDefs[h.handler];
        }
      }
    }
  }
  if (methodInfo != null) {
    for (int i=0; i < oldInstructionsToNew.length; i++) {
      if (oldInstructionsToNew[i] != -1) {
        newInstructionsToOld[oldInstructionsToNew[i]]=i;
      }
    }
    methodInfo.update(instructions,handlers,newInstructionsToOld,instructionsToBytecodes);
  }
  return true;
}

Fixed version (ground truth):
/** 
 * This method finishes a pass. All code is updated; instructions are reordered and old labels may not be valid. If no patches were issued, we don't need to do anything at all; this case is detected quickly and no updates are made.
 * @return true iff non-trivial patches were applied
 */
public boolean applyPatches() throws IllegalArgumentException {
  verifyState(DURING_PASS);
  state=EMITTING_CODE;
  if (patchCount == 0) {
    state=BEFORE_END_PASS;
    return false;
  }
  Output w=new Output(nextLabel);
  int[] oldInstructionsToNew=new int[instructions.length];
  w.additionalHandlers=noHandlers;
  w.originalBytecode=0;
  for (Patch p=methodStartPatches; p != null; p=p.next) {
    p.emitTo(w);
  }
  ExceptionHandler[] methodHandlers=makeExceptionArray(methodHandlerPatches);
  if (methodHandlers.length > 0) {
    w.codeChanged=true;
  }
  for (int i=0; i < instructions.length; i++) {
    ExceptionHandler[] basicHandlers=mergeHandlers(handlers[i],methodHandlers);
    HandlerPatch hp=instructionHandlerPatches[i];
    w.emitLabel(i);
    w.originalBytecode=instructionsToBytecodes[i];
    w.additionalHandlers=basicHandlers;
    for (Patch p=beforePatches[i]; p != null; p=p.next) {
      p.emitTo(w);
    }
    w.additionalHandlers=mergeHandlers(makeExceptionArray(hp),basicHandlers);
    Patch replace=replacementPatches[i];
    if (replace == null) {
      oldInstructionsToNew[i]=w.newInstructions.size();
      w.internalEmitInstruction(instructions[i]);
    }
 else {
      w.codeChanged=true;
      oldInstructionsToNew[i]=-1;
      replace.emitTo(w);
    }
    w.additionalHandlers=basicHandlers;
    for (Patch p=afterPatches[i]; p != null; p=p.next) {
      p.emitTo(w);
    }
    if (hp != null) {
      w.codeChanged=true;
      GotoInstruction branchOver=GotoInstruction.make(i + 1);
      w.internalEmitInstruction(branchOver);
      for (HandlerPatch hpIterator=hp; hpIterator != null; hpIterator=hpIterator.next) {
        w.additionalHandlers=mergeHandlers(makeExceptionArray(hpIterator.next),basicHandlers);
        w.emitLabel(hpIterator.label);
        hpIterator.patch.emitTo(w);
        w.internalEmitInstruction(branchOver);
      }
    }
  }
  w.originalBytecode=0;
  for (HandlerPatch hpIterator=methodHandlerPatches; hpIterator != null; hpIterator=hpIterator.next) {
    w.additionalHandlers=makeExceptionArray(hpIterator.next);
    w.emitLabel(hpIterator.label);
    hpIterator.patch.emitTo(w);
  }
  w.additionalHandlers=noHandlers;
  for (Patch p=afterMethodPatches; p != null; p=p.next) {
    p.emitTo(w);
  }
  state=BEFORE_END_PASS;
  if (!w.codeChanged) {
    return false;
  }
  instructions=new Instruction[w.newInstructions.size()];
  handlers=new ExceptionHandler[instructions.length][];
  instructionsToBytecodes=new int[instructions.length];
  w.newInstructions.toArray(instructions);
  w.newInstructionHandlers.toArray(handlers);
  System.arraycopy(w.instructionsToBytecodes,0,instructionsToBytecodes,0,instructionsToBytecodes.length);
  int[] labelDefs=w.labelDefs;
  int[] newInstructionsToOld=new int[instructions.length];
  for (int i=0; i < instructions.length; i++) {
    instructions[i]=instructions[i].redirectTargets(labelDefs);
    newInstructionsToOld[i]=-1;
  }
  HashSet<ExceptionHandler> adjustedHandlers=null;
  for (int i=0; i < handlers.length; i++) {
    ExceptionHandler[] hs=handlers[i];
    if (hs.length > 0 && (i == 0 || hs != handlers[i - 1])) {
      if (adjustedHandlers == null) {
        adjustedHandlers=new HashSet<ExceptionHandler>();
      }
      for (int j=0; j < hs.length; j++) {
        ExceptionHandler h=hs[j];
        if (!adjustedHandlers.contains(h)) {
          adjustedHandlers.add(h);
          h.handler=labelDefs[h.handler];
        }
      }
    }
  }
  if (methodInfo != null) {
    for (int i=0; i < oldInstructionsToNew.length; i++) {
      if (oldInstructionsToNew[i] != -1) {
        newInstructionsToOld[oldInstructionsToNew[i]]=i;
      }
    }
    methodInfo.update(instructions,handlers,newInstructionsToOld,instructionsToBytecodes);
  }
  return true;
}