From 6a98eecf0f0484edea884ef7bcd330a3b4913833 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Mon, 18 Mar 2024 14:18:08 +0100 Subject: [PATCH] [java-dfa] Copy state for catch (regression after AIOOBE handling) Fixes IDEA-349343 Exceptions during DFA Dataflow interpretation error (wasForciblyMerged = false), throwable=java.lang.IllegalStateException: Stack for instruction 72 increased by 13; it's likely that IR was built incorrectly GitOrigin-RevId: 2ebeeb9b58d6daa94ff885d20c606cae6e5391bb --- .../dataFlow/java/inst/ArrayAccessInstruction.java | 2 +- .../dataFlow/java/inst/ArrayStoreInstruction.java | 2 +- .../inspection/dataFlow/fixture/ArrayAccessInTry.java | 11 +++++++++++ .../java/codeInspection/DataFlowInspectionTest.java | 1 + 4 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 java/java-tests/testData/inspection/dataFlow/fixture/ArrayAccessInTry.java diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/java/inst/ArrayAccessInstruction.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/java/inst/ArrayAccessInstruction.java index ddedc34bad50..50a159ada658 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/java/inst/ArrayAccessInstruction.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/java/inst/ArrayAccessInstruction.java @@ -50,7 +50,7 @@ public class ArrayAccessInstruction extends ExpressionPushingInstruction { DfaValue array = stateBefore.pop(); List finalStates = new ArrayList<>(); if (myOutOfBoundsTransfer != null) { - finalStates.addAll(IndexOutOfBoundsProblem.dispatchTransfer(interpreter, stateBefore, myOutOfBoundsTransfer)); + finalStates.addAll(IndexOutOfBoundsProblem.dispatchTransfer(interpreter, stateBefore.createCopy(), myOutOfBoundsTransfer)); } DfaInstructionState[] states = myProblem.processOutOfBounds(interpreter, stateBefore, index, array, myOutOfBoundsTransfer); if (states != null) return states; diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/java/inst/ArrayStoreInstruction.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/java/inst/ArrayStoreInstruction.java index 865beb5cbb3d..1c4f0d175019 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/java/inst/ArrayStoreInstruction.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/java/inst/ArrayStoreInstruction.java @@ -56,7 +56,7 @@ public class ArrayStoreInstruction extends ExpressionPushingInstruction { DfaValue array = stateBefore.pop(); List finalStates = new ArrayList<>(); if (myOutOfBoundsTransfer != null) { - finalStates.addAll(IndexOutOfBoundsProblem.dispatchTransfer(interpreter, stateBefore, myOutOfBoundsTransfer)); + finalStates.addAll(IndexOutOfBoundsProblem.dispatchTransfer(interpreter, stateBefore.createCopy(), myOutOfBoundsTransfer)); } DfaInstructionState[] states = myIndexProblem.processOutOfBounds(interpreter, stateBefore, index, array, myOutOfBoundsTransfer); diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/ArrayAccessInTry.java b/java/java-tests/testData/inspection/dataFlow/fixture/ArrayAccessInTry.java new file mode 100644 index 000000000000..17b52e09a801 --- /dev/null +++ b/java/java-tests/testData/inspection/dataFlow/fixture/ArrayAccessInTry.java @@ -0,0 +1,11 @@ +class Scratch { + public static void main(String[] files) { + try { + System.out.println(files[0]); + files[1] = "hello"; + } + catch (RuntimeException e) { + System.out.println(e); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionTest.java index cc97af92d301..08588cdd6135 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionTest.java @@ -579,6 +579,7 @@ public class DataFlowInspectionTest extends DataFlowInspectionTestCase { public void testGetterOfNullableFieldIsNotNull() { doTest(); } public void testArrayStoreProblems() { doTest(); } + public void testArrayAccessInTry() { doTest(); } public void testNestedScopeComplexity() { doTest(); }