< prev index next >

src/java.base/share/classes/java/lang/String.java

Print this page
rev 55936 : [mq]: 8221307-String-substring-OOB-exception-on-start-index-reports-improper-information

*** 1866,1885 **** * @exception IndexOutOfBoundsException if * {@code beginIndex} is negative or larger than the * 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); } /** --- 1866,1886 ---- * @exception IndexOutOfBoundsException if * {@code beginIndex} is negative or larger than the * length of this {@code String} object. */ public String substring(int beginIndex) { int subLen = length() - beginIndex; ! if (beginIndex <= 0 || subLen <= 0) { if (beginIndex == 0) { return this; } + if (subLen == 0) { + return ""; + } + throw new StringIndexOutOfBoundsException( + "begin " + beginIndex + ", length " + length()); + } return isLatin1() ? StringLatin1.newString(value, beginIndex, subLen) : StringUTF16.newString(value, beginIndex, subLen); } /**
< prev index next >