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 83fe22025 Preserve numeric types for negative hexadecimal minima 
(#1877)
83fe22025 is described below

commit 83fe22025de1df35a22ede1bbe75ccc5267ac779
Author: Amar Ghugare <[email protected]>
AuthorDate: Sun Nov 15 21:43:04 2026 +0530

    Preserve numeric types for negative hexadecimal minima (#1877)
---
 .../org/apache/commons/lang3/math/NumberUtils.java |  7 +++++--
 .../apache/commons/lang3/math/NumberUtilsTest.java | 23 ++++++++++++++++++++++
 2 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/apache/commons/lang3/math/NumberUtils.java 
b/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
index 4f813303b..041d73177 100644
--- a/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
+++ b/src/main/java/org/apache/commons/lang3/math/NumberUtils.java
@@ -389,13 +389,16 @@ public static Number createNumber(final String str) {
             if (isLongCh) {
                 hexDigits--;
             }
-            if (hexDigits > 16 || hexDigits == 16 && firstSigDigit > '7') { // 
too many for Long
+            // Signed types can represent one more negative magnitude than 
positive.
+            final boolean isNegativeMinimum = str.charAt(0) == '-' && 
firstSigDigit == '8' && (hexDigits == 8 || hexDigits == 16)
+                && isAllZeros(str.substring(pfxLen + 1, isLongCh ? length - 1 
: length));
+            if (hexDigits > 16 || hexDigits == 16 && firstSigDigit > '7' && 
!isNegativeMinimum) { // too many for Long
                 return createBigInteger(isLongCh ? str.substring(0, length - 
1) : str);
             }
             if (isLongCh) {
                 return createLong(str.substring(0, str.length() - 1));
             }
-            if (hexDigits > 8 || hexDigits == 8 && firstSigDigit > '7') { // 
too many for an int
+            if (hexDigits > 8 || hexDigits == 8 && firstSigDigit > '7' && 
!isNegativeMinimum) { // too many for an int
                 return createLong(str);
             }
             return createInteger(str);
diff --git a/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java 
b/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
index 2c0726d2f..a0a012750 100644
--- a/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java
@@ -690,6 +690,29 @@ void testCreateNumber() {
         assertEquals(new BigDecimal("0.100000001490116121"), 
NumberUtils.createNumber("0.100000001490116121"));
     }
 
+    @ParameterizedTest
+    @ValueSource(strings = { "-0x80000000", "-0X80000000", "-#80000000", 
"-0x000080000000", "-0X0000000080000000" })
+    void testCreateNumberNegativeHexIntegerMinimum(final String str) {
+        assertEquals(Integer.valueOf(Integer.MIN_VALUE), 
NumberUtils.createNumber(str));
+    }
+
+    @ParameterizedTest
+    @ValueSource(strings = {
+        "-0x8000000000000000", "-0X8000000000000000", "-#8000000000000000", 
"-0x00008000000000000000",
+        "-0x8000000000000000L", "-0X8000000000000000l", "-#8000000000000000L", 
"-0x00008000000000000000l"
+    })
+    void testCreateNumberNegativeHexLongMinimum(final String str) {
+        assertEquals(Long.valueOf(Long.MIN_VALUE), 
NumberUtils.createNumber(str));
+    }
+
+    @Test
+    void testCreateNumberNegativeHexRangeBoundaries() {
+        assertEquals(Integer.valueOf(Integer.MIN_VALUE + 1), 
NumberUtils.createNumber("-0x7FFFFFFF"));
+        assertEquals(Long.valueOf((long) Integer.MIN_VALUE - 1), 
NumberUtils.createNumber("-0x80000001"));
+        assertEquals(Long.valueOf(Integer.MIN_VALUE), 
NumberUtils.createNumber("-0x80000000L"));
+        assertEquals(new BigInteger("-8000000000000001", 16), 
NumberUtils.createNumber("-0x8000000000000001"));
+    }
+
     @ParameterizedTest
     @ValueSource(strings = { "", " ", "\t", "\r\n", "\u2003", "\u2003 \u2003", 
"\u00a0" })
     void testCreateNumberBlank(final String str) {

Reply via email to