Copilot commented on code in PR #424:
URL: https://github.com/apache/commons-jexl/pull/424#discussion_r4526718176


##########
src/main/java/org/apache/commons/jexl3/internal/Engine.java:
##########
@@ -545,6 +549,14 @@ protected JexlOptions evalOptions(final ASTJexlScript 
script, final JexlContext
         if (opts != options && 
!scriptFeatures.supportsNamespaceInstantiation()) {
             opts.setNamespaceInstantiation(false);
         }
+        // declared antish names are all solved at parse time: no run-time 
solving
+        final JexlFeatures features = script != null && script.getFeatures() 
!= null ? script.getFeatures() : scriptFeatures;
+        if (features.antishTest() != JexlFeatures.TEST_STR_FALSE && 
opts.isAntish()) {
+            if (opts == options) {
+                opts = opts.copy();
+            }
+            opts.setAntish(false);

Review Comment:
   On an engine without declared names, supplying `x.y` through 
`createScript(features, ...)` makes a backquoted `${x.y}` fail when only 
context key `x.y` is defined, although a direct `x.y` reference works. Embedded 
expressions are parsed with the engine's features but evaluated with the outer 
script's options, which disable runtime antish resolution here. Propagate the 
enclosing script's declarations into interpolation parsing and its cache keys 
so the embedded references are rewritten too.



##########
src/main/java/org/apache/commons/jexl3/internal/Interpreter.java:
##########
@@ -1885,24 +1952,23 @@ protected Object visit(final ASTReference node, final 
Object data) {
                         break main;
                     }
                     final ASTIdentifier afirst = (ASTIdentifier) first;
-                    ant = new StringBuilder(afirst.getName());
+                    ant = new AntishName(afirst.getName());
+                    if (afirst.getSymbol() >= 0) {
+                        // top level is a symbol (a null local variable), 
cannot be an antish var; same as in executeAssign
+                        ptyNode = numChildren > 1 ? node.jjtGetChild(1) : 
objectNode;

Review Comment:
   With strict mode and runtime antish resolution enabled, a null local now 
makes `var x; x?.y` throw instead of returning null; `var x; x.y` also throws 
with `safe(true)`. Conversely, `var x; x.y?.z` incorrectly treats the first 
access as safe because `isSafeLhs()` checks the following sibling. Keep 
`ptyNode` on the null root so its immediate access determines safety.



##########
src/main/java/org/apache/commons/jexl3/parser/JexlParser.java:
##########
@@ -1026,6 +1042,63 @@ protected void jjtreeCloseNodeScope(final JexlNode node) 
{
         featureController.controlNode(node);
     }
 
+    /**
+     * Resolves a declared antish variable in a reference.
+     * <p>When the features declare antish variable names, the longest dotted 
prefix satisfying the
+     * declaration test of a reference rooted on a global variable, like 
<code>x.y</code> in
+     * <code>x.y.z</code>, is rewritten as a single identifier.</p>
+     *
+     * @param reference The reference node
+     * @return the reference (rewritten or not) or the identifier if the whole 
reference is an antish variable
+     * @see JexlFeatures#antishTest(Predicate)
+     */
+    private JexlNode resolveAntish(final ASTReference reference) {
+        final Predicate<String> declared = getFeatures().antishTest();
+        if (declared == JexlFeatures.TEST_STR_FALSE) {
+            return reference;
+        }
+        final JexlNode first = reference.jjtGetChild(0);
+        // a local variable or a namespace can not be the root of an antish 
variable
+        if (!(first instanceof ASTIdentifier)
+            || ((ASTIdentifier) first).getSymbol() >= 0
+            || ((ASTIdentifier) first).getNamespace() != null) {
+            return reference;
+        }
+        final ASTIdentifier root = (ASTIdentifier) first;
+        final int count = reference.jjtGetNumChildren();
+        final StringBuilder name = new StringBuilder(root.getName());
+        String antish = null;
+        int last = 0;
+        for (int c = 1; c < count; ++c) {
+            final JexlNode child = reference.jjtGetChild(c);
+            // only plain identifier access, no safe navigation nor expression
+            if (child.getClass() != ASTIdentifierAccess.class) {
+                break;

Review Comment:
   With `x.y` declared and a callable `JexlScript` stored under that context 
key, `x.y()` is not rewritten because `y()` is an `ASTMethodNode`, so this loop 
stops before testing the declared name. Runtime antish resolution is disabled, 
so the call fails although `(x.y)()` works. Rewrite declared call targets while 
preserving their arguments and safe-navigation boundaries, and add a regression 
test for the direct call.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to