This is an automated email from the ASF dual-hosted git repository. asf-gitbox-commits pushed a commit to branch misc-hardening__MATH-1691 in repository https://gitbox.apache.org/repos/asf/commons-math.git
commit 25c3ade210808b4fff8c75aa573495d9d3245d32 Author: Gilles Sadowski <[email protected]> AuthorDate: Sun Sep 27 18:41:49 2026 +0200 MATH-1691: Abort early on non finite input. Found by a security scan. --- .../commons/math4/legacy/special/BesselJ.java | 13 +++++++---- .../commons/math4/legacy/special/BesselJTest.java | 27 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/special/BesselJ.java b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/special/BesselJ.java index 6ad328ff4..cd79f86b8 100644 --- a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/special/BesselJ.java +++ b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/special/BesselJ.java @@ -20,6 +20,7 @@ package org.apache.commons.math4.legacy.special; import java.util.Arrays; import org.apache.commons.numbers.gamma.Gamma; import org.apache.commons.math4.legacy.analysis.UnivariateFunction; +import org.apache.commons.math4.legacy.exception.NotFiniteNumberException; import org.apache.commons.math4.legacy.exception.ConvergenceException; import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException; import org.apache.commons.math4.legacy.exception.util.LocalizedFormats; @@ -140,8 +141,7 @@ public class BesselJ * @throws ConvergenceException if the algorithm fails to converge */ @Override - public double value(double x) - throws MathIllegalArgumentException, ConvergenceException { + public double value(double x) { return BesselJ.value(order, x); } @@ -153,9 +153,12 @@ public class BesselJ * @return Value of the Bessel function of the first kind, \(J_{order}(x)\) * @throws MathIllegalArgumentException if {@code x} is too large relative to {@code order} * @throws ConvergenceException if the algorithm fails to converge + * @throws NotFiniteNumberException if {@code order} or {@code x} are not finite. */ - public static double value(double order, double x) - throws MathIllegalArgumentException, ConvergenceException { + public static double value(double order, double x) { + NotFiniteNumberException.check(order); + NotFiniteNumberException.check(x); + final int n = (int) order; final double alpha = order - n; final int nb = n + 1; @@ -164,7 +167,7 @@ public class BesselJ if (res.nVals >= nb) { return res.vals[n]; } else if (res.nVals < 0) { - throw new MathIllegalArgumentException(LocalizedFormats.BESSEL_FUNCTION_BAD_ARGUMENT,order, x); + throw new MathIllegalArgumentException(LocalizedFormats.BESSEL_FUNCTION_BAD_ARGUMENT, order, x); } else if (JdkMath.abs(res.vals[res.nVals - 1]) < 1e-100) { return res.vals[n]; // underflow; return value (will be zero) } diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/special/BesselJTest.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/special/BesselJTest.java index ed00d1f30..a2b3f8835 100644 --- a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/special/BesselJTest.java +++ b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/special/BesselJTest.java @@ -17,6 +17,7 @@ package org.apache.commons.math4.legacy.special; import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException; +import org.apache.commons.math4.legacy.exception.NotFiniteNumberException; import org.junit.Assert; import org.junit.Test; @@ -772,4 +773,30 @@ public class BesselJTest { public void testIAEBadArgument() { BesselJ.value(1, 100000); } + + @Test(expected=NotFiniteNumberException.class) + public void testNotFiniteOrder1() { + BesselJ.value(Double.POSITIVE_INFINITY, 123.456); + } + @Test(expected=NotFiniteNumberException.class) + public void testNotFiniteOrder2() { + BesselJ.value(Double.NEGATIVE_INFINITY, 123.456); + } + @Test(expected=NotFiniteNumberException.class) + public void testNotFiniteOrder3() { + BesselJ.value(Double.NaN, 123.456); + } + + @Test(expected=NotFiniteNumberException.class) + public void testNotFiniteArgument1() { + BesselJ.value(4.56, Double.POSITIVE_INFINITY); + } + @Test(expected=NotFiniteNumberException.class) + public void testNotFiniteArgument2() { + BesselJ.value(4.56, Double.NEGATIVE_INFINITY); + } + @Test(expected=NotFiniteNumberException.class) + public void testNotFiniteArgument3() { + BesselJ.value(4.56, Double.NaN); + } }
