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

asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git


The following commit(s) were added to refs/heads/master by this push:
     new 0d098352 Do not throw exceptions from public BrentSolver constructor
0d098352 is described below

commit 0d098352ee4eb60137bedaefa7babeab30a2e8c6
Author: Alex Herbert <[email protected]>
AuthorDate: Thu Aug 20 20:14:31 2026 +0100

    Do not throw exceptions from public BrentSolver constructor
    
    Use a private constructor after validation of arguments. Addresses
    Spotbugs CT_CONSTRUCTOR_THROW.
---
 .../commons/numbers/rootfinder/BrentSolver.java    | 27 ++++++++++++++++++----
 1 file changed, 23 insertions(+), 4 deletions(-)

diff --git 
a/commons-numbers-rootfinder/src/main/java/org/apache/commons/numbers/rootfinder/BrentSolver.java
 
b/commons-numbers-rootfinder/src/main/java/org/apache/commons/numbers/rootfinder/BrentSolver.java
index cf0fd552..c9c7ffa4 100644
--- 
a/commons-numbers-rootfinder/src/main/java/org/apache/commons/numbers/rootfinder/BrentSolver.java
+++ 
b/commons-numbers-rootfinder/src/main/java/org/apache/commons/numbers/rootfinder/BrentSolver.java
@@ -60,9 +60,26 @@ public class BrentSolver {
     public BrentSolver(double relativeAccuracy,
                        double absoluteAccuracy,
                        double functionValueAccuracy) {
-        checkAccuracy("relative", relativeAccuracy);
-        checkAccuracy("absolute", absoluteAccuracy);
-        checkAccuracy("function value", functionValueAccuracy);
+        this(checkAccuracy("relative", relativeAccuracy),
+             checkAccuracy("absolute", absoluteAccuracy),
+             checkAccuracy("function value", functionValueAccuracy),
+             false);
+    }
+
+    /**
+     * Private constructor which does not throw exceptions.
+     * This exists to raise an exception before invocation of the private 
constructor;
+     * this mitigates Finalizer attacks (see SpotBugs CT_CONSTRUCTOR_THROW).
+     *
+     * @param relativeAccuracy Relative accuracy.
+     * @param absoluteAccuracy Absolute accuracy.
+     * @param functionValueAccuracy Function value accuracy.
+     * @param ignored Ignored flag.
+     */
+    private BrentSolver(double relativeAccuracy,
+                        double absoluteAccuracy,
+                        double functionValueAccuracy,
+                        boolean ignored) {
         this.relativeAccuracy = relativeAccuracy;
         this.absoluteAccuracy = absoluteAccuracy;
         this.functionValueAccuracy = functionValueAccuracy;
@@ -289,13 +306,15 @@ public class BrentSolver {
      *
      * @param name Name of the accuracy.
      * @param accuracy Accuracy.
+     * @return accuracy
      * @throws IllegalArgumentException if {@code accuracy} is NaN, infinite
      * or negative.
      */
-    private static void checkAccuracy(String name, double accuracy) {
+    private static double checkAccuracy(String name, double accuracy) {
         if (!(accuracy >= 0 && accuracy <= Double.MAX_VALUE)) {
             throw new SolverException(SolverException.INVALID_ACCURACY, name, 
accuracy);
         }
+        return accuracy;
     }
 
     /**

Reply via email to