From 5bbc34d48c032ccccba8c6836623e5f8041e2202 Mon Sep 17 00:00:00 2001 From: Brian Burkhalter Date: Wed, 2 Jul 2025 13:21:45 -0700 Subject: [PATCH 1/3] 8361299: (bf) CharBuffer.getChars(int,int,char[],int) violates pre-existing specification --- .../classes/java/nio/X-Buffer.java.template | 32 +++++++++++-------- test/jdk/java/nio/Buffer/GetChars.java | 4 +-- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/java.base/share/classes/java/nio/X-Buffer.java.template b/src/java.base/share/classes/java/nio/X-Buffer.java.template index 283f553459778..02f41e64329e9 100644 --- a/src/java.base/share/classes/java/nio/X-Buffer.java.template +++ b/src/java.base/share/classes/java/nio/X-Buffer.java.template @@ -1897,21 +1897,30 @@ public abstract sealed class $Type$Buffer #if[char] /** - * Absolute bulk get method. + * Relative bulk get method. * *

This method transfers {@code srcEnd-srcBegin} characters from this - * buffer into the given array, starting at index {@code srcBegin} in this - * buffer and at offset {@code dstBegin} in the array. The position of this - * buffer is unchanged. + * buffer into the given array, starting at index {@code position() + srcBegin} in this + * buffer and at offset {@code dstBegin} in the array. + * + *

An invocation of this method behaves exaclty the same was as the + * invocation + * + * {@snippet lang=java : + * get(position() + srcBegin, dst, dstBegin, srcEnd - srcBegin) + * } * * @param srcBegin - * The index in this buffer from which the first character will be - * read; must be non-negative and less than {@code limit()} + * The index in this buffer, relative to the current position, + * of the first character to + * read; must be non-negative and less than + * {@code limit() - position()} * * @param srcEnd - * The index in this buffer directly before the last character to - * read; must be non-negative and less or equal than {@code limit()} - * and must be greater or equal than {@code srcBegin} + * The index in this buffer, relative to the current position, + * after the last character to read; + * must be greater than or equal to {@code srcBegin} and less than + * or equal to {@code limit() - position()} * * @param dst * The destination array @@ -1924,14 +1933,11 @@ public abstract sealed class $Type$Buffer * If the preconditions on the {@code srcBegin}, {@code srcEnd}, * and {@code dstBegin} parameters do not hold * - * @implSpec This method is equivalent to - * {@code get(srcBegin, dst, dstBegin, srcEnd - srcBegin)}. - * * @since 25 */ @Override public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) { - get(srcBegin, dst, dstBegin, srcEnd - srcBegin); + get(position() + srcBegin, dst, dstBegin, srcEnd - srcBegin); } /** diff --git a/test/jdk/java/nio/Buffer/GetChars.java b/test/jdk/java/nio/Buffer/GetChars.java index 8633482871751..8bb1230e1880c 100644 --- a/test/jdk/java/nio/Buffer/GetChars.java +++ b/test/jdk/java/nio/Buffer/GetChars.java @@ -36,7 +36,7 @@ /** * @test - * @bug 8343110 + * @bug 8343110 8361299 * @summary Check for expected behavior of CharBuffer.getChars(). * @run testng GetChars * @key randomness @@ -200,7 +200,7 @@ public void testGetChars(String type, CharBuffer cb) { System.out.format("%s position=%d, limit=%d%n", type, cb.position(), cb.limit()); int expected = intSum(cb); var dst = new char[cb.remaining()]; - cb.getChars(cb.position(), cb.limit(), dst, 0); + cb.getChars(0, cb.remaining(), dst, 0); int actual = intSum(dst); assertEquals(actual, expected); } From e9abd20f85ae9bf5dd86427bdf07779496110c2b Mon Sep 17 00:00:00 2001 From: Brian Burkhalter Date: Wed, 2 Jul 2025 14:09:03 -0700 Subject: [PATCH 2/3] 8361299: Fix a type; add a sentence --- .../share/classes/java/nio/X-Buffer.java.template | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/java.base/share/classes/java/nio/X-Buffer.java.template b/src/java.base/share/classes/java/nio/X-Buffer.java.template index 02f41e64329e9..cea9d8c546e51 100644 --- a/src/java.base/share/classes/java/nio/X-Buffer.java.template +++ b/src/java.base/share/classes/java/nio/X-Buffer.java.template @@ -1900,10 +1900,11 @@ public abstract sealed class $Type$Buffer * Relative bulk get method. * *

This method transfers {@code srcEnd-srcBegin} characters from this - * buffer into the given array, starting at index {@code position() + srcBegin} in this - * buffer and at offset {@code dstBegin} in the array. + * buffer into the given array, starting at index + * {@code position() + srcBegin} in this buffer and at offset + * {@code dstBegin} in the array. The position of this buffer is unchanged. * - *

An invocation of this method behaves exaclty the same was as the + *

An invocation of this method behaves exactly the same was as the * invocation * * {@snippet lang=java : From a98e8dd4e1aa527cb6d631e3ec828daea05a53fa Mon Sep 17 00:00:00 2001 From: Brian Burkhalter Date: Wed, 2 Jul 2025 17:30:42 -0700 Subject: [PATCH 3/3] 8361299: Ensure [srcBegin,srcEnd) is contained in [0,limit()-position()) --- .../share/classes/java/nio/X-Buffer.java.template | 7 ++++++- test/jdk/java/nio/Buffer/GetChars.java | 13 +++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/java.base/share/classes/java/nio/X-Buffer.java.template b/src/java.base/share/classes/java/nio/X-Buffer.java.template index cea9d8c546e51..c43004cc0ac90 100644 --- a/src/java.base/share/classes/java/nio/X-Buffer.java.template +++ b/src/java.base/share/classes/java/nio/X-Buffer.java.template @@ -1938,7 +1938,12 @@ public abstract sealed class $Type$Buffer */ @Override public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) { - get(position() + srcBegin, dst, dstBegin, srcEnd - srcBegin); + // Check [srcBegin,srcEnd) is a subset of [0,limit()-position) + int pos = position(); + int lim = limit(); + Objects.checkFromToIndex(srcBegin, srcEnd, lim - pos); + + get(pos + srcBegin, dst, dstBegin, srcEnd - srcBegin); } /** diff --git a/test/jdk/java/nio/Buffer/GetChars.java b/test/jdk/java/nio/Buffer/GetChars.java index 8bb1230e1880c..65aeca70328e0 100644 --- a/test/jdk/java/nio/Buffer/GetChars.java +++ b/test/jdk/java/nio/Buffer/GetChars.java @@ -71,6 +71,19 @@ public void testSrcBeginIsNegative() { () -> CB.getChars(-1, 3, new char[4], 0)); } + @Test + public void testSrcBeginIsNegationOfPosition() { + CB.position(1); + Assert.assertThrows(IndexOutOfBoundsException.class, + () -> { + try { + CB.getChars(-1, 3, new char[4], 0); + } finally { + CB.position(0); + } + }); + } + @Test public void testDstBeginIsNegative() { Assert.assertThrows(IndexOutOfBoundsException.class,