Dmitri Blinov created JEXL-321:
----------------------------------
Summary: Empty do-while loop is broken
Key: JEXL-321
URL: https://issues.apache.org/jira/browse/JEXL-321
Project: Commons JEXL
Issue Type: Bug
Affects Versions: 3.1
Reporter: Dmitri Blinov
The following test case with AIOOB.
{code:java}
@Test
public void testEmptyBody() throws Exception {
JexlScript e = JEXL.createScript("var i = 0; do ; while((i+=1) < 10);
i");
JexlContext jc = new MapContext();
Object o = e.execute(jc);
Assert.assertEquals(10, o);
} {code}
The suggestion is to change interpreter as follows
{code}
@Override
protected Object visit(ASTDoWhileStatement node, Object data) {
Object result = null;
/* last objectNode is the expression */
Node expressionNode = node.jjtGetChild(node.jjtGetNumChildren()-1);
do {
cancelCheck(node);
if (node.jjtGetNumChildren() > 1) {
try {
// execute statement
result = node.jjtGetChild(0).jjtAccept(this, data);
} catch (JexlException.Break stmtBreak) {
break;
} catch (JexlException.Continue stmtContinue) {
//continue;
}
}
} while (arithmetic.toBoolean(expressionNode.jjtAccept(this, data)));
return result;
}
{code} and Debugger as follows
{code}
@Override
protected Object visit(ASTDoWhileStatement node, Object data) {
int num = node.jjtGetNumChildren();
builder.append("do ");
if (num > 1) {
acceptStatement(node.jjtGetChild(0), data);
} else {
builder.append(" ; ");
}
builder.append(" while (");
accept(node.jjtGetChild(num - 1), data);
builder.append(")");
return data;
}
{code}
--
This message was sent by Atlassian Jira
(v8.3.4#803005)