This is an automated email from the ASF dual-hosted git repository.
markt 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 00c6bc21a0 Fix parsing of large numeric literals
00c6bc21a0 is described below
commit 00c6bc21a05a387114d4f26002d251f5be9dab0b
Author: Mark Thomas <[email protected]>
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 d60113322f..7715edcf7a 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 ca869d22e0..f30c131993 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..a7faf34889
--- /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 jakarta.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 = 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 = 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..f16d3edf7d
--- /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 jakarta.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 = 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 84362426a0..ae54f10053 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -180,6 +180,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: [email protected]
For additional commands, e-mail: [email protected]