From e3ccaa6541e98aaa57b31a05cb998d48a0f7ee87 Mon Sep 17 00:00:00 2001 From: Brian Burkhalter Date: Tue, 25 Apr 2023 20:18:19 +0000 Subject: [PATCH] 8306623: (bf) CharBuffer::allocate throws unexpected exception type with some CharSequences Reviewed-by: alanb, lancea --- .../classes/java/nio/X-Buffer.java.template | 11 ++++++- .../jdk/java/nio/Buffer/Basic-X.java.template | 32 ++++++++++++++++++- test/jdk/java/nio/Buffer/Basic.java | 4 +-- test/jdk/java/nio/Buffer/BasicChar.java | 32 ++++++++++++++++++- 4 files changed, 74 insertions(+), 5 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 6c496667d25..1261e325515 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 @@ -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; diff --git a/test/jdk/java/nio/Buffer/Basic-X.java.template b/test/jdk/java/nio/Buffer/Basic-X.java.template index 0c328c89baf..25ba1e2e26d 100644 --- a/test/jdk/java/nio/Buffer/Basic-X.java.template +++ b/test/jdk/java/nio/Buffer/Basic-X.java.template @@ -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); diff --git a/test/jdk/java/nio/Buffer/Basic.java b/test/jdk/java/nio/Buffer/Basic.java index 90d5ec943ab..6bdf901012b 100644 --- a/test/jdk/java/nio/Buffer/Basic.java +++ b/test/jdk/java/nio/Buffer/Basic.java @@ -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 diff --git a/test/jdk/java/nio/Buffer/BasicChar.java b/test/jdk/java/nio/Buffer/BasicChar.java index 08c247841ef..58cbd0799ec 100644 --- a/test/jdk/java/nio/Buffer/BasicChar.java +++ b/test/jdk/java/nio/Buffer/BasicChar.java @@ -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);