Skip to content

Commit 6249259

Browse files
author
Brian Burkhalter
committed
8361299: (bf) CharBuffer.getChars(int,int,char[],int) violates pre-existing specification
Reviewed-by: alanb, liach
1 parent a41d350 commit 6249259

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

src/java.base/share/classes/java/nio/X-Buffer.java.template

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,21 +1897,31 @@ public abstract sealed class $Type$Buffer
18971897
#if[char]
18981898

18991899
/**
1900-
* Absolute bulk <i>get</i> method.
1900+
* Relative bulk <i>get</i> method.
19011901
*
19021902
* <p> This method transfers {@code srcEnd-srcBegin} characters from this
1903-
* buffer into the given array, starting at index {@code srcBegin} in this
1904-
* buffer and at offset {@code dstBegin} in the array. The position of this
1905-
* buffer is unchanged.
1903+
* buffer into the given array, starting at index
1904+
* {@code position() + srcBegin} in this buffer and at offset
1905+
* {@code dstBegin} in the array. The position of this buffer is unchanged.
1906+
*
1907+
* <p> An invocation of this method behaves exactly the same was as the
1908+
* invocation
1909+
*
1910+
* {@snippet lang=java :
1911+
* get(position() + srcBegin, dst, dstBegin, srcEnd - srcBegin)
1912+
* }
19061913
*
19071914
* @param srcBegin
1908-
* The index in this buffer from which the first character will be
1909-
* read; must be non-negative and less than {@code limit()}
1915+
* The index in this buffer, relative to the current position,
1916+
* of the first character to
1917+
* read; must be non-negative and less than
1918+
* {@code limit() - position()}
19101919
*
19111920
* @param srcEnd
1912-
* The index in this buffer directly before the last character to
1913-
* read; must be non-negative and less or equal than {@code limit()}
1914-
* and must be greater or equal than {@code srcBegin}
1921+
* The index in this buffer, relative to the current position,
1922+
* after the last character to read;
1923+
* must be greater than or equal to {@code srcBegin} and less than
1924+
* or equal to {@code limit() - position()}
19151925
*
19161926
* @param dst
19171927
* The destination array
@@ -1924,14 +1934,16 @@ public abstract sealed class $Type$Buffer
19241934
* If the preconditions on the {@code srcBegin}, {@code srcEnd},
19251935
* and {@code dstBegin} parameters do not hold
19261936
*
1927-
* @implSpec This method is equivalent to
1928-
* {@code get(srcBegin, dst, dstBegin, srcEnd - srcBegin)}.
1929-
*
19301937
* @since 25
19311938
*/
19321939
@Override
19331940
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) {
1934-
get(srcBegin, dst, dstBegin, srcEnd - srcBegin);
1941+
// Check [srcBegin,srcEnd) is a subset of [0,limit()-position)
1942+
int pos = position();
1943+
int lim = limit();
1944+
Objects.checkFromToIndex(srcBegin, srcEnd, lim - pos);
1945+
1946+
get(pos + srcBegin, dst, dstBegin, srcEnd - srcBegin);
19351947
}
19361948

19371949
/**

test/jdk/java/nio/Buffer/GetChars.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
/**
3838
* @test
39-
* @bug 8343110
39+
* @bug 8343110 8361299
4040
* @summary Check for expected behavior of CharBuffer.getChars().
4141
* @run testng GetChars
4242
* @key randomness
@@ -71,6 +71,19 @@ public void testSrcBeginIsNegative() {
7171
() -> CB.getChars(-1, 3, new char[4], 0));
7272
}
7373

74+
@Test
75+
public void testSrcBeginIsNegationOfPosition() {
76+
CB.position(1);
77+
Assert.assertThrows(IndexOutOfBoundsException.class,
78+
() -> {
79+
try {
80+
CB.getChars(-1, 3, new char[4], 0);
81+
} finally {
82+
CB.position(0);
83+
}
84+
});
85+
}
86+
7487
@Test
7588
public void testDstBeginIsNegative() {
7689
Assert.assertThrows(IndexOutOfBoundsException.class,
@@ -200,7 +213,7 @@ public void testGetChars(String type, CharBuffer cb) {
200213
System.out.format("%s position=%d, limit=%d%n", type, cb.position(), cb.limit());
201214
int expected = intSum(cb);
202215
var dst = new char[cb.remaining()];
203-
cb.getChars(cb.position(), cb.limit(), dst, 0);
216+
cb.getChars(0, cb.remaining(), dst, 0);
204217
int actual = intSum(dst);
205218
assertEquals(actual, expected);
206219
}

0 commit comments

Comments
 (0)