You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "alhudz (via GitHub)" <gi...@apache.org> on 2026/06/17 07:33:16 UTC

[PR] strip L suffix for oversized hex literals in createNumber [commons-lang]

alhudz opened a new pull request, #1712:
URL: https://github.com/apache/commons-lang/pull/1712

   Repro: `NumberUtils.createNumber("0xFFFFFFFFFFFFFFFFL")` throws `NumberFormatException: For input string: "FFFFFFL"`.
   Expected: a `BigInteger` of `0xFFFFFFFFFFFFFFFF`, the same result as `createNumber("0xFFFFFFFFFFFFFFFF")` (no suffix) and the decimal path `createNumber("9223372036854775808L")`.
   Cause: the hex branch that is too large for a `Long` calls `createBigInteger(str)` with the `L`/`l` type suffix still attached. `createBigInteger` strips only the radix prefix and then runs `new BigInteger(substring, 16)`, so the trailing `L` reaches the hex parser and is rejected. The decimal branch already drops the suffix before its `BigInteger` fallback, which is why only the hex form throws.
   Fix: drop the trailing type char before the `createBigInteger` fallback when the literal carries one, so an over-long hex literal with a suffix parses the same as the unsuffixed form.
   
   Covers the `0x`/`0X`/`#` prefixes, signed and unsigned. Regression assertions added to `NumberUtilsTest.testCreateNumber`; they fail with `NumberFormatException` before the change and pass after.
   
   - [x] Read the [contribution guidelines](CONTRIBUTING.md) for this project.
   - [ ] Read the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) if you use Artificial Intelligence (AI).
   - [ ] I used AI to create any part of, or all of, this pull request. Which AI tool was used to create this pull request, and to what extent did it contribute?
   - [ ] Run a successful build using the default [Maven](https://maven.apache.org/) goal with `mvn`; that's `mvn` on the command line by itself.
   - [x] Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied. This may not always be possible, but it is a best practice.
   - [x] Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
   - [x] Each commit in the pull request should have a meaningful subject line and body. Note that a maintainer may squash commits during the merge process.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] strip L suffix for oversized hex literals in createNumber [commons-lang]

Posted by "sarankumarbaskar (via GitHub)" <gi...@apache.org>.
sarankumarbaskar commented on code in PR #1712:
URL: https://github.com/apache/commons-lang/pull/1712#discussion_r3428535267


