JDK-8210583 : Base64.Encoder incorrectly throws NegativeArraySizeException
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util
  • Affected Version: 9,11,12
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2018-09-09
  • Updated: 2019-04-03
  • Resolved: 2019-01-24
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 13
13 b06Fixed
Related Reports
CSR :  
Relates :  
Relates :  
Sub Tasks
JDK-8217706 :  
Description
ADDITIONAL SYSTEM INFORMATION :
openjdk version "11-ea" 2018-09-25
OpenJDK Runtime Environment 18.9 (build 11-ea+26)
OpenJDK 64-Bit Server VM 18.9 (build 11-ea+26, mixed mode)


A DESCRIPTION OF THE PROBLEM :
Base64.Encoder incorrectly handles arrays of sizes that approach Integer.MAX_VALUE. Due to a bug in the implementation, a NegativeArraySizeException may be thrown during encoding. Instead, it should either complete the encoding successfully, or throw an OutOfMemoryError.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
launch jshell:
jshell -R-Xmx8G

and execute the following 2 commands (the first will take quite some time):
byte[] input = new byte[Integer.MAX_VALUE-2];
byte[] output = Base64.getEncoder().encode(input);

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Either no exception occurs, or an OutOfMemoryError is thrown
ACTUAL -
|  Exception java.lang.NegativeArraySizeException: -1431655768
|        at Base64$Encoder.encode (Base64.java:265)
|        at (#6:1)


Comments
The test added for this fails with: java.lang.OutOfMemoryError: Requested array size exceeds VM limit at TestEncodingDecodingLength.main(TestEncodingDecodingLength.java:41)
03-02-2019

The issue exists with Base64.Decoder as well. byte[] input = new byte[Integer.MAX_VALUE-2]; Base64.getDecoder().decode(input); throws NegativeArraySizeException
19-12-2018

check max_length_for_array
11-09-2018

To reproduce the issue, follow the steps outlined in the bug report. JDK 11-ea+28 - Fail JDK 12-ea - Fail Output: jshell> byte[] output = Base64.getEncoder().encode(input); | Exception java.lang.NegativeArraySizeException: -1431655768 | at Base64$Encoder.encode (Base64.java:265) | at (#2:1)
11-09-2018