From 3543d67b45ba46f8dfe65309e11e890f35b45aa9 Mon Sep 17 00:00:00 2001 From: Takeshi Yamamuro Date: Wed, 28 Apr 2021 10:46:46 +0900 Subject: [PATCH 1/2] Fix the bug that wrongly trims local vars in a stack --- .../compiler/tests/ReportedBugsTest.java | 33 +++++++++++++++++++ .../java/org/codehaus/janino/CodeContext.java | 10 +++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/commons-compiler-tests/src/test/java/org/codehaus/commons/compiler/tests/ReportedBugsTest.java b/commons-compiler-tests/src/test/java/org/codehaus/commons/compiler/tests/ReportedBugsTest.java index b5b856087..3046affed 100644 --- a/commons-compiler-tests/src/test/java/org/codehaus/commons/compiler/tests/ReportedBugsTest.java +++ b/commons-compiler-tests/src/test/java/org/codehaus/commons/compiler/tests/ReportedBugsTest.java @@ -1220,6 +1220,39 @@ class ReportedBugsTest extends CommonsCompilerTestSuite { this.assertScriptCookable("import java.util.Map; Map a;"); } + @Test public void + testPullRequestXX() throws Exception { + this.assertCompilationUnitCookable(( + "" + + "public class JaninoTest {\n" + + " public int func(int v) {\n" + + " long local_var0 = 0;\n" + + " switch (v) {\n" + + " case 0:\n" + + " long local_var1 = 1;\n" + + " local_var0 = local_var1;\n" + + " break;\n" + + "\n" + + " default:\n" + + " long local_var2 = 2;\n" + + " local_var0 = local_var2;\n" + + " break;\n" + + " }\n" + + "\n" + + " long local_var3 = 1;\n" + + " if (v % 4 == 0) {\n" + + " local_var3 += 2;\n" + + " }\n" + + " if ((local_var0 + local_var3) % 2 == 0) {\n" + + " return 0;\n" + + " } else {\n" + + " return 1;\n" + + " }\n" + + " }\n" + + "}\n" + )); + } + public ClassLoader compile(ClassLoader parentClassLoader, CompileUnit... compileUnits) { diff --git a/janino/src/main/java/org/codehaus/janino/CodeContext.java b/janino/src/main/java/org/codehaus/janino/CodeContext.java index 418c99b84..56b0f60f7 100644 --- a/janino/src/main/java/org/codehaus/janino/CodeContext.java +++ b/janino/src/main/java/org/codehaus/janino/CodeContext.java @@ -232,7 +232,15 @@ class LocalScope { if (this.currentLocalScope != null) { StackMap sm = this.currentInserter.getStackMap(); if (sm != null) { - while (sm.locals().length > this.allLocalVars.size()) sm = sm.popLocal(); + int numLocalsInStackMap = 0; + for (VerificationTypeInfo slot : sm.locals()) { + if (slot != StackMapTableAttribute.TOP_VARIABLE_INFO) { + numLocalsInStackMap++; + } + } + while (numLocalsInStackMap-- > this.allLocalVars.size()) { + sm = sm.popLocal(); + } this.currentInserter.setStackMap(sm); } } From 801f9d8689410add6e0b0222cfa9762d2cc542f4 Mon Sep 17 00:00:00 2001 From: Takeshi Yamamuro Date: Wed, 28 Apr 2021 11:20:02 +0900 Subject: [PATCH 2/2] Add the PR number in the test name --- .../org/codehaus/commons/compiler/tests/ReportedBugsTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commons-compiler-tests/src/test/java/org/codehaus/commons/compiler/tests/ReportedBugsTest.java b/commons-compiler-tests/src/test/java/org/codehaus/commons/compiler/tests/ReportedBugsTest.java index 3046affed..c3f751759 100644 --- a/commons-compiler-tests/src/test/java/org/codehaus/commons/compiler/tests/ReportedBugsTest.java +++ b/commons-compiler-tests/src/test/java/org/codehaus/commons/compiler/tests/ReportedBugsTest.java @@ -1221,7 +1221,7 @@ class ReportedBugsTest extends CommonsCompilerTestSuite { } @Test public void - testPullRequestXX() throws Exception { + testPullRequest146() throws Exception { this.assertCompilationUnitCookable(( "" + "public class JaninoTest {\n"