JDK-8306623 : (bf) CharBuffer::allocate throws unexpected exception type with some CharSequences
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 21
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • Submitted: 2023-04-20
  • Updated: 2023-07-24
  • Resolved: 2023-04-25
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 21
21 b20Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Description
Consider the following code:

        CharSequence csq = java.nio.CharBuffer.wrap(new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g'});
        // CharBuffer::allocate spec
        // Throws:
        //   BufferOverflowException – If there is insufficient space in this buffer
        //   IndexOutOfBoundsException – If start or end are negative, start is greater than end, or end is greater than csq.length()
        System.out.println("csq.length() = " + csq.length());
        int capacity = 1;
        java.nio.CharBuffer.allocate(capacity).append(csq, 0, 4);


The expectations are to get java.nio.BufferOverflowException which is the case for JDK21 b18, however for b19 IOOBE is thrown:

jshell>         java.nio.CharBuffer.allocate(1)
   ...>                 .append(java.nio.CharBuffer.wrap(new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g'}), 0, 4);
   ...> 
|  Exception java.lang.IndexOutOfBoundsException: Range [0, 0 + 4) out of bounds for length 1
|        at Preconditions.outOfBounds (Preconditions.java:100)
|        at Preconditions.outOfBoundsCheckFromIndexSize (Preconditions.java:118)
|        at Preconditions.checkFromIndexSize (Preconditions.java:397)
|        at Objects.checkFromIndexSize (Objects.java:437)
|        at CharBuffer.put (CharBuffer.java:1088)
|        at HeapCharBuffer.put (HeapCharBuffer.java:260)
|        at CharBuffer.append (CharBuffer.java:2050)
|        at HeapCharBuffer.append (HeapCharBuffer.java:329)
|        at (#5:2)

Comments
Changeset: e3ccaa65 Author: Brian Burkhalter <bpb@openjdk.org> Date: 2023-04-25 20:18:19 +0000 URL: https://git.openjdk.org/jdk/commit/e3ccaa6541e98aaa57b31a05cb998d48a0f7ee87
25-04-2023

A pull request was submitted for review. URL: https://git.openjdk.org/jdk/pull/13632 Date: 2023-04-25 01:44:14 +0000
25-04-2023

https://github.com/openjdk/jdk/pull/13415#pullrequestreview-1396740613
24-04-2023

Yes, the changes mean the check for insufficient space in the buffer is skipped and the IndexOutOfBoundsException from the absolute bulk op is thrown instead.
22-04-2023

Could be possibly caused by the JDK-8305811 fix ?
20-04-2023