Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8271732: Regression in StringBuilder.charAt bounds checking #5086

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -349,10 +349,11 @@ public void setLength(int newLength) {
*/
@Override
public char charAt(int index) {
checkIndex(index, count);
if (isLatin1()) {
return StringLatin1.charAt(value, index);
return (char)(value[index] & 0xff);
}
return StringUTF16.charAt(value, index);
return StringUTF16.getChar(value, index);
}

/**
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2021, 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.
*/

/*
* @test
* @bug 8271732
* @summary Basic test that charAt throws IIOBE as expected for out of bounds indexes.
* @run testng CharAt
*/

import java.util.List;

import org.testng.annotations.Test;
import org.testng.annotations.DataProvider;
import static org.testng.Assert.*;

public class CharAt {

/**
* StringBuilder/-Buffer.charAt throws:
* IndexOutOfBoundsException - if index is negative or greater than or equal to length().
* the test inputs, expected to throw IndexOutOfBoundsException.
*/
@Test
public void charAtIIOBE() {
StringBuilder sb = new StringBuilder("test");
StringBuffer sbuf = new StringBuffer("test");

StringBuilder sbUtf16 = new StringBuilder("\uFF34est"); // Fullwidth Latin Capital Letter T
StringBuffer sbufUtf16 = new StringBuffer("\uFF34est");

List<Integer> outOfBoundsIndices = List.of(Integer.MIN_VALUE, -2, -1, 4, 5, Integer.MAX_VALUE);

for (int index : outOfBoundsIndices) {
try {
sb.charAt(index);
fail("StringBuilder.charAt index: " + index + " does not throw IOOBE as expected");
} catch (IndexOutOfBoundsException ex) {
// OK
}

try {
sbUtf16.charAt(index);
fail("StringBuilder.charAt index: " + index + " does not throw IOOBE as expected (UTF-16)");
} catch (IndexOutOfBoundsException ex) {
// OK
}

try {
sbuf.charAt(index);
fail("StringBuffer.charAt index: " + index + " does not throw IOOBE as expected");
} catch (IndexOutOfBoundsException ex) {
// OK
}

try {
sbufUtf16.charAt(index);
fail("StringBuffer.charAt index: " + index + " does not throw IOOBE as expected (UTF-16)");
} catch (IndexOutOfBoundsException ex) {
// OK
}
}
}
}
@@ -24,18 +24,25 @@

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.CompilerControl;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;

import java.util.concurrent.TimeUnit;

@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Thread)
@Warmup(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 5, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
@Fork(3)
public class StringBuilders {

private String[] strings;
@@ -330,6 +337,20 @@ public String appendBoundsMix(Data data) {
endIndex).append(';').toString();
}

public int charAt_index = 3;

@Benchmark
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
public char charAtLatin1() {
return sbLatin1.charAt(charAt_index);
}

@Benchmark
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
public char charAtUtf16() {
return sbUtf16.charAt(charAt_index);
}

@State(Scope.Thread)
public static class Data {
int i = 0;