Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #119: Guarantee executing popOperand() in popUninitializedVariableOperand() via moving popOperand() out of "assert" #120

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion janino/src/main/java/org/codehaus/janino/CodeContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -1364,7 +1364,8 @@ interface FixUp {
*/
public void
popUninitializedVariableOperand() {
assert this.popOperand() instanceof StackMapTableAttribute.UninitializedVariableInfo;
VerificationTypeInfo result = this.popOperand();
assert result instanceof StackMapTableAttribute.UninitializedVariableInfo;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@

import org.codehaus.commons.compiler.CompileException;
import org.codehaus.commons.compiler.IClassBodyEvaluator;
import org.codehaus.commons.compiler.IScriptEvaluator;
import org.codehaus.commons.compiler.util.reflect.ByteArrayClassLoader;
import org.codehaus.commons.compiler.util.resource.MapResourceCreator;
import org.codehaus.commons.compiler.util.resource.MapResourceFinder;
import org.codehaus.commons.compiler.util.resource.Resource;
import org.codehaus.commons.nullanalysis.Nullable;
import org.codehaus.janino.ClassLoaderIClassLoader;
import org.codehaus.janino.CodeContext;
import org.codehaus.janino.ExpressionEvaluator;
import org.codehaus.janino.IClassLoader;
import org.codehaus.janino.Java;
Expand All @@ -53,6 +55,7 @@
import org.codehaus.janino.Java.Rvalue;
import org.codehaus.janino.Parser;
import org.codehaus.janino.Scanner;
import org.codehaus.janino.ScriptEvaluator;
import org.codehaus.janino.SimpleCompiler;
import org.codehaus.janino.UnitCompiler;
import org.codehaus.janino.util.ClassFile;
Expand Down Expand Up @@ -343,6 +346,44 @@ class GithubIssuesTest {
return sb.toString();
}

@Test public void
testIssue119() throws Exception {
try {
// This only happens when assert is disabled, so we disable it temporary.
CodeContext.class.getClassLoader().setClassAssertionStatus(CodeContext.class.getName(), false);

IScriptEvaluator eval = new ScriptEvaluator();
eval.setReturnType(Object.class);
eval.cook(
""
+ "class A {\n"
+ " private int val1;\n"
+ " private int val2;\n"
+ " public A(int v1, int v2) {\n"
+ " val1 = v1;\n"
+ " val2 = v2;\n"
+ " }\n"
+ " public int getValue1() {\n"
+ " return val1;\n"
+ " }\n"
+ " public int getValue2() {\n"
+ " return val2;\n"
+ " }\n"
+ "}\n"
+ "Integer a = 1;"
+ "Object[] b = new Object[]{\n"
// Technically stack map will be broken while calling constructor, but to force checking
// stack map and make test fail, we leverage ternary operator.
+ "a == null ? null : new A(3, 4).getValue1()};\n"
+ "return b;"
);
Object[] ret = (Object[]) eval.evaluate(new Object[]{});
Assert.assertEquals(3, ret[0]);
} finally {
CodeContext.class.getClassLoader().setClassAssertionStatus(CodeContext.class.getName(), false);
}
}

public ClassLoader
compile(ClassLoader parentClassLoader, CompileUnit... compileUnits) {

Expand Down