When converting JDT AST to IR, a do-while loop in a case statement will throw NPE (#186)

* Fixed bug for source analysis

Do while in case statement would throw an NPE. Now it doesn't

* Added test case

The test will fail with an NPE if the fix is not applied.
This commit is contained in:
Caius Brindescu 2017-06-07 08:27:23 -07:00 committed by Manu Sridharan
parent 554a6e7ee9
commit 404a17c9cc
3 changed files with 24 additions and 1 deletions

View File

@ -2444,7 +2444,9 @@ public abstract class JDTJava2CAstTranslator<T extends Position> {
WalkContext loopContext = new LoopContext(context, loopLabel, breakTarget, continueTarget);
CAstNode loopBody = visitNode(n.getBody(), loopContext);
return doLoopTranslator.translateDoLoop(loopTest, loopBody, continueNode, breakNode, context);
CAstNode madeNode = doLoopTranslator.translateDoLoop(loopTest, loopBody, continueNode, breakNode, context);
context.pos().setPosition(madeNode, makePosition(n));
return madeNode;
}
/**

View File

@ -0,0 +1,17 @@
package bugfixes;
public class DoWhileInCase {
static int x = 3;
public static void main(String[] args) {
switch(x) {
case 1:
do {
System.out.println("Problem");
x ++;
} while (x < 3);
break;
default:
System.out.println("Default");
}
}
}

View File

@ -630,5 +630,9 @@ public abstract class JavaIRTests extends IRTests {
Assert.assertEquals(2, SlicerTest.countAllocations(slice));
Assert.assertEquals(2, SlicerTest.countPutfields(slice));
}
@Test public void testDoWhileInCase() throws IllegalArgumentException, CancelException, IOException {
runTest(singleTestSrc("bugfixes"), rtJar, simplePkgTestEntryPoint("bugfixes"), emptyList, true);
}
}