JDK-8230743 : StringJoiner does not handle too large strings correctly
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util
  • Affected Version: 11,12,13,14
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2019-09-06
  • Updated: 2024-10-17
  • Resolved: 2020-06-05
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 15
15 b27Fixed
Related Reports
Relates :  
Description
A DESCRIPTION OF THE PROBLEM :
StringJoiner does not handle or detect when too large strings are joined or built. This can result in NegativeArraySizeException and possibly other incorrect behavior.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run the code provided below.
Requirements:
-  String.COMPACT_STRINGS = true (probably the default)
- JVM arg -Xmx3G

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
RuntimeException or OutOfMemoryError is thrown during building because result would be too large.
ACTUAL -
NegativeArraySizeException is thrown.

---------- BEGIN SOURCE ----------
// To not get OutOfMemoryError because JVM cannot create that large arrays
int missingToMaxInt = 10;

// Requires String.COMPACT_STRINGS = true and -Xmx3G
new StringJoiner("")
    .add("a".repeat(Integer.MAX_VALUE - missingToMaxInt))
    .add("a".repeat(missingToMaxInt + 1))
    .toString();
---------- END SOURCE ----------


Comments
URL: https://hg.openjdk.java.net/jdk/jdk/rev/8c5fd0b6fc17 User: jlaskey Date: 2020-06-05 12:37:47 +0000
05-06-2020

I'd think that throwing OOME would be preferable to throwing NegativeArraySizeException.
20-04-2020

Wouldn't fixing this be a significant change in behaviour?
06-01-2020

To reproduce the issue, run the attached test case : JDK 11.0.4 - Fail JDK 13-ea - Fail JDK 14-ea - Fail Output: Exception in thread "main" java.lang.NegativeArraySizeException: -2147483648 at java.base/java.util.StringJoiner.compactElts(StringJoiner.java:242) at java.base/java.util.StringJoiner.toString(StringJoiner.java:173) at JI9062164.main(JI9062164.java:12)
09-09-2019