8221307: String.substring() OOB exception on start index reports improper information
Reviewed-by: rriggs, redestad
--- a/src/java.base/share/classes/java/lang/String.java Fri Aug 16 18:06:51 2019 +0000
+++ b/src/java.base/share/classes/java/lang/String.java Fri Aug 16 11:35:17 2019 -0700
@@ -1868,18 +1868,7 @@
* length of this {@code String} object.
*/
public String substring(int beginIndex) {
- if (beginIndex < 0) {
- throw new StringIndexOutOfBoundsException(beginIndex);
- }
- int subLen = length() - beginIndex;
- if (subLen < 0) {
- throw new StringIndexOutOfBoundsException(subLen);
- }
- if (beginIndex == 0) {
- return this;
- }
- return isLatin1() ? StringLatin1.newString(value, beginIndex, subLen)
- : StringUTF16.newString(value, beginIndex, subLen);
+ return substring(beginIndex, length());
}
/**
@@ -3677,7 +3666,7 @@
static void checkIndex(int index, int length) {
if (index < 0 || index >= length) {
throw new StringIndexOutOfBoundsException("index " + index +
- ",length " + length);
+ ", length " + length);
}
}
@@ -3688,7 +3677,7 @@
static void checkOffset(int offset, int length) {
if (offset < 0 || offset > length) {
throw new StringIndexOutOfBoundsException("offset " + offset +
- ",length " + length);
+ ", length " + length);
}
}
--- a/src/java.base/share/classes/java/lang/StringUTF16.java Fri Aug 16 18:06:51 2019 +0000
+++ b/src/java.base/share/classes/java/lang/StringUTF16.java Fri Aug 16 11:35:17 2019 -0700
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2019, 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
@@ -489,7 +489,7 @@
final char lo = Character.lowSurrogate(ch);
checkBoundsBeginEnd(fromIndex, max, value);
for (int i = fromIndex; i < max - 1; i++) {
- if (getChar(value, i) == hi && getChar(value, i + 1 ) == lo) {
+ if (getChar(value, i) == hi && getChar(value, i + 1) == lo) {
return i;
}
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/micro/org/openjdk/bench/java/lang/StringSubstring.java Fri Aug 16 11:35:17 2019 -0700
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2019, 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package org.openjdk.bench.java.lang;
+
+import org.openjdk.jmh.annotations.*;
+import java.util.concurrent.TimeUnit;
+
+@BenchmarkMode(Mode.AverageTime)
+@OutputTimeUnit(TimeUnit.NANOSECONDS)
+@State(Scope.Benchmark)
+public class StringSubstring {
+
+ public String s = new String("An arbitrary string that happened to be of length 52");
+
+ @Benchmark
+ public String from26toEnd0() {
+ return s.substring(26);
+ }
+
+ @Benchmark
+ public String from26toEnd1() {
+ return s.substring(26, s.length());
+ }
+}