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

ChristopherSchultz pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/main by this push:
     new a59c714933 Wrap Instant conversion failures in ELException
a59c714933 is described below

commit a59c7149338f7e7c1269a805d7a03532e1cf114e
Author: lihongyi <[email protected]>
AuthorDate: Thu Aug 13 09:31:54 2026 +0800

    Wrap Instant conversion failures in ELException
    
    Coercion to java.time.Instant could leak DateTimeException
    (DateTimeParseException or UnsupportedTemporalTypeException) for invalid
    strings or TemporalAccessors without timezone information. This is
    inconsistent with coercion to numbers, which wraps NumberFormatException
    in ELException as required by the EL specification.
    
    Wrap the failures from Instant.from() and Instant.parse() in ELException.
---
 java/org/apache/el/lang/ELSupport.java     | 19 +++++++++++++++++--
 test/org/apache/el/lang/TestELSupport.java | 18 ++++++++++++++++++
 2 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/java/org/apache/el/lang/ELSupport.java 
b/java/org/apache/el/lang/ELSupport.java
index 1e36382082..48ee4952bb 100644
--- a/java/org/apache/el/lang/ELSupport.java
+++ b/java/org/apache/el/lang/ELSupport.java
@@ -25,6 +25,7 @@ import java.lang.reflect.Proxy;
 import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.time.Clock;
+import java.time.DateTimeException;
 import java.time.Instant;
 import java.time.temporal.TemporalAccessor;
 import java.util.Collections;
@@ -551,10 +552,24 @@ public class ELSupport {
 
         return switch (obj) {
             case null -> null;
-            case TemporalAccessor t -> Instant.from(t);
+            case TemporalAccessor t -> {
+                try {
+                    yield Instant.from(t);
+                } catch (DateTimeException e) {
+                    throw new ELException(
+                            MessageFactory.get("error.convert", obj, 
obj.getClass().getName(), Instant.class), e);
+                }
+            }
             case Clock c -> c.instant();
             case Date d -> d.toInstant();
-            case String s -> Instant.parse(s);
+            case String s -> {
+                try {
+                    yield Instant.parse(s);
+                } catch (DateTimeException e) {
+                    throw new ELException(
+                            MessageFactory.get("error.convert", obj, 
obj.getClass().getName(), Instant.class), e);
+                }
+            }
             default -> {
                 throw new ELException(
                         MessageFactory.get("error.convert", obj, 
obj.getClass().getName(), Instant.class));
diff --git a/test/org/apache/el/lang/TestELSupport.java 
b/test/org/apache/el/lang/TestELSupport.java
index 87d4f4e224..11ff702552 100644
--- a/test/org/apache/el/lang/TestELSupport.java
+++ b/test/org/apache/el/lang/TestELSupport.java
@@ -19,6 +19,8 @@ package org.apache.el.lang;
 import java.beans.PropertyEditorManager;
 import java.math.BigDecimal;
 import java.math.BigInteger;
+import java.time.Instant;
+import java.time.LocalDate;
 import java.util.Map;
 import java.util.function.BiPredicate;
 import java.util.function.Predicate;
@@ -238,6 +240,22 @@ public class TestELSupport {
         Assert.assertNull(result);
     }
 
+    @Test
+    public void testCoerceToInstant01() {
+        Object result = ELSupport.coerceToType(null, "2024-01-01T00:00:00Z", 
Instant.class);
+        Assert.assertEquals(Instant.parse("2024-01-01T00:00:00Z"), result);
+    }
+
+    @Test(expected = ELException.class)
+    public void testCoerceToInstant02() {
+        ELSupport.coerceToType(null, "not-a-date", Instant.class);
+    }
+
+    @Test(expected = ELException.class)
+    public void testCoerceToInstant03() {
+        ELSupport.coerceToType(null, LocalDate.of(2024, 1, 1), Instant.class);
+    }
+
     @Test
     public void testCoerceToNumber01() {
         Object result = ELSupport.coerceToNumber(null, null, Integer.class);


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

Reply via email to