##########
src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java:
##########
@@ -661,6 +661,13 @@ void testCreateNumber() {
         assertEquals(Integer.decode("+0xF"), NumberUtils.createNumber("+0xF"), "createNumber(String) LANG-1645a failed");
         assertEquals(Long.decode("+0xFFFFFFFF"), NumberUtils.createNumber("+0xFFFFFFFF"), "createNumber(String) LANG-1645b failed");
         assertEquals(new BigInteger("+FFFFFFFFFFFFFFFF", 16), NumberUtils.createNumber("+0xFFFFFFFFFFFFFFFF"), "createNumber(String) LANG-1645c failed");
+        // A hex literal too large for a Long but carrying an explicit 'L'/'l' type suffix must drop the suffix
+        // before falling back to BigInteger, matching the decimal path (e.g. "12345678901234567890L").
+        assertEquals(new BigInteger("FFFFFFFFFFFFFFFF", 16), NumberUtils.createNumber("0xFFFFFFFFFFFFFFFFL"));
+        assertEquals(new BigInteger("FFFFFFFFFFFFFFFF", 16), NumberUtils.createNumber("0xFFFFFFFFFFFFFFFFl"));
+        assertEquals(new BigInteger("8000000000000000", 16), NumberUtils.createNumber("0x8000000000000000L"));
+        assertEquals(new BigInteger("-FFFFFFFFFFFFFFFF", 16), NumberUtils.createNumber("-0xFFFFFFFFFFFFFFFFL"));
+        assertEquals(new BigInteger("FFFFFFFFFFFFFFFF", 16), NumberUtils.createNumber("#FFFFFFFFFFFFFFFFL"));
         // Map to a BigDecimal, not a Float.

Review Comment:
   This looks consistent with the decimal suffix path, and the added tests fail without the production change. One small suggestion: since the description mentions signed forms and nearby LANG-1645 tests cover +0x..., it would give wider coverage to add a +0xFFFFFFFFFFFFFFFFL assertion as well.
   
   ```java
   assertEquals(new BigInteger("+FFFFFFFFFFFFFFFF", 16),
           NumberUtils.createNumber("+0xFFFFFFFFFFFFFFFFL"));



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] strip L suffix for oversized hex literals in createNumber [commons-lang]

Posted by "garydgregory (via GitHub)" <gi...@apache.org>.
garydgregory merged PR #1712:
URL: https://github.com/apache/commons-lang/pull/1712


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Strip L suffix for oversized hex literals in createNumber [commons-lang]

Posted by "alhudz (via GitHub)" <gi...@apache.org>.
alhudz commented on code in PR #1712:
URL: https://github.com/apache/commons-lang/pull/1712#discussion_r3434790791


##########
src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java:
##########
@@ -661,6 +661,13 @@ void testCreateNumber() {
         assertEquals(Integer.decode("+0xF"), NumberUtils.createNumber("+0xF"), "createNumber(String) LANG-1645a failed");
         assertEquals(Long.decode("+0xFFFFFFFF"), NumberUtils.createNumber("+0xFFFFFFFF"), "createNumber(String) LANG-1645b failed");
         assertEquals(new BigInteger("+FFFFFFFFFFFFFFFF", 16), NumberUtils.createNumber("+0xFFFFFFFFFFFFFFFF"), "createNumber(String) LANG-1645c failed");
+        // A hex literal too large for a Long but carrying an explicit 'L'/'l' type suffix must drop the suffix
+        // before falling back to BigInteger, matching the decimal path (e.g. "12345678901234567890L").
+        assertEquals(new BigInteger("FFFFFFFFFFFFFFFF", 16), NumberUtils.createNumber("0xFFFFFFFFFFFFFFFFL"));
+        assertEquals(new BigInteger("FFFFFFFFFFFFFFFF", 16), NumberUtils.createNumber("0xFFFFFFFFFFFFFFFFl"));
+        assertEquals(new BigInteger("8000000000000000", 16), NumberUtils.createNumber("0x8000000000000000L"));
+        assertEquals(new BigInteger("-FFFFFFFFFFFFFFFF", 16), NumberUtils.createNumber("-0xFFFFFFFFFFFFFFFFL"));
+        assertEquals(new BigInteger("FFFFFFFFFFFFFFFF", 16), NumberUtils.createNumber("#FFFFFFFFFFFFFFFFL"));
         // Map to a BigDecimal, not a Float.

Review Comment:
   Fair point. The `+` form runs through the same suffix-strip: the leading sign stays on `str` and only the trailing `L` is dropped, so `+0xFFFFFFFFFFFFFFFFL` resolves to `new BigInteger("+FFFFFFFFFFFFFFFF", 16)` exactly like the `-0x...L` case that's already asserted. It merged before I could fold the `+` assertion in, but happy to add it as a small follow-up if the explicit case is wanted.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Strip L suffix for oversized hex literals in createNumber [commons-lang]

Posted by "garydgregory (via GitHub)" <gi...@apache.org>.
garydgregory commented on PR #1712:
URL: https://github.com/apache/commons-lang/pull/1712#issuecomment-4734237814

   FYI I tested that the each new BI assertion fails without the main changes, merged 🚀  TY @alhudz 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Strip L suffix for oversized hex literals in createNumber [commons-lang]

Posted by "sarankumarbaskar (via GitHub)" <gi...@apache.org>.
sarankumarbaskar commented on code in PR #1712:
URL: https://github.com/apache/commons-lang/pull/1712#discussion_r3433273328


##########
src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java:
##########
@@ -661,6 +661,13 @@ void testCreateNumber() {
         assertEquals(Integer.decode("+0xF"), NumberUtils.createNumber("+0xF"), "createNumber(String) LANG-1645a failed");
         assertEquals(Long.decode("+0xFFFFFFFF"), NumberUtils.createNumber("+0xFFFFFFFF"), "createNumber(String) LANG-1645b failed");
         assertEquals(new BigInteger("+FFFFFFFFFFFFFFFF", 16), NumberUtils.createNumber("+0xFFFFFFFFFFFFFFFF"), "createNumber(String) LANG-1645c failed");
+        // A hex literal too large for a Long but carrying an explicit 'L'/'l' type suffix must drop the suffix
+        // before falling back to BigInteger, matching the decimal path (e.g. "12345678901234567890L").
+        assertEquals(new BigInteger("FFFFFFFFFFFFFFFF", 16), NumberUtils.createNumber("0xFFFFFFFFFFFFFFFFL"));
+        assertEquals(new BigInteger("FFFFFFFFFFFFFFFF", 16), NumberUtils.createNumber("0xFFFFFFFFFFFFFFFFl"));
+        assertEquals(new BigInteger("8000000000000000", 16), NumberUtils.createNumber("0x8000000000000000L"));
+        assertEquals(new BigInteger("-FFFFFFFFFFFFFFFF", 16), NumberUtils.createNumber("-0xFFFFFFFFFFFFFFFFL"));
+        assertEquals(new BigInteger("FFFFFFFFFFFFFFFF", 16), NumberUtils.createNumber("#FFFFFFFFFFFFFFFFL"));
         // Map to a BigDecimal, not a Float.

Review Comment:
   You’re right, I worded that poorly. I only meant the new assertions exercise the changed path on current master. Sorry for the confusion.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] strip L suffix for oversized hex literals in createNumber [commons-lang]

Posted by "garydgregory (via GitHub)" <gi...@apache.org>.
garydgregory commented on code in PR #1712:
URL: https://github.com/apache/commons-lang/pull/1712#discussion_r3430662369


##########
src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java:
##########
@@ -661,6 +661,13 @@ void testCreateNumber() {
         assertEquals(Integer.decode("+0xF"), NumberUtils.createNumber("+0xF"), "createNumber(String) LANG-1645a failed");
         assertEquals(Long.decode("+0xFFFFFFFF"), NumberUtils.createNumber("+0xFFFFFFFF"), "createNumber(String) LANG-1645b failed");
         assertEquals(new BigInteger("+FFFFFFFFFFFFFFFF", 16), NumberUtils.createNumber("+0xFFFFFFFFFFFFFFFF"), "createNumber(String) LANG-1645c failed");
+        // A hex literal too large for a Long but carrying an explicit 'L'/'l' type suffix must drop the suffix
+        // before falling back to BigInteger, matching the decimal path (e.g. "12345678901234567890L").
+        assertEquals(new BigInteger("FFFFFFFFFFFFFFFF", 16), NumberUtils.createNumber("0xFFFFFFFFFFFFFFFFL"));
+        assertEquals(new BigInteger("FFFFFFFFFFFFFFFF", 16), NumberUtils.createNumber("0xFFFFFFFFFFFFFFFFl"));
+        assertEquals(new BigInteger("8000000000000000", 16), NumberUtils.createNumber("0x8000000000000000L"));
+        assertEquals(new BigInteger("-FFFFFFFFFFFFFFFF", 16), NumberUtils.createNumber("-0xFFFFFFFFFFFFFFFFL"));
+        assertEquals(new BigInteger("FFFFFFFFFFFFFFFF", 16), NumberUtils.createNumber("#FFFFFFFFFFFFFFFFL"));
         // Map to a BigDecimal, not a Float.

Review Comment:
   @sarankumarbaskar 
   You wrote "and the added tests fail without the production change."
   If I add the test only side of the patch, ALL of the new BigInteger assertions in this patch fail. So what are you talking about?
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@commons.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org