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

Reviewed-by: alanb, lancea
This commit is contained in:
Brian Burkhalter 2023-04-25 20:18:19 +00:00
parent d819debaa5
commit e3ccaa6541
4 changed files with 74 additions and 5 deletions

View File

@ -2045,8 +2045,17 @@ public abstract sealed class $Type$Buffer
*/
public $Type$Buffer append(CharSequence csq, int start, int end) {
if (csq instanceof CharBuffer cb) {
int pos = position();
//
// the append method throws BufferOverflowException when
// there is insufficient space in the buffer
//
int length = end - start;
int pos = position();
int lim = limit();
int rem = (pos <= lim) ? lim - pos : 0;
if (length > rem)
throw new BufferOverflowException();
put(pos, cb, start, length);
position(pos + length);
return this;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -627,6 +627,36 @@ public class Basic$Type$
absBulkGet(b);
#if[char]
// 8306623
String str = "in violet night walking beneath a reign of uncouth stars";
char[] chars = str.toCharArray();
int cslen = chars.length;
CharSequence[] csqs = new CharSequence[] {
str,
new StringBuffer(str),
new StringBuilder(str),
CharBuffer.wrap(chars),
ByteBuffer.allocateDirect(2*chars.length).asCharBuffer()
};
int[][] bounds = new int[][] {
{-1, cslen}, // negative start
{0, -1}, // negative end
{1, 0}, // start > end
{cslen/2, cslen + 1} // end > cslen
};
for (CharSequence csq : csqs) {
// append() should throw BufferOverflowException
tryCatch(b, BufferOverflowException.class, () ->
CharBuffer.allocate(cslen/8).append(csq, cslen/4, cslen/2));
// append() should throw IndexOutOfBoundsException
for (int[] bds : bounds)
tryCatch(b, IndexOutOfBoundsException.class, () ->
CharBuffer.allocate(cslen + 1).append(csq, bds[0], bds[1]));
}
// end 8306623
bulkPutString(b);
relGet(b);

View File

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

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -627,6 +627,36 @@ public class BasicChar
absBulkGet(b);
// 8306623
String str = "in violet night walking beneath a reign of uncouth stars";
char[] chars = str.toCharArray();
int cslen = chars.length;
CharSequence[] csqs = new CharSequence[] {
str,
new StringBuffer(str),
new StringBuilder(str),
CharBuffer.wrap(chars),
ByteBuffer.allocateDirect(2*chars.length).asCharBuffer()
};
int[][] bounds = new int[][] {
{-1, cslen}, // negative start
{0, -1}, // negative end
{1, 0}, // start > end
{cslen/2, cslen + 1} // end > cslen
};
for (CharSequence csq : csqs) {
// append() should throw BufferOverflowException
tryCatch(b, BufferOverflowException.class, () ->
CharBuffer.allocate(cslen/8).append(csq, cslen/4, cslen/2));
// append() should throw IndexOutOfBoundsException
for (int[] bds : bounds)
tryCatch(b, IndexOutOfBoundsException.class, () ->
CharBuffer.allocate(cslen + 1).append(csq, bds[0], bds[1]));
}
// end 8306623
bulkPutString(b);
relGet(b);