JDK-8306959 : (bf) CharBuffer.append(CharSequence,int,int) throws BufferOverflowException where IndexOutOfBoundsException expected
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 21
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2023-04-26
  • Updated: 2023-05-04
  • Resolved: 2023-04-27
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 b21Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Description
CharBuffer.append(CharSequence csq, int start, int end) should throw an IndexOutOfBoundsException according to the specification of Appendable.append(CharSequence csq, int start, int end) which states the preconditions: 0 <= start <= end <= csq.length(). For example,

	CharBuffer.allocate(7).append("12345678", 4, 12);

should throw an IndexOutOfBoundsException.

Specifically, in the current code, a BufferOverflowException is thrown if end - start > cb.remaining() without first checking the validity of start and end, which clearly should precede the overflow check as otherwise the indexes are not known to be valid. Throwing an IndexOutOfBoundsException also matches the behavior in JDK 20.
Comments
Changeset: 80fae514 Author: Brian Burkhalter <bpb@openjdk.org> Date: 2023-04-27 17:20:08 +0000 URL: https://git.openjdk.org/jdk/commit/80fae514b16bfe38ff29f71f8cdc6d4c3482ad3d
27-04-2023

A pull request was submitted for review. URL: https://git.openjdk.org/jdk/pull/13680 Date: 2023-04-26 22:48:19 +0000
26-04-2023