You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was expecting that the NumberUtils.convertNumberToTargetClass method might throw an exception when converting a BigInteger to a Long if the BigInteger value is less than Long.MIN_VALUE or greater than Long.MAX_VALUE.
In other words I would not expect this test to pass:
assertEquals(Long.valueOf(Long.MIN_VALUE),NumberUtils.convertNumberToTargetClass(BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE), Long.class));
... yet it passes due to the two's complement rollover from max to min value.
Attached are a modified version of NumberUtils and NumberUtilsTests which have a fix that will cause an exception to be thrown when the targetClass is Long, the number is an instance of BigInteger or BigDecimal, and the value of the number is outside the range of the long domain. Feel free to use the "fix" if you think this issue is a bug in the NumberUtils class.
Turns out that this is effectively analogous to JDK 8's BigInteger.longValueExact() method, which we of course can't use there but we're now addressing the same issue through min/max comparisons.
Tom Leccese opened SPR-11434 and commented
I was expecting that the NumberUtils.convertNumberToTargetClass method might throw an exception when converting a BigInteger to a Long if the BigInteger value is less than Long.MIN_VALUE or greater than Long.MAX_VALUE.
In other words I would not expect this test to pass:
assertEquals(Long.valueOf(Long.MIN_VALUE),NumberUtils.convertNumberToTargetClass(BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE), Long.class));
... yet it passes due to the two's complement rollover from max to min value.
Attached are a modified version of NumberUtils and NumberUtilsTests which have a fix that will cause an exception to be thrown when the targetClass is Long, the number is an instance of BigInteger or BigDecimal, and the value of the number is outside the range of the long domain. Feel free to use the "fix" if you think this issue is a bug in the NumberUtils class.
Affects: 3.2.7
Attachments:
Issue Links:
Referenced from: commits 4744180
The text was updated successfully, but these errors were encountered: