Copilot commented on code in PR #425:
URL: https://github.com/apache/commons-jexl/pull/425#discussion_r4541588145
##########
src/main/java/org/apache/commons/jexl3/internal/Interpreter.java:
##########
@@ -1721,7 +1721,7 @@ private Object visit(final ASTMethodNode node, final
Object antish, final Object
object = data;
if (object == null) {
// no object, we fail
- return node.isSafeLhs(isSafe())
+ return isSafe() || ((ASTIdentifierAccess)
methodNode).isSafe()
Review Comment:
This introduces an unconditional cast of `methodNode` to
`ASTIdentifierAccess`. In this codebase, the method 'member' can also be
represented by other node types (notably `ASTArrayAccess`), and the previous
logic avoided a cast. This can throw `ClassCastException` at runtime for
safe-call scenarios. Use the existing safe-access check logic (e.g.,
`InterpreterBase.isSafeAccess(...)`) or guard with `instanceof` and handle
`ASTArrayAccess` as well.
##########
src/main/java/org/apache/commons/jexl3/JexlOptions.java:
##########
@@ -535,6 +551,8 @@ public void setNamespaces(final Map<String, Object> ns) {
* <p>If safe, encountering null during a navigation expression -
dereferencing a method or a field through a null
* object or property - will <em>not</em> be considered an error but
evaluated as <em>null</em>. It is recommended
* to use <em>setSafe(false)</em> as an explicit default.</p>
+ * <p>If safe, every access behaves as a safe access, {@code x.y} as
{@code x?.y}; whether this also
+ * applies to what is undefined is determined by {@link
#setSafeUndefined(boolean)}.</p>
Review Comment:
This wording is misleading in light of the documented compatibility
exceptions (notably: with `safe` enabled, an undefined property can still be an
error). Consider rephrasing to clarify that `setSafe(true)` primarily affects
null-dereference behavior, and that handling of undefined
variables/properties/members follows `safeUndefined` (with the stated
exceptions), so readers do not infer `x.y` is fully equivalent to `x?.y` in all
undefined-member cases.
##########
src/main/java/org/apache/commons/jexl3/internal/InterpreterBase.java:
##########
@@ -770,6 +771,82 @@ protected boolean isSilent() {
return options.isSilent();
}
+ /**
+ * Whether safe navigation applies to what is undefined in this
interpreter.
+ *
+ * @return true if safe navigation applies to undefined and null, false if
it only applies to null
+ */
+ protected boolean isSafeUndefined() {
+ return options.isSafeUndefined();
+ }
+
+ /**
+ * Whether safe navigation excuses an undefined property.
+ * <p>The access to the property is explicitly safe, as in x?.y or x?[y],
or the access that follows it is,
+ * as in x.y?.z; the safe option does not excuse an undefined property.</p>
+ *
+ * @param node the node accessing the property
+ * @return true if the property evaluates as null, false if it is an error
+ */
+ protected boolean isSafeUndefined(final JexlNode node) {
+ if (!isSafeUndefined()) {
+ return false;
+ }
+ JexlNode access = node;
+ JexlNode parent = node.jjtGetParent();
+ if (parent instanceof ASTArrayAccess) {
+ // an index, as in x?[y] or x[y]?[z]
+ final ASTArrayAccess indexed = (ASTArrayAccess) parent;
+ final int next = indexOf(indexed, node) + 1;
+ if (indexed.isSafeChild(next - 1)) {
+ return true;
+ }
Review Comment:
`indexOf(indexed, node)` can return `-1` if the passed `node` is not a
direct child of `indexed`, which would make `next - 1` equal `-1` and lead to
an invalid `isSafeChild(-1)` call. Even if current call sites always pass a
child node, this helper is `protected` and easy to reuse incorrectly. Add a
guard for `idx < 0` (return `false`) before computing `next` / calling
`isSafeChild`.
##########
src/test/java/org/apache/commons/jexl3/Issues400Test.java:
##########
@@ -1393,5 +1393,76 @@ void test475() {
assertNull(jexl.createScript("#pragma jexl.options
'-strict'\nfoo").execute(null));
assertThrows(JexlException.Variable.class, () ->
jexl.createScript("foo").execute(null));
}
-}
+ @Test
+ void test476() {
+ final String err = "error";
+ // an expression and its result when (safeUndefined, safe) is (true,
false), (true, true), (false, false), (false, true)
+ final Object[][] cases = {
+ // an undefined variable
+ {"u", err, err, err, err},
+ {"u.y", err, null, err, err},
+ {"u?.y", null, null, err, err},
+ {"u[0]", err, null, err, err},
+ {"u?[0]", null, null, err, err},
+ {"u.m()", err, null, err, err},
+ {"u?.m()", null, null, err, err},
+ {"u == null", err, err, err, err},
+ {"u ?? 1", 1, 1, 1, 1},
+ // an undefined property
+ {"'abc'.nothing", err, err, err, err},
+ {"'abc'?.nothing", null, null, err, err},
+ {"'abc'['nothing']", err, err, err, err},
+ {"'abc'?['nothing']", null, null, err, err},
+ {"'abc'.nothing.y", err, err, err, err},
+ {"'abc'.nothing?.y", null, null, err, err},
+ {"'abc'.nothing ?? 1", 1, 1, 1, 1},
+ // an undefined method
+ {"'abc'.nothing()", err, null, err, err},
+ {"'abc'?.nothing()", null, null, err, err},
+ {"'abc'.nothing().y", err, null, err, err},
+ {"'abc'.nothing()?.y", null, null, err, err},
+ {"'abc'.nothing() ?? 1", 1, 1, 1, 1},
+ // an undefined function
+ {"nothing(1)", err, err, err, err},
+ {"nothing(1).y", err, null, err, err},
+ {"nothing(1)?.y", null, null, err, err},
+ {"nothing(1) ?? 1", 1, 1, 1, 1},
+ // null is not undefined
+ {"nul", null, null, null, null},
+ {"nul.y", err, null, err, null},
+ {"nul?.y", null, null, null, null},
+ {"nul?.y.z", null, null, null, null},
+ {"nul[0]", err, null, err, null},
+ {"nul?[0]", null, null, null, null},
+ {"nul.m()", err, null, err, null},
+ {"nul?.m()", null, null, null, null},
+ {"nul(1)", err, err, err, err},
+ {"nul + 1", err, err, err, err},
+ {"nul.y ?? 1", 1, 1, 1, 1},
+ };
+ final JexlContext ctxt = new MapContext();
+ ctxt.set("nul", null);
+ for (int m = 0; m < 4; ++m) {
+ final boolean undef = (m & 2) == 0;
+ final boolean safe = (m & 1) != 0;
+ final JexlEngine jexl = new
JexlBuilder().strict(true).safe(safe).safeUndefined(undef).create();
+ for (final Object[] c : cases) {
+ final String src = (String) c[0];
+ final Object expected = c[m + 1];
+ final String msg = src + ", safeUndefined: " + undef + ",
safe: " + safe;
+ final JexlScript script = jexl.createScript(src);
+ if (expected == err) {
+ assertThrows(JexlException.class, () ->
script.execute(ctxt), msg);
+ } else {
+ assertEquals(expected, script.execute(ctxt), msg);
Review Comment:
The test uses `expected == err` where `err` is a `String` sentinel. This is
brittle because it relies on reference identity and also risks colliding with
legitimate expression results that might equal the same string. Prefer a
dedicated sentinel object (e.g., `private static final Object ERROR = new
Object();`) and compare by reference to that sentinel.
--
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]