8221307: String.substring() OOB exception on start index reports improper information

Reviewed-by: rriggs, redestad
This commit is contained in:
Ivan Gerasimov 2019-08-16 11:35:17 -07:00
parent a073e1261b
commit c0b8844dce
3 changed files with 50 additions and 16 deletions

View file

@ -1868,18 +1868,7 @@ public final class String
* 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 @@ public final class String
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 @@ public final class String
static void checkOffset(int offset, int length) {
if (offset < 0 || offset > length) {
throw new StringIndexOutOfBoundsException("offset " + offset +
",length " + length);
", length " + length);
}
}

View file

@ -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 class StringUTF16 {
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;
}
}