Skip to content

Commit cfcbfc6

Browse files
Aleksei VoitylovRoger Riggs
Aleksei Voitylov
authored and
Roger Riggs
committedSep 28, 2023
8316879: RegionMatches1Tests fails if CompactStrings are disabled after JDK-8302163
Reviewed-by: simonis, rgiulietti, rriggs
1 parent ca5eee2 commit cfcbfc6

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed
 

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

+4
Original file line numberDiff line numberDiff line change
@@ -2156,6 +2156,10 @@ public boolean regionMatches(int toffset, String other, int ooffset, int len) {
21562156
(ooffset > (long)other.length() - len)) {
21572157
return false;
21582158
}
2159+
// Any strings match if len <= 0
2160+
if (len <= 0) {
2161+
return true;
2162+
}
21592163
byte[] tv = value;
21602164
byte[] ov = other.value;
21612165
byte coder = coder();

‎test/jdk/java/lang/String/RegionMatches.java

+24-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1998, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,18 +23,34 @@
2323

2424
/**
2525
* @test
26-
* @bug 4016509
27-
* @summary test regionMatches corner case
26+
* @bug 4016509 8316879
27+
* @summary test regionMatches corner cases
28+
* @run junit RegionMatches
2829
*/
2930

31+
import java.io.UnsupportedEncodingException;
32+
import org.junit.jupiter.api.Test;
33+
import static org.junit.jupiter.api.Assertions.assertTrue;
3034

3135
public class RegionMatches {
3236

33-
public static void main (String args[]) throws Exception {
34-
String s1="abc";
35-
String s2="def";
37+
private final String s1_LATIN1 = "abc";
38+
private final String s2_LATIN1 = "def";
3639

37-
if (!s1.regionMatches(0,s2,0,Integer.MIN_VALUE))
38-
throw new RuntimeException("Integer overflow in RegionMatches");
40+
private final String s1_UTF16 = "\u041e\u0434\u043d\u0430\u0436\u0434\u044b";
41+
private final String s2_UTF16 = "\u0432\u0441\u0442\u0443\u0434\u0435\u043d";
42+
43+
@Test
44+
public void TestLATIN1() {
45+
// Test for 4016509
46+
boolean result = s1_LATIN1.regionMatches(0, s2_LATIN1, 0, Integer.MIN_VALUE);
47+
assertTrue(result, "Integer overflow in RegionMatches when comparing LATIN1 strings");
48+
}
49+
50+
@Test
51+
public void TestUTF16() throws UnsupportedEncodingException{
52+
// Test for 8316879
53+
boolean result = s1_UTF16.regionMatches(0, s2_UTF16, 0, Integer.MIN_VALUE + 1);
54+
assertTrue(result, "Integer overflow in RegionMatches when comparing UTF16 strings");
3955
}
4056
}

0 commit comments

Comments
 (0)
Please sign in to comment.