diff --git a/src/java.base/share/classes/java/lang/String.java b/src/java.base/share/classes/java/lang/String.java index 5c234d4dfac..9b19d7e2ac1 100644 --- a/src/java.base/share/classes/java/lang/String.java +++ b/src/java.base/share/classes/java/lang/String.java @@ -2156,6 +2156,10 @@ public final class String (ooffset > (long)other.length() - len)) { return false; } + // Any strings match if len <= 0 + if (len <= 0) { + return true; + } byte[] tv = value; byte[] ov = other.value; byte coder = coder(); diff --git a/test/jdk/java/lang/String/RegionMatches.java b/test/jdk/java/lang/String/RegionMatches.java index 43544fbe9e9..575f5283e9b 100644 --- a/test/jdk/java/lang/String/RegionMatches.java +++ b/test/jdk/java/lang/String/RegionMatches.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2023, 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 @@ -23,18 +23,34 @@ /** * @test - * @bug 4016509 - * @summary test regionMatches corner case + * @bug 4016509 8316879 + * @summary test regionMatches corner cases + * @run junit RegionMatches */ +import java.io.UnsupportedEncodingException; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertTrue; public class RegionMatches { - public static void main (String args[]) throws Exception { - String s1="abc"; - String s2="def"; + private final String s1_LATIN1 = "abc"; + private final String s2_LATIN1 = "def"; - if (!s1.regionMatches(0,s2,0,Integer.MIN_VALUE)) - throw new RuntimeException("Integer overflow in RegionMatches"); + private final String s1_UTF16 = "\u041e\u0434\u043d\u0430\u0436\u0434\u044b"; + private final String s2_UTF16 = "\u0432\u0441\u0442\u0443\u0434\u0435\u043d"; + + @Test + public void TestLATIN1() { + // Test for 4016509 + boolean result = s1_LATIN1.regionMatches(0, s2_LATIN1, 0, Integer.MIN_VALUE); + assertTrue(result, "Integer overflow in RegionMatches when comparing LATIN1 strings"); + } + + @Test + public void TestUTF16() throws UnsupportedEncodingException{ + // Test for 8316879 + boolean result = s1_UTF16.regionMatches(0, s2_UTF16, 0, Integer.MIN_VALUE + 1); + assertTrue(result, "Integer overflow in RegionMatches when comparing UTF16 strings"); } }