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

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


The following commit(s) were added to refs/heads/master by this push:
     new 864dd6ea8 NaN bypass in primitive double range validators. (#1640)
864dd6ea8 is described below

commit 864dd6ea887fd394f15e3b55203d6f52f717373b
Author: Gary Gregory <[email protected]>
AuthorDate: Thu May 7 14:19:27 2026 -0400

    NaN bypass in primitive double range validators. (#1640)
    
    * NaN bypass in primitive double range validators.
---
 .../java/org/apache/commons/lang3/Validate.java    |  8 +--
 .../apache/commons/lang3/ValidateDoublesTest.java  | 80 ++++++++++++++++++++++
 2 files changed, 84 insertions(+), 4 deletions(-)

diff --git a/src/main/java/org/apache/commons/lang3/Validate.java 
b/src/main/java/org/apache/commons/lang3/Validate.java
index b35a9daf5..37ba0af82 100644
--- a/src/main/java/org/apache/commons/lang3/Validate.java
+++ b/src/main/java/org/apache/commons/lang3/Validate.java
@@ -93,7 +93,7 @@ public class Validate {
     @SuppressWarnings("boxing")
     public static void exclusiveBetween(final double start, final double end, 
final double value) {
         // TODO when breaking BC, consider returning value
-        if (value <= start || value >= end) {
+        if (value <= start || value >= end || Double.isNaN(value)) {
             throw new 
IllegalArgumentException(String.format(DEFAULT_EXCLUSIVE_BETWEEN_EX_MESSAGE, 
value, start, end));
         }
     }
@@ -114,7 +114,7 @@ public static void exclusiveBetween(final double start, 
final double end, final
      */
     public static void exclusiveBetween(final double start, final double end, 
final double value, final String message) {
         // TODO when breaking BC, consider returning value
-        if (value <= start || value >= end) {
+        if (value <= start || value >= end || Double.isNaN(value)) {
             throw new IllegalArgumentException(message);
         }
     }
@@ -270,7 +270,7 @@ private static String getMessage(final String message, 
final Object... values) {
     @SuppressWarnings("boxing")
     public static void inclusiveBetween(final double start, final double end, 
final double value) {
         // TODO when breaking BC, consider returning value
-        if (value < start || value > end) {
+        if (value < start || value > end || Double.isNaN(value)) {
             throw new 
IllegalArgumentException(String.format(DEFAULT_INCLUSIVE_BETWEEN_EX_MESSAGE, 
value, start, end));
         }
     }
@@ -291,7 +291,7 @@ public static void inclusiveBetween(final double start, 
final double end, final
      */
     public static void inclusiveBetween(final double start, final double end, 
final double value, final String message) {
         // TODO when breaking BC, consider returning value
-        if (value < start || value > end) {
+        if (value < start || value > end || Double.isNaN(value)) {
             throw new IllegalArgumentException(message);
         }
     }
diff --git a/src/test/java/org/apache/commons/lang3/ValidateDoublesTest.java 
b/src/test/java/org/apache/commons/lang3/ValidateDoublesTest.java
new file mode 100644
index 000000000..57033e5cd
--- /dev/null
+++ b/src/test/java/org/apache/commons/lang3/ValidateDoublesTest.java
@@ -0,0 +1,80 @@
+/*
+ * 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
+ *
+ *      https://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.commons.lang3;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.junit.jupiter.api.Test;
+
+/**
+ * NaN bypass in primitive double range validators.
+ * <p>
+ * Validate.inclusiveBetween(double,double,double) and 
Validate.exclusiveBetween(double,double,double) use ordered comparisons (&lt; > 
&lt;= >=). All ordered
+ * comparisons involving NaN return false, so Double.NaN passes any range 
check silently. A caller expecting NaN to be rejected (for example, for quota or
+ * monetary validation) receives no exception.
+ * </p>
+ * <p>
+ * Pre-patch: tests below FAIL (no exception thrown). Post-patch: tests below 
PASS (IllegalArgumentException thrown for NaN).
+ * </p>
+ */
+class ValidateDoublesTest extends AbstractLangTest {
+
+    @Test
+    void exclusiveBetweenRejectsNaN() {
+        assertThrows(IllegalArgumentException.class, () -> 
Validate.exclusiveBetween(0.0, 10.0, Double.NaN),
+                "NaN should be rejected by exclusiveBetween but is silently 
accepted");
+    }
+
+    @Test
+    void exclusiveBetweenWithMessageRejectsNaN() {
+        assertThrows(IllegalArgumentException.class, () -> 
Validate.exclusiveBetween(0.0, 10.0, Double.NaN, "value must be in (0,10)"),
+                "NaN should be rejected by exclusiveBetween(with message)");
+    }
+
+    @Test
+    void inclusiveBetweenRejectsNaN() {
+        assertThrows(IllegalArgumentException.class, () -> 
Validate.inclusiveBetween(0.0, 10.0, Double.NaN),
+                "NaN should be rejected by inclusiveBetween but is silently 
accepted");
+    }
+
+    @Test
+    void inclusiveBetweenRejectsNegativeInfinity() {
+        assertThrows(IllegalArgumentException.class, () -> 
Validate.inclusiveBetween(0.0, 10.0, Double.NEGATIVE_INFINITY),
+                "Positive infinity beyond range end should be rejected");
+    }
+
+    @Test
+    void inclusiveBetweenRejectsPositiveInfinity() {
+        assertThrows(IllegalArgumentException.class, () -> 
Validate.inclusiveBetween(0.0, 10.0, Double.POSITIVE_INFINITY),
+                "Positive infinity beyond range end should be rejected");
+    }
+
+    @Test
+    void inclusiveBetweenWithMessageRejectsNaN() {
+        assertThrows(IllegalArgumentException.class, () -> 
Validate.inclusiveBetween(0.0, 10.0, Double.NaN, "value must be in [0,10]"),
+                "NaN should be rejected by inclusiveBetween(with message)");
+    }
+
+    @Test
+    void validFiniteValuesStillAccepted() {
+        Validate.inclusiveBetween(0.0, 10.0, 5.0);
+        Validate.inclusiveBetween(Double.NEGATIVE_INFINITY, 
Double.POSITIVE_INFINITY, 0.0);
+        Validate.exclusiveBetween(0.0, 10.0, 5.0);
+        Validate.exclusiveBetween(Double.NEGATIVE_INFINITY, 
Double.POSITIVE_INFINITY, 0.0);
+    }
+}

Reply via email to