RFR: 8318915: Enhance checks in BigDecimal.toPlainString()

Roger Riggs rriggs at openjdk.org
Tue Nov 7 18:44:31 UTC 2023


On Wed, 1 Nov 2023 17:40:04 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.

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

> 3516:             for (int i = 0; i < trailingZeros; i++) {
> 3517:                 buf.append('0');
> 3518:             }

A bit more compact as:
Suggestion:

            StringBuilder buf = new StringBuilder(len);
            buf.append(str);
            buf.repeat('0', trailingZeros);

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

> 3549:             for (int i = insertionPoint; i < 0; ++i) {
> 3550:                 buf.append('0');
> 3551:             }

Would this be the same as: 
`buf.repeat('0',  -i);`

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

PR Review Comment: https://git.openjdk.org/jdk/pull/16457#discussion_r1385363742
PR Review Comment: https://git.openjdk.org/jdk/pull/16457#discussion_r1385372642


More information about the core-libs-dev mailing list