You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by he...@apache.org on 2022/05/17 14:59:19 UTC

[commons-jexl] branch master updated: JEXL-369: detect undefined const use;

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


The following commit(s) were added to refs/heads/master by this push:
     new 67d25108 JEXL-369: detect undefined const use;
67d25108 is described below

commit 67d251088dea0af0172928d4d8ab3eac6453a273
Author: henrib <he...@apache.org>
AuthorDate: Tue May 17 16:59:13 2022 +0200

    JEXL-369: detect undefined const use;
---
 .../apache/commons/jexl3/parser/JexlParser.java    | 24 +++++++++++++---------
 .../java/org/apache/commons/jexl3/JXLTTest.java    |  2 +-
 .../java/org/apache/commons/jexl3/LambdaTest.java  | 21 +++++++++++++++++++
 3 files changed, 36 insertions(+), 11 deletions(-)

diff --git a/src/main/java/org/apache/commons/jexl3/parser/JexlParser.java b/src/main/java/org/apache/commons/jexl3/parser/JexlParser.java
index 7f971216..d790ac75 100644
--- a/src/main/java/org/apache/commons/jexl3/parser/JexlParser.java
+++ b/src/main/java/org/apache/commons/jexl3/parser/JexlParser.java
@@ -334,12 +334,10 @@ public abstract class JexlParser extends StringParser {
                         }
                     }
                     if (declared) {
+                        // track if const is defined or not
                         if (unit.isConstant(symbol)) {
                             identifier.setConstant(true);
-                            if (!unit.isDefined(symbol)) {
-                                throw new JexlException.Parsing(info, name + ": variable is not defined").clean();
-                            }
-                            identifier.setDefined(true);
+                            identifier.setDefined(unit.isDefined(symbol));
                         }
                     } else if (info instanceof JexlNode.Info) {
                         declared = isSymbolDeclared((JexlNode.Info) info, symbol);
@@ -569,12 +567,6 @@ public abstract class JexlParser extends StringParser {
      */
     protected abstract Token getToken(int index);
 
-    /**
-     * Overridden in actual parser to access tokens stack.
-     * @return the next token on the stack
-     */
-    protected abstract Token getNextToken();
-
     /**
      * The set of assignment operators as classes.
      */
@@ -644,6 +636,18 @@ public abstract class JexlParser extends StringParser {
                     }
                 }
             }
+        } else {
+            // control that a const is defined before usage
+            int nchildren = node.jjtGetNumChildren();
+            for(int c = 0; c < nchildren; ++c) {
+                JexlNode child = node.jjtGetChild(c);
+                if (child instanceof ASTIdentifier) {
+                    ASTIdentifier var = (ASTIdentifier) child;
+                    if (var.isConstant() && !var.isDefined()) {
+                        throw new JexlException.Parsing(info, var.getName() + ": constant is not defined").clean();
+                    }
+                }
+            }
         }
         // heavy check
         featureController.controlNode(node);
diff --git a/src/test/java/org/apache/commons/jexl3/JXLTTest.java b/src/test/java/org/apache/commons/jexl3/JXLTTest.java
index 6a65ba63..b917d187 100644
--- a/src/test/java/org/apache/commons/jexl3/JXLTTest.java
+++ b/src/test/java/org/apache/commons/jexl3/JXLTTest.java
@@ -267,7 +267,7 @@ public class JXLTTest extends JexlTestCase {
     }
 
     @Test
-    public void testConstant() throws Exception {
+    public void testConstant0() throws Exception {
         final JexlContext none = null;
         final String source = "Hello World!";
         final JxltEngine.Expression expr = JXLT.createExpression(source);
diff --git a/src/test/java/org/apache/commons/jexl3/LambdaTest.java b/src/test/java/org/apache/commons/jexl3/LambdaTest.java
index de7dab86..70d823f0 100644
--- a/src/test/java/org/apache/commons/jexl3/LambdaTest.java
+++ b/src/test/java/org/apache/commons/jexl3/LambdaTest.java
@@ -461,6 +461,27 @@ public class LambdaTest extends JexlTestCase {
         }
     }
 
+    @Test
+    public void testConst2() {
+        final JexlFeatures f = new JexlFeatures();
+        final JexlEngine jexl = new JexlBuilder().strict(true).create();
+            final JexlScript script = jexl.createScript( "const x;  x = 1");
+            Object result = script.execute(null);
+            Assert.assertEquals(1, result);
+    }
+
+    @Test
+    public void testConst3() {
+        final JexlFeatures f = new JexlFeatures();
+        final JexlEngine jexl = new JexlBuilder().strict(true).create();
+        try {
+            final JexlScript script = jexl.createScript( "const x;  x = 1; x = 2;");
+            Assert.fail("should fail, x is not defined");
+        } catch(JexlException.Parsing xparse) {
+            Assert.assertTrue(xparse.getMessage().contains("x"));
+        }
+    }
+
     @Test public void testNamedFuncIsConst() {
         String src = "function foo(x) { x + x }; var foo ='nonononon'";
         JexlEngine jexl = createEngine();