This is an automated email from the ASF dual-hosted git repository. rmaucher pushed a commit to branch 10.1.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit 3022bb57b186c8f0b715f5469620364af90b2534 Author: opencode <[email protected]> AuthorDate: Sun Oct 18 15:03:23 2026 +0200 Coerce strings correctly for the EL unary minus operator The String case of AstNegative parsed its operand by calling Long.parseLong() and Double.parseDouble() directly, bypassing the shared numeric coercion. Two consequences. An empty string operand threw an uncaught NumberFormatException. Every other numeric conversion path in the EL implementation coerces the empty string to zero, including the binary operators, so a common pattern such as a unary minus applied to an empty request parameter reported a raw NumberFormatException to the container instead of evaluating to zero. The null case already returned zero; the empty string now matches it. Any string that cannot be parsed, e.g. text or an integer literal too large for a long, leaked the raw unchecked NumberFormatException. The Jakarta EL API requires conversion failures to surface as an ELException, as ELSupport.coerceToNumber() does for the same parse failures. The parse calls are now wrapped, producing an ELException with the standard conversion message and the NumberFormatException as the cause. The result types and values for all operands that previously evaluated successfully are unchanged. Adds a test class for the node covering the null, empty, valid and invalid string operands, including the exception type and cause. The four failure-mode tests throw the raw NumberFormatException against the previous implementation. --- java/org/apache/el/parser/AstNegative.java | 15 +++- test/org/apache/el/parser/TestAstNegative.java | 111 +++++++++++++++++++++++++ 2 files changed, 123 insertions(+), 3 deletions(-) diff --git a/java/org/apache/el/parser/AstNegative.java b/java/org/apache/el/parser/AstNegative.java index 2eb41a20c5..877eed6528 100644 --- a/java/org/apache/el/parser/AstNegative.java +++ b/java/org/apache/el/parser/AstNegative.java @@ -24,6 +24,7 @@ import jakarta.el.ELException; import org.apache.el.lang.ELSupport; import org.apache.el.lang.EvaluationContext; +import org.apache.el.util.MessageFactory; public final class AstNegative extends SimpleNode { @@ -52,10 +53,18 @@ public final class AstNegative extends SimpleNode { return ((BigInteger) obj).negate(); } if (obj instanceof String) { - if (ELSupport.isStringFloat((String) obj)) { - return Double.valueOf(-Double.parseDouble((String) obj)); + String s = (String) obj; + if (s.isEmpty()) { + return Long.valueOf(0); + } + try { + if (ELSupport.isStringFloat(s)) { + return Double.valueOf(-Double.parseDouble(s)); + } + return Long.valueOf(-Long.parseLong(s)); + } catch (NumberFormatException nfe) { + throw new ELException(MessageFactory.get("error.convert", s, String.class, Number.class), nfe); } - return Long.valueOf(-Long.parseLong((String) obj)); } if (obj instanceof Long) { return Long.valueOf(-((Long) obj).longValue()); diff --git a/test/org/apache/el/parser/TestAstNegative.java b/test/org/apache/el/parser/TestAstNegative.java new file mode 100644 index 0000000000..311ff29ff6 --- /dev/null +++ b/test/org/apache/el/parser/TestAstNegative.java @@ -0,0 +1,111 @@ +/* + * 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 jakarta.el.ELException; +import jakarta.el.ELProcessor; + +import org.junit.Assert; +import org.junit.Test; + +public class TestAstNegative { + + @Test + public void testNull() { + ELProcessor processor = new ELProcessor(); + Assert.assertEquals(Long.valueOf(0), processor.eval("-null")); + } + + + @Test + public void testEmptyString() { + // An empty string coerces to zero. This is consistent with the + // coercion used by the binary operators, e.g. 5 - '' is 5. + ELProcessor processor = new ELProcessor(); + Object result = processor.eval("-''"); + Assert.assertEquals(Long.class, result.getClass()); + Assert.assertEquals(Long.valueOf(0), result); + } + + + @Test + public void testIntegerString() { + ELProcessor processor = new ELProcessor(); + Assert.assertEquals(Long.valueOf(-12), processor.eval("-'12'")); + } + + + @Test + public void testFloatingPointString() { + ELProcessor processor = new ELProcessor(); + Object result = processor.eval("-'1.5'"); + Assert.assertEquals(Double.class, result.getClass()); + Assert.assertEquals(Double.valueOf(-1.5), result); + } + + + @Test + public void testNonNumericString() { + // A string that cannot be parsed as a number must produce an + // ELException, not the raw NumberFormatException from parsing + ELProcessor processor = new ELProcessor(); + try { + processor.eval("-'abc'"); + Assert.fail("An ELException was expected"); + } catch (ELException e) { + // Expected + Assert.assertTrue("Unexpected cause: " + e.getCause(), + e.getCause() instanceof NumberFormatException); + } + } + + + @Test + public void testInvalidFloatingPointString() { + ELProcessor processor = new ELProcessor(); + try { + processor.eval("-'1.5.5'"); + Assert.fail("An ELException was expected"); + } catch (ELException e) { + // Expected + } + } + + + @Test + public void testOverflowString() { + // A string that is too large for a long cannot be negated as a long. + // The unary minus of a string is specified in terms of + // Long.parseLong()/Double.parseDouble(), so this is a conversion + // failure rather than a change of result type. + ELProcessor processor = new ELProcessor(); + try { + processor.eval("-'99999999999999999999999999'"); + Assert.fail("An ELException was expected"); + } catch (ELException e) { + // Expected + } + } + + + @Test + public void testNumericLiteral() { + ELProcessor processor = new ELProcessor(); + Assert.assertEquals(Long.valueOf(-12), processor.eval("-12")); + Assert.assertEquals(Double.valueOf(-1.5), processor.eval("-1.5")); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
