diff --git a/src/java.base/share/classes/java/nio/Heap-X-Buffer.java.template b/src/java.base/share/classes/java/nio/Heap-X-Buffer.java.template index b77e45ee156..d148fe73674 100644 --- a/src/java.base/share/classes/java/nio/Heap-X-Buffer.java.template +++ b/src/java.base/share/classes/java/nio/Heap-X-Buffer.java.template @@ -289,6 +289,8 @@ class Heap$Type$Buffer$RW$ private $Type$Buffer appendChars(CharSequence csq, int start, int end) { checkSession(); + Objects.checkFromToIndex(start, end, csq.length()); + int length = end - start; int pos = position(); int lim = limit(); 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 698ecbfc9e4..a9a5b4e9e90 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,6 +2045,8 @@ public abstract sealed class $Type$Buffer */ public $Type$Buffer append(CharSequence csq, int start, int end) { if (csq instanceof CharBuffer cb) { + Objects.checkFromToIndex(start, end, csq.length()); + // // the append method throws BufferOverflowException when // there is insufficient space in the buffer diff --git a/test/jdk/java/nio/Buffer/Basic-X.java.template b/test/jdk/java/nio/Buffer/Basic-X.java.template index 53982769694..d26d3e3e622 100644 --- a/test/jdk/java/nio/Buffer/Basic-X.java.template +++ b/test/jdk/java/nio/Buffer/Basic-X.java.template @@ -630,7 +630,7 @@ public class Basic$Type$ absBulkGet(b); #if[char] - // 8306623 + // 8306623 and 8306959 String str = "in violet night walking beneath a reign of uncouth stars"; char[] chars = str.toCharArray(); int cslen = chars.length; @@ -661,14 +661,29 @@ public class Basic$Type$ tryCatch(cbBOE, BufferOverflowException.class, () -> cbBOE.append(csq, cslen/4, cslen/2)); + CharBuffer cb = f.apply(7); + tryCatch(cbBOE, BufferOverflowException.class, () -> + cb.append(csq.subSequence(0, 8), 0, 8)); + // append() should throw IndexOutOfBoundsException final CharBuffer cbIOOBE = f.apply(cslen + 1); for (int[] bds : bounds) tryCatch(cbIOOBE, IndexOutOfBoundsException.class, () -> cbIOOBE.append(csq, bds[0], bds[1])); + + tryCatch(cb, IndexOutOfBoundsException.class, () -> + cb.append(csq.subSequence(0, 8), 4, 12)); + + // should append nothing + int rem = cb.remaining(); + ck(cb, cb.append(csq, 0, 0).remaining(), rem); + + // should fill the buffer + int start = (csq.length() - rem)/2; + ck(cb, cb.append(csq, start, start + rem).remaining(), 0); } } - // end 8306623 + // end 8306623 and 8306959 bulkPutString(b); relGet(b); diff --git a/test/jdk/java/nio/Buffer/Basic.java b/test/jdk/java/nio/Buffer/Basic.java index 0eac87b765e..34175f04f19 100644 --- a/test/jdk/java/nio/Buffer/Basic.java +++ b/test/jdk/java/nio/Buffer/Basic.java @@ -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 8306374 8306623 + * 7199551 8065556 8149469 8230665 8237514 8306374 8306623 8306959 * @modules java.base/java.nio:open * java.base/jdk.internal.misc * @author Mark Reinhold diff --git a/test/jdk/java/nio/Buffer/BasicByte.java b/test/jdk/java/nio/Buffer/BasicByte.java index cbcebb1040e..b3db0b0c105 100644 --- a/test/jdk/java/nio/Buffer/BasicByte.java +++ b/test/jdk/java/nio/Buffer/BasicByte.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 @@ -43,6 +43,9 @@ import java.util.Random; + + + public class BasicByte extends Basic { @@ -641,6 +644,60 @@ public class BasicByte + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/jdk/java/nio/Buffer/BasicChar.java b/test/jdk/java/nio/Buffer/BasicChar.java index 813b9cff429..c1358847ad2 100644 --- a/test/jdk/java/nio/Buffer/BasicChar.java +++ b/test/jdk/java/nio/Buffer/BasicChar.java @@ -630,7 +630,7 @@ public class BasicChar absBulkGet(b); - // 8306623 + // 8306623 and 8306959 String str = "in violet night walking beneath a reign of uncouth stars"; char[] chars = str.toCharArray(); int cslen = chars.length; @@ -661,14 +661,29 @@ public class BasicChar tryCatch(cbBOE, BufferOverflowException.class, () -> cbBOE.append(csq, cslen/4, cslen/2)); + CharBuffer cb = f.apply(7); + tryCatch(cbBOE, BufferOverflowException.class, () -> + cb.append(csq.subSequence(0, 8), 0, 8)); + // append() should throw IndexOutOfBoundsException final CharBuffer cbIOOBE = f.apply(cslen + 1); for (int[] bds : bounds) tryCatch(cbIOOBE, IndexOutOfBoundsException.class, () -> cbIOOBE.append(csq, bds[0], bds[1])); + + tryCatch(cb, IndexOutOfBoundsException.class, () -> + cb.append(csq.subSequence(0, 8), 4, 12)); + + // should append nothing + int rem = cb.remaining(); + ck(cb, cb.append(csq, 0, 0).remaining(), rem); + + // should fill the buffer + int start = (csq.length() - rem)/2; + ck(cb, cb.append(csq, start, start + rem).remaining(), 0); } } - // end 8306623 + // end 8306623 and 8306959 bulkPutString(b); relGet(b); diff --git a/test/jdk/java/nio/Buffer/BasicDouble.java b/test/jdk/java/nio/Buffer/BasicDouble.java index c569955e7e2..d74fe3db0b7 100644 --- a/test/jdk/java/nio/Buffer/BasicDouble.java +++ b/test/jdk/java/nio/Buffer/BasicDouble.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 @@ -43,6 +43,9 @@ import java.nio.*; + + + public class BasicDouble extends Basic { @@ -641,6 +644,60 @@ public class BasicDouble + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/jdk/java/nio/Buffer/BasicFloat.java b/test/jdk/java/nio/Buffer/BasicFloat.java index 15f1e3c86ee..e60fb7b203c 100644 --- a/test/jdk/java/nio/Buffer/BasicFloat.java +++ b/test/jdk/java/nio/Buffer/BasicFloat.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 @@ -43,6 +43,9 @@ import java.nio.*; + + + public class BasicFloat extends Basic { @@ -641,6 +644,60 @@ public class BasicFloat + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/jdk/java/nio/Buffer/BasicInt.java b/test/jdk/java/nio/Buffer/BasicInt.java index 4d424b0c279..2d0f591e892 100644 --- a/test/jdk/java/nio/Buffer/BasicInt.java +++ b/test/jdk/java/nio/Buffer/BasicInt.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 @@ -43,6 +43,9 @@ import java.nio.*; + + + public class BasicInt extends Basic { @@ -641,6 +644,60 @@ public class BasicInt + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/jdk/java/nio/Buffer/BasicLong.java b/test/jdk/java/nio/Buffer/BasicLong.java index b8ed196757f..04807c3461b 100644 --- a/test/jdk/java/nio/Buffer/BasicLong.java +++ b/test/jdk/java/nio/Buffer/BasicLong.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 @@ -43,6 +43,9 @@ import java.nio.*; + + + public class BasicLong extends Basic { @@ -641,6 +644,60 @@ public class BasicLong + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/test/jdk/java/nio/Buffer/BasicShort.java b/test/jdk/java/nio/Buffer/BasicShort.java index d9eb7c3d413..bbf1b7a7fa4 100644 --- a/test/jdk/java/nio/Buffer/BasicShort.java +++ b/test/jdk/java/nio/Buffer/BasicShort.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 @@ -43,6 +43,9 @@ import java.nio.*; + + + public class BasicShort extends Basic { @@ -641,6 +644,60 @@ public class BasicShort + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +