diff --git a/java/java-psi-impl/src/com/intellij/psi/controlFlow/DefUseUtil.java b/java/java-psi-impl/src/com/intellij/psi/controlFlow/DefUseUtil.java index b494e8eded30..0acfcda94343 100644 --- a/java/java-psi-impl/src/com/intellij/psi/controlFlow/DefUseUtil.java +++ b/java/java-psi-impl/src/com/intellij/psi/controlFlow/DefUseUtil.java @@ -169,7 +169,14 @@ public class DefUseUtil { } } - Map stateMap = getStates(instructions); + Map stateMap; + try { + stateMap = getStates(instructions); + } + catch (InstructionKey.OverflowException e) { + LOG.error("Failed to compute paths in the control flow graph", e, flow.toString()); + return null; + } InstructionState[] states = stateMap.values().toArray(new InstructionState[0]); Arrays.sort(states); @@ -511,6 +518,7 @@ public class DefUseUtil { Set visited = new THashSet(instructions.size()); while (!myWalkThroughStack.isEmpty()) { + ProgressManager.checkCanceled(); InstructionKey fromKey = myWalkThroughStack.peekFrom(); InstructionKey nextKey = myWalkThroughStack.popNext(); addBackwardTrace(fromKey, nextKey); diff --git a/java/java-psi-impl/src/com/intellij/psi/controlFlow/InstructionKey.java b/java/java-psi-impl/src/com/intellij/psi/controlFlow/InstructionKey.java index 283e9dc182e1..06b056ca5627 100644 --- a/java/java-psi-impl/src/com/intellij/psi/controlFlow/InstructionKey.java +++ b/java/java-psi-impl/src/com/intellij/psi/controlFlow/InstructionKey.java @@ -42,6 +42,9 @@ class InstructionKey implements Comparable { } InstructionKey push(int nextOffset, int returnOffset) { + if(myCallStack.length > 100) { // normally it's way below 100, as it's the number of levels of nested 'finally' blocks + throw new OverflowException(myOffset); // most likely the graph traversal is in an endless loop + } int[] nextStack = ArrayUtil.append(myCallStack, returnOffset); return new InstructionKey(nextOffset, nextStack); } @@ -105,4 +108,10 @@ class InstructionKey implements Comparable { c = myCallStack.length - key.myCallStack.length; return c; } + + static class OverflowException extends RuntimeException { + public OverflowException(int offset) { + super("Instruction key overflow at offset " + offset); + } + } }