Painless: Fixes a null pointer exception in certain cases of for loop usage (#28506)

The initializer and afterthought were not having their types
appropriately cast which is necessary with expressions which in turn
caused values to be popped off the stack that were null.
This commit is contained in:
Jack Conradson 2018-02-05 11:57:21 -08:00 committed by GitHub
parent 5003ef18ac
commit 5c1d3aa2f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 1 deletions

View File

@ -76,7 +76,7 @@ public final class SFor extends AStatement {
locals = Locals.newLocalScope(locals);
if (initializer != null) {
if (initializer instanceof AStatement) {
if (initializer instanceof SDeclBlock) {
initializer.analyze(locals);
} else if (initializer instanceof AExpression) {
AExpression initializer = (AExpression)this.initializer;
@ -87,6 +87,9 @@ public final class SFor extends AStatement {
if (!initializer.statement) {
throw createError(new IllegalArgumentException("Not a statement."));
}
initializer.expected = initializer.actual;
this.initializer = initializer.cast(locals);
} else {
throw createError(new IllegalStateException("Illegal tree structure."));
}
@ -119,6 +122,9 @@ public final class SFor extends AStatement {
if (!afterthought.statement) {
throw createError(new IllegalArgumentException("Not a statement."));
}
afterthought.expected = afterthought.actual;
afterthought = afterthought.cast(locals);
}
if (block != null) {
@ -197,6 +203,7 @@ public final class SFor extends AStatement {
if (afterthought != null) {
writer.mark(begin);
afterthought.write(writer, globals);
writer.writePop(MethodWriter.getType(afterthought.expected).getSize());
}
if (afterthought != null || !allEscape) {

View File

@ -108,8 +108,12 @@ public class BasicStatementTests extends ScriptTestCase {
}
public void testForStatement() {
assertEquals(6, exec("int x, y; for (x = 0; x < 4; ++x) {y += x;} return y;"));
assertEquals("aaaaaa", exec("String c = \"a\"; for (int x = 0; x < 5; ++x) c += \"a\"; return c;"));
assertEquals(6, exec("double test() { return 0.0; }" +
"int x, y; for (test(); x < 4; test()) {y += x; ++x;} return y;"));
Object value = exec(
" int[][] b = new int[5][5]; \n" +
" for (int x = 0; x < 5; ++x) { \n" +