Skip to content
Permalink
Browse files
8271732: Regression in StringBuilder.charAt bounds checking
Reviewed-by: alanb, naoto
  • Loading branch information
cl4es committed Aug 12, 2021
1 parent ec2fc38 commit a15b659278741337aefc15ce8002df66ce6323c0
Showing 3 changed files with 108 additions and 2 deletions.
@@ -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;

1 comment on commit a15b659

@openjdk-notifier
Copy link

@openjdk-notifier openjdk-notifier bot commented on a15b659 Aug 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.