This is an automated email from the ASF dual-hosted git repository. henrib pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-jexl.git
commit 041fcaf0b32e1316c578f971039e1632a01f8f79 Author: Henri Biestro <hbies...@gmail.com> AuthorDate: Sat Jun 8 15:18:31 2019 +0200 JEXL-306: ensure ternary expressions only protect the condition part against variable resolution failure Task #JEXL-306 - Ternary operator ? protects also its branches from resolvance errors --- .../java/org/apache/commons/jexl3/parser/JexlNode.java | 14 ++++++++------ src/test/java/org/apache/commons/jexl3/IfTest.java | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/apache/commons/jexl3/parser/JexlNode.java b/src/main/java/org/apache/commons/jexl3/parser/JexlNode.java index c8ae5e5..092467a 100644 --- a/src/main/java/org/apache/commons/jexl3/parser/JexlNode.java +++ b/src/main/java/org/apache/commons/jexl3/parser/JexlNode.java @@ -55,11 +55,11 @@ public abstract class JexlNode extends SimpleNode { public void jjtSetLastToken(Token t) { // nothing } - + public int getLine() { return this.lc >>> 0xc; } - + public int getColumn() { return this.lc & 0xfff; } @@ -89,11 +89,11 @@ public abstract class JexlNode extends SimpleNode { return info; } } - + /** * Marker interface for cachable function calls. */ - public interface Funcall {} + public interface Funcall {} /** * Clears any cached value of type JexlProperty{G,S}et or JexlMethod. @@ -261,15 +261,17 @@ public abstract class JexlNode extends SimpleNode { public boolean isTernaryProtected() { JexlNode node = this; for (JexlNode walk = node.jjtGetParent(); walk != null; walk = walk.jjtGetParent()) { - if (walk instanceof ASTTernaryNode) { + // protect only the condition part of the ternay + if (walk instanceof ASTTernaryNode && node == walk.jjtGetChild(0)) { return true; } - if (walk instanceof ASTNullpNode) { + if (walk instanceof ASTNullpNode && node == walk.jjtGetChild(0)) { return true; } if (!(walk instanceof ASTReference || walk instanceof ASTArrayAccess)) { break; } + node = walk; } return false; } diff --git a/src/test/java/org/apache/commons/jexl3/IfTest.java b/src/test/java/org/apache/commons/jexl3/IfTest.java index 83f0d2f..377cf82 100644 --- a/src/test/java/org/apache/commons/jexl3/IfTest.java +++ b/src/test/java/org/apache/commons/jexl3/IfTest.java @@ -140,6 +140,7 @@ public class IfTest extends JexlTestCase { o = e.execute(null, 4); Assert.assertEquals(40, o); } + @Test public void testIfElseIfReturnExpression() throws Exception { JexlScript e = JEXL.createScript( @@ -426,4 +427,21 @@ public class IfTest extends JexlTestCase { Assert.assertEquals("Should be 0", 0, o); debuggerCheck(JEXL); } + + + @Test + public void testTernaryFail() throws Exception { + JexlEvalContext jc = new JexlEvalContext(); + JexlExpression e = JEXL.createExpression("false ? bar : quux"); + Object o; + jc.setStrict(true); + jc.setSilent(false); + try { + o = e.evaluate(jc); + Assert.fail("Should have failed"); + } catch (JexlException xjexl) { + // OK + Assert.assertTrue(xjexl.toString().contains("quux")); + } + } }