This is an automated email from the ASF dual-hosted git repository.

rmaucher pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit afd0ce93d64f1b369e3f782b83e1e8450e4d5945
Author: opencode <[email protected]>
AuthorDate: Sun Oct 18 14:34:04 2026 +0200

    Correct the EL empty operator for empty primitive arrays
    
    The EL specification defines the empty operator as true when the
    operand evaluates to an empty array. AstAbstractEmpty tested arrays
    with instanceof Object[] and Array.length, which only matches object
    arrays. A one-dimensional primitive array, such as an int[0] returned
    by a bean property, is not an instance of Object[], so empty evaluated
    to false and not empty to true, silently inverting the spec result.
    Other array handling in the EL implementation (the stream resolver and
    ELSupport) already uses the primitive-array-correct reflective checks.
    
    Test array-ness with Class.isArray() and length with
    java.lang.reflect.Array.getLength() instead. The null disjunct
    precedes this test and short-circuits, so no additional null guard is
    required. Multi-dimensional behaviour is unchanged: an empty outer
    array is empty and a non-empty outer array with empty inner arrays is
    not, consistent with the previous treatment of equivalent object
    arrays.
    
    Adds tests to TestAstEmpty and TestAstNotEmpty covering empty and
    filled primitive arrays plus an empty object array as a regression
    guard for the original path. The primitive-array tests fail against
    the previous implementation.
---
 java/org/apache/el/parser/AstAbstractEmpty.java |  3 ++-
 test/org/apache/el/parser/TestAstEmpty.java     | 29 +++++++++++++++++++++++++
 test/org/apache/el/parser/TestAstNotEmpty.java  | 19 ++++++++++++++++
 3 files changed, 50 insertions(+), 1 deletion(-)

diff --git a/java/org/apache/el/parser/AstAbstractEmpty.java 
b/java/org/apache/el/parser/AstAbstractEmpty.java
index a10ee6ea83..73a280307b 100644
--- a/java/org/apache/el/parser/AstAbstractEmpty.java
+++ b/java/org/apache/el/parser/AstAbstractEmpty.java
@@ -17,6 +17,7 @@
 /* Generated By:JJTree: Do not edit this line. AstAbstractEmpty.java */
 package org.apache.el.parser;
 
+import java.lang.reflect.Array;
 import java.util.Collection;
 import java.util.Map;
 
@@ -50,7 +51,7 @@ public abstract class AstAbstractEmpty extends SimpleNode {
     public Object getValue(EvaluationContext ctx) throws ELException {
         Object obj = this.children[0].getValue(ctx);
         if (obj == null || obj instanceof String && ((String) obj).isEmpty() ||
-                obj instanceof Object[] && ((Object[]) obj).length == 0 ||
+                obj.getClass().isArray() && Array.getLength(obj) == 0 ||
                 obj instanceof Collection<?> && ((Collection<?>) 
obj).isEmpty() ||
                 obj instanceof Map<?,?> && ((Map<?,?>) obj).isEmpty()) {
             return RETURN_EMPTY;
diff --git a/test/org/apache/el/parser/TestAstEmpty.java 
b/test/org/apache/el/parser/TestAstEmpty.java
index dd38899754..7e933ee412 100644
--- a/test/org/apache/el/parser/TestAstEmpty.java
+++ b/test/org/apache/el/parser/TestAstEmpty.java
@@ -37,4 +37,33 @@ public class TestAstEmpty {
         Object result = processor.eval("empty ''");
         Assert.assertEquals(Boolean.TRUE, result);
     }
+
+    @Test
+    public void test03() {
+        // An array with no elements is empty. This includes primitive arrays.
+        ELProcessor processor = new ELProcessor();
+        processor.defineBean("arr", new int[0]);
+        Boolean result = processor.eval("empty arr");
+        Assert.assertEquals(Boolean.TRUE, result);
+    }
+
+
+
+    @Test
+    public void test04() {
+        ELProcessor processor = new ELProcessor();
+        processor.defineBean("arr", new int[] { 1, 2 });
+        Boolean result = processor.eval("empty arr");
+        Assert.assertEquals(Boolean.FALSE, result);
+    }
+
+
+
+    @Test
+    public void test05() {
+        ELProcessor processor = new ELProcessor();
+        processor.defineBean("arr", new Object[0]);
+        Boolean result = processor.eval("empty arr");
+        Assert.assertEquals(Boolean.TRUE, result);
+    }
 }
diff --git a/test/org/apache/el/parser/TestAstNotEmpty.java 
b/test/org/apache/el/parser/TestAstNotEmpty.java
index ab6ce3944b..ffc2d4ef37 100644
--- a/test/org/apache/el/parser/TestAstNotEmpty.java
+++ b/test/org/apache/el/parser/TestAstNotEmpty.java
@@ -37,4 +37,23 @@ public class TestAstNotEmpty {
         Object result = processor.eval("not empty ''");
         Assert.assertEquals(Boolean.FALSE, result);
     }
+
+
+    @Test
+    public void test03() {
+        // An array with no elements is empty. This includes primitive arrays.
+        ELProcessor processor = new ELProcessor();
+        processor.defineBean("arr", new int[0]);
+        Boolean result = processor.eval("not empty arr");
+        Assert.assertEquals(Boolean.FALSE, result);
+    }
+
+
+    @Test
+    public void test04() {
+        ELProcessor processor = new ELProcessor();
+        processor.defineBean("arr", new int[] { 1, 2 });
+        Boolean result = processor.eval("not empty arr");
+        Assert.assertEquals(Boolean.TRUE, result);
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to