Skip to content

WW-5854 Check the range of a grouped number on the parsed value, not the string - #2197

Open
emilhuseyn wants to merge 1 commit into
apache:mainfrom
emilhuseyn:WW-5854-number-converter-range
Open

emilhuseyn wants to merge 1 commit into
apache:mainfrom
emilhuseyn:WW-5854-number-converter-range

Conversation

@emilhuseyn

Copy link
Copy Markdown
Contributor

Fixes WW-5854

NumberConverter.isInRange re-parsed the original string with BigInteger or BigDecimal. A grouped string such as 2,147,483,648 does not parse there, the catch returned true, and DefaultTypeConverter then narrowed the value with an unchecked cast, so it wrapped.

This takes the fix proposed in the ticket. When the string does not parse, isInRange now checks the number that NumberFormat produced from it, before any narrowing, against the target type's bounds. For an integer target NumberFormat only returns a Double when the value does not fit a long, so that comparison rejects it. The catch that returned true is gone. All bounds are BigDecimal now, so one comparison covers every target type.

Two more cases came up while writing the tests and are fixed by the same change:

  • Primitive targets with surrounding whitespace. DefaultTypeConverter.longValue trims the string before Long.parseLong, but isInRange did not, so " 2147483648" to int failed to parse in the check and wrapped to -2147483648. The check now trims too.
  • Grouped values too large for Double or Float. NumberFormat returns Infinity for a grouped 1,000,... with 310 digits, and a grouped 1e39 was accepted for Float and became Infinity there. A non-finite parsed value is now out of range, and a finite one is compared with the bounds.

Double.MAX_VALUE and Float.MAX_VALUE written out in full (the existing testStringToDoubleConversionPL and testStringToFloatConversionPL) still convert, because the parsed value is compared in the same BigDecimal.valueOf form as the bounds.

Tests in NumberConverterTest, en_US:

  • 2,147,483,648 and -2,147,483,649 to Integer, 32,768 to Short, 1,000 to Byte and 9,223,372,036,854,775,808 to Long are rejected as out of range
  • the grouped bounds themselves (Integer.MAX_VALUE, Integer.MIN_VALUE, Short.MIN_VALUE, Long.MAX_VALUE, Long.MIN_VALUE) still convert
  • " 2147483648" and "2147483648 " to int are rejected
  • a grouped 1e309 to Double and a grouped 1e39 to Float are rejected

Three of the new tests fail on main (they get -2147483648, -2147483648 and Infinity) and pass with the change; the bounds test passes on both. The converter tests (NumberConverterTest, XWorkConverterTest and the other *Converter* and *Conversion* tests, 238 in all) pass locally on JDK 21, along with the full core suite.

🤖 Generated with Claude Code

…arsed value

isInRange re-parsed the original string, so a grouped value such as
2,147,483,648 failed to parse, the catch returned true and the value
wrapped when DefaultTypeConverter narrowed it. Check the number that
NumberFormat produced instead, trim the string as DefaultTypeConverter
does, and treat a non-finite parsed value as out of range.
Copilot AI balanced review requested due to automatic review settings December 3, 2026 14:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants