Skip to content

Commit

Permalink
8306623: (bf) CharBuffer::allocate throws unexpected exception type w…
Browse files Browse the repository at this point in the history
…ith some CharSequences

Reviewed-by: alanb, lancea
  • Loading branch information
Brian Burkhalter committed Apr 25, 2023
1 parent d819deb commit e3ccaa6
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
11 changes: 10 additions & 1 deletion src/java.base/share/classes/java/nio/X-Buffer.java.template
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
32 changes: 31 additions & 1 deletion test/jdk/java/nio/Buffer/Basic-X.java.template
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions test/jdk/java/nio/Buffer/Basic.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
32 changes: 31 additions & 1 deletion test/jdk/java/nio/Buffer/BasicChar.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -627,6 +627,36 @@ public static void test(int level, final CharBuffer b, boolean direct) {
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);
Expand Down

1 comment on commit e3ccaa6

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.