| // Copyright (c) 2024, the R8 project authors. Please see the AUTHORS file |
| // for details. All rights reserved. Use of this source code is governed by a |
| // BSD-style license that can be found in the LICENSE file. |
| |
| package string; |
| |
| import com.android.tools.r8.TestBase; |
| import com.android.tools.r8.TestParameters; |
| import com.android.tools.r8.TestParametersCollection; |
| import com.android.tools.r8.ToolHelper; |
| import com.android.tools.r8.utils.StringUtils; |
| import org.junit.Test; |
| import org.junit.runner.RunWith; |
| import org.junit.runners.Parameterized; |
| import org.junit.runners.Parameterized.Parameter; |
| import org.junit.runners.Parameterized.Parameters; |
| |
| @RunWith(Parameterized.class) |
| public class StringBuilderWithAppendOutOfBoundsTest extends TestBase { |
| |
| private static final String EXPECTED_OUTPUT = |
| StringUtils.lines("class java.lang.IndexOutOfBoundsException"); |
| |
| @Parameter(0) |
| public TestParameters parameters; |
| |
| @Parameters(name = "{0}") |
| public static TestParametersCollection data() { |
| return getTestParameters().withDexRuntimes().withAllApiLevels().build(); |
| } |
| |
| @Test |
| public void testD8() throws Exception { |
| testForRuntime(parameters) |
| .addInnerClassesAndStrippedOuter(getClass()) |
| .run(parameters.getRuntime(), Main.class) |
| .assertSuccessWithOutput(EXPECTED_OUTPUT); |
| } |
| |
| @Test |
| public void testR8() throws Exception { |
| testForR8(parameters.getBackend()) |
| .addInnerClassesAndStrippedOuter(getClass()) |
| .addKeepMainRule(Main.class) |
| .addLibraryFiles(ToolHelper.getAndroidJar(35)) |
| .setMinApi(parameters) |
| .run(parameters.getRuntime(), Main.class) |
| .assertSuccessWithOutput(EXPECTED_OUTPUT); |
| } |
| |
| static class Main { |
| |
| public static void main(String[] strArr) { |
| String text = "sss"; |
| int l = 7; |
| |
| StringBuilder sb = new StringBuilder(); |
| try { |
| sb.append(text, 0, l); |
| System.out.println("not out of bounds"); |
| } catch (Exception e) { |
| System.out.println(e.getClass()); |
| } |
| } |
| } |
| } |