Repository navigation
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
Open
emilhuseyn wants to merge 1 commit into
emilhuseyn wants to merge 1 commit into
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes WW-5854
NumberConverter.isInRangere-parsed the original string withBigIntegerorBigDecimal. A grouped string such as2,147,483,648does not parse there, thecatchreturnedtrue, andDefaultTypeConverterthen narrowed the value with an unchecked cast, so it wrapped.This takes the fix proposed in the ticket. When the string does not parse,
isInRangenow checks the number thatNumberFormatproduced from it, before any narrowing, against the target type's bounds. For an integer targetNumberFormatonly returns aDoublewhen the value does not fit a long, so that comparison rejects it. Thecatchthat returnedtrueis gone. All bounds areBigDecimalnow, so one comparison covers every target type.Two more cases came up while writing the tests and are fixed by the same change:
DefaultTypeConverter.longValuetrims the string beforeLong.parseLong, butisInRangedid not, so" 2147483648"tointfailed to parse in the check and wrapped to-2147483648. The check now trims too.DoubleorFloat.NumberFormatreturnsInfinityfor a grouped1,000,...with 310 digits, and a grouped1e39was accepted forFloatand becameInfinitythere. A non-finite parsed value is now out of range, and a finite one is compared with the bounds.Double.MAX_VALUEandFloat.MAX_VALUEwritten out in full (the existingtestStringToDoubleConversionPLandtestStringToFloatConversionPL) still convert, because the parsed value is compared in the sameBigDecimal.valueOfform as the bounds.Tests in
NumberConverterTest,en_US:2,147,483,648and-2,147,483,649toInteger,32,768toShort,1,000toByteand9,223,372,036,854,775,808toLongare rejected as out of rangeInteger.MAX_VALUE,Integer.MIN_VALUE,Short.MIN_VALUE,Long.MAX_VALUE,Long.MIN_VALUE) still convert" 2147483648"and"2147483648 "tointare rejected1e309toDoubleand a grouped1e39toFloatare rejectedThree of the new tests fail on
main(they get-2147483648,-2147483648andInfinity) and pass with the change; the bounds test passes on both. The converter tests (NumberConverterTest,XWorkConverterTestand the other*Converter*and*Conversion*tests, 238 in all) pass locally on JDK 21, along with the fullcoresuite.🤖 Generated with Claude Code