You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2022/08/19 08:54:26 UTC

[tomcat] branch 8.5.x updated: Fix parsing of large numeric literals

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

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


The following commit(s) were added to refs/heads/8.5.x by this push:
     new 05341577d5 Fix parsing of large numeric literals
05341577d5 is described below

commit 05341577d555019ce3ba9658bb2f70009981e861
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Fri Aug 19 09:46:04 2022 +0100

    Fix parsing of large numeric literals
---
 java/org/apache/el/parser/AstFloatingPoint.java    | 14 ++++--
 java/org/apache/el/parser/AstInteger.java          | 15 +++++--
 .../org/apache/el/parser/TestAstFloatingPoint.java | 46 +++++++++++++++++++
 test/org/apache/el/parser/TestAstInteger.java      | 52 ++++++++++++++++++++++
 webapps/docs/changelog.xml                         |  5 +++
 5 files changed, 126 insertions(+), 6 deletions(-)

diff --git a/java/org/apache/el/parser/AstFloatingPoint.java b/java/org/apache/el/parser/AstFloatingPoint.java
index b3a8bc8b85..1e27047e0f 100644
--- a/java/org/apache/el/parser/AstFloatingPoint.java
+++ b/java/org/apache/el/parser/AstFloatingPoint.java
@@ -35,11 +35,19 @@ public final class AstFloatingPoint extends SimpleNode {
     private volatile Number number;
 
     public Number getFloatingPoint() {
+        // The parser should ensure the format of the string to be parsed.
         if (this.number == null) {
             try {
-                this.number = Double.valueOf(this.image);
-            } catch (ArithmeticException e0) {
-                this.number = new BigDecimal(this.image);
+                Double d = Double.valueOf(this.image);
+                if (d.isInfinite() || d.isNaN()) {
+                    this.number = new BigDecimal(this.image);
+                } else {
+                    this.number = d;
+                }
+            } catch (NumberFormatException e) {
+                // Catch NumberFormatException here just in case the parser
+                // provides invalid input.
+                throw new ELException(e);
             }
         }
         return this.number;
diff --git a/java/org/apache/el/parser/AstInteger.java b/java/org/apache/el/parser/AstInteger.java
index 454534b32a..597a13bad0 100644
--- a/java/org/apache/el/parser/AstInteger.java
+++ b/java/org/apache/el/parser/AstInteger.java
@@ -35,11 +35,20 @@ public final class AstInteger extends SimpleNode {
     private volatile Number number;
 
     protected Number getInteger() {
+        // The parser should ensure the format of the string to be parsed
         if (this.number == null) {
             try {
-                this.number = Long.valueOf(this.image);
-            } catch (ArithmeticException e1) {
-                this.number = new BigInteger(this.image);
+                try {
+                    this.number = Long.valueOf(this.image);
+                } catch (NumberFormatException ignore) {
+                    // Too large for Long. Try BigInteger.
+                    this.number = new BigInteger(this.image);
+                }
+            } catch (ArithmeticException | NumberFormatException e) {
+                // Too big for BigInteger.
+                // Catch NumberFormatException as well here just in case the
+                // parser provides invalid input.
+                throw new ELException(e);
             }
         }
         return number;
diff --git a/test/org/apache/el/parser/TestAstFloatingPoint.java b/test/org/apache/el/parser/TestAstFloatingPoint.java
new file mode 100644
index 0000000000..9cccb360a0
--- /dev/null
+++ b/test/org/apache/el/parser/TestAstFloatingPoint.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.el.parser;
+
+import java.math.BigDecimal;
+
+import javax.el.ELProcessor;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import org.apache.el.lang.ELSupport;
+
+public class TestAstFloatingPoint {
+
+    @Test
+    public void testValidDouble() {
+        String value = "1.234e100";
+        ELProcessor processor = new ELProcessor();
+        Number result = (Number) processor.eval(value);
+        Assert.assertTrue(ELSupport.equals(null, Double.valueOf(value), result));
+    }
+
+
+    @Test
+    public void testValidBigDecimal() {
+        String value = "1.234e1000";
+        ELProcessor processor = new ELProcessor();
+        Number result = (Number) processor.eval(value);
+        Assert.assertTrue(ELSupport.equals(null, new BigDecimal(value), result));
+    }
+}
diff --git a/test/org/apache/el/parser/TestAstInteger.java b/test/org/apache/el/parser/TestAstInteger.java
new file mode 100644
index 0000000000..f361ea8f96
--- /dev/null
+++ b/test/org/apache/el/parser/TestAstInteger.java
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.el.parser;
+
+import java.math.BigInteger;
+
+import javax.el.ELProcessor;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import org.apache.el.lang.ELSupport;
+
+public class TestAstInteger {
+
+    @Test
+    public void testValidLong() {
+        doTestValid("1234");
+    }
+
+
+    @Test
+    public void testValidBigInteger() {
+        doTestValid("12345678901234567890");
+    }
+
+
+    private void doTestValid(String value) {
+        ELProcessor processor = new ELProcessor();
+        Number result = (Number) processor.eval(value);
+        Assert.assertTrue(ELSupport.equals(null, new BigInteger(value), result));
+    }
+
+
+    // Note: Testing an invalid integer would require a number larger than
+    //       2^Integer.MAX_VALUE. It is not practical to test with a String of
+    //       digits representing a number that large.
+}
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index b97009f8f3..7abdc17294 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -134,6 +134,11 @@
         Improve handling of stack overflow errors when parsing EL expressions.
         (markt)
       </fix>
+      <fix>
+        Correct parsing of integer and floating point literals in EL expressions
+        so that larger values are correctly parsed to <code>BigInteger</code>
+        and <code>BigDecimal</code> respectively. (markt)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Other">


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org