Skip to content

Commit e3ccaa6

Browse files
author
Brian Burkhalter
committed
8306623: (bf) CharBuffer::allocate throws unexpected exception type with some CharSequences
Reviewed-by: alanb, lancea
1 parent d819deb commit e3ccaa6

File tree

4 files changed

+74
-5
lines changed

4 files changed

+74
-5
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2045,8 +2045,17 @@ public abstract sealed class $Type$Buffer
20452045
*/
20462046
public $Type$Buffer append(CharSequence csq, int start, int end) {
20472047
if (csq instanceof CharBuffer cb) {
2048-
int pos = position();
2048+
//
2049+
// the append method throws BufferOverflowException when
2050+
// there is insufficient space in the buffer
2051+
//
20492052
int length = end - start;
2053+
int pos = position();
2054+
int lim = limit();
2055+
int rem = (pos <= lim) ? lim - pos : 0;
2056+
if (length > rem)
2057+
throw new BufferOverflowException();
2058+
20502059
put(pos, cb, start, length);
20512060
position(pos + length);
20522061
return this;

test/jdk/java/nio/Buffer/Basic-X.java.template

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -627,6 +627,36 @@ public class Basic$Type$
627627
absBulkGet(b);
628628

629629
#if[char]
630+
// 8306623
631+
String str = "in violet night walking beneath a reign of uncouth stars";
632+
char[] chars = str.toCharArray();
633+
int cslen = chars.length;
634+
CharSequence[] csqs = new CharSequence[] {
635+
str,
636+
new StringBuffer(str),
637+
new StringBuilder(str),
638+
CharBuffer.wrap(chars),
639+
ByteBuffer.allocateDirect(2*chars.length).asCharBuffer()
640+
};
641+
642+
int[][] bounds = new int[][] {
643+
{-1, cslen}, // negative start
644+
{0, -1}, // negative end
645+
{1, 0}, // start > end
646+
{cslen/2, cslen + 1} // end > cslen
647+
};
648+
649+
for (CharSequence csq : csqs) {
650+
// append() should throw BufferOverflowException
651+
tryCatch(b, BufferOverflowException.class, () ->
652+
CharBuffer.allocate(cslen/8).append(csq, cslen/4, cslen/2));
653+
654+
// append() should throw IndexOutOfBoundsException
655+
for (int[] bds : bounds)
656+
tryCatch(b, IndexOutOfBoundsException.class, () ->
657+
CharBuffer.allocate(cslen + 1).append(csq, bds[0], bds[1]));
658+
}
659+
// end 8306623
630660

631661
bulkPutString(b);
632662
relGet(b);

test/jdk/java/nio/Buffer/Basic.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -26,7 +26,7 @@
2626
* @bug 4413135 4414911 4416536 4416562 4418782 4471053 4472779 4490253 4523725
2727
* 4526177 4463011 4660660 4661219 4663521 4782970 4804304 4938424 5029431
2828
* 5071718 6231529 6221101 6234263 6535542 6591971 6593946 6795561 7190219
29-
* 7199551 8065556 8149469 8230665 8237514
29+
* 7199551 8065556 8149469 8230665 8237514 8306623
3030
* @modules java.base/java.nio:open
3131
* java.base/jdk.internal.misc
3232
* @author Mark Reinhold

test/jdk/java/nio/Buffer/BasicChar.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -627,6 +627,36 @@ public static void test(int level, final CharBuffer b, boolean direct) {
627627
absBulkGet(b);
628628

629629

630+
// 8306623
631+
String str = "in violet night walking beneath a reign of uncouth stars";
632+
char[] chars = str.toCharArray();
633+
int cslen = chars.length;
634+
CharSequence[] csqs = new CharSequence[] {
635+
str,
636+
new StringBuffer(str),
637+
new StringBuilder(str),
638+
CharBuffer.wrap(chars),
639+
ByteBuffer.allocateDirect(2*chars.length).asCharBuffer()
640+
};
641+
642+
int[][] bounds = new int[][] {
643+
{-1, cslen}, // negative start
644+
{0, -1}, // negative end
645+
{1, 0}, // start > end
646+
{cslen/2, cslen + 1} // end > cslen
647+
};
648+
649+
for (CharSequence csq : csqs) {
650+
// append() should throw BufferOverflowException
651+
tryCatch(b, BufferOverflowException.class, () ->
652+
CharBuffer.allocate(cslen/8).append(csq, cslen/4, cslen/2));
653+
654+
// append() should throw IndexOutOfBoundsException
655+
for (int[] bds : bounds)
656+
tryCatch(b, IndexOutOfBoundsException.class, () ->
657+
CharBuffer.allocate(cslen + 1).append(csq, bds[0], bds[1]));
658+
}
659+
// end 8306623
630660

631661
bulkPutString(b);
632662
relGet(b);

0 commit comments

Comments
 (0)