Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8306623

(bf) CharBuffer::allocate throws unexpected exception type with some CharSequences

XMLWordPrintable

    • b19
    • 21
    • b20
    • Verified

      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)

            bpb Brian Burkhalter
            dbessono Dmitry Bessonov
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: