RFR: 8318915: Enhance checks in BigDecimal.toPlainString() [v2]

Brian Burkhalter bpb at openjdk.org
Wed Nov 8 00:28:05 UTC 2023


On Tue, 7 Nov 2023 19:17:03 GMT, Raffaello Giulietti <rgiulietti at openjdk.org> wrote:

>> Prevent a `NegativeArraySizeException` in `BigDecimal.toPlainString()`, throwing `OutOfMemoryError` instead to indicate that the resulting `String` would be too large.
>
> Raffaello Giulietti has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Using repeat() instead of loop.

+1

The test might be a good candidate for conversion to JUnit 5, but that does not need to be part of this request.

src/java.base/share/classes/java/math/BigDecimal.java line 3510:

> 3508:                 : intVal.toString();
> 3509:             int len = str.length() + trailingZeros;
> 3510:             if (len < 0) {

One could use `Math.addExact`, which has an intrinsic, and convert any thrown `ArithmeticException` into an OOME but I am not sure it's worth it.

-------------

Marked as reviewed by bpb (Reviewer).

PR Review: https://git.openjdk.org/jdk/pull/16457#pullrequestreview-1719081771
PR Comment: https://git.openjdk.org/jdk/pull/16457#issuecomment-1800720638
PR Review Comment: https://git.openjdk.org/jdk/pull/16457#discussion_r1385790342


More information about the core-libs-dev mailing list