Skip to content

Commit ed9769a

Browse files
committed
Avoid infinite loop in PatternMatchUtils
Issue: SPR-12971 (cherry picked from commit 8e074b6)
1 parent b5a56ca commit ed9769a

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

spring-core/src/main/java/org/springframework/util/PatternMatchUtils.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2007 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -50,6 +50,9 @@ public static boolean simpleMatch(String pattern, String str) {
5050
return str.endsWith(pattern.substring(1));
5151
}
5252
String part = pattern.substring(1, nextIndex);
53+
if ("".equals(part)) {
54+
return simpleMatch(pattern.substring(nextIndex), str);
55+
}
5356
int partIndex = str.indexOf(part);
5457
while (partIndex != -1) {
5558
if (simpleMatch(pattern.substring(nextIndex), str.substring(partIndex + part.length()))) {
@@ -74,8 +77,8 @@ public static boolean simpleMatch(String pattern, String str) {
7477
*/
7578
public static boolean simpleMatch(String[] patterns, String str) {
7679
if (patterns != null) {
77-
for (int i = 0; i < patterns.length; i++) {
78-
if (simpleMatch(patterns[i], str)) {
80+
for (String pattern : patterns) {
81+
if (simpleMatch(pattern, str)) {
7982
return true;
8083
}
8184
}

spring-core/src/test/java/org/springframework/util/PatternMatchUtilsTests.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2007 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,31 +16,37 @@
1616

1717
package org.springframework.util;
1818

19-
import junit.framework.TestCase;
19+
import org.junit.Test;
20+
21+
import static org.junit.Assert.*;
2022

2123
/**
2224
* @author Juergen Hoeller
2325
* @author Johan Gorter
2426
*/
25-
public class PatternMatchUtilsTests extends TestCase {
27+
public class PatternMatchUtilsTests {
2628

29+
@Test
2730
public void testTrivial() {
2831
assertEquals(false, PatternMatchUtils.simpleMatch((String) null, ""));
2932
assertEquals(false, PatternMatchUtils.simpleMatch("1", null));
3033
doTest("*", "123", true);
3134
doTest("123", "123", true);
3235
}
3336

37+
@Test
3438
public void testStartsWith() {
3539
doTest("get*", "getMe", true);
3640
doTest("get*", "setMe", false);
3741
}
3842

43+
@Test
3944
public void testEndsWith() {
4045
doTest("*Test", "getMeTest", true);
4146
doTest("*Test", "setMe", false);
4247
}
4348

49+
@Test
4450
public void testBetween() {
4551
doTest("*stuff*", "getMeTest", false);
4652
doTest("*stuff*", "getstuffTest", true);
@@ -49,13 +55,15 @@ public void testBetween() {
4955
doTest("*stuff*", "stuff", true);
5056
}
5157

58+
@Test
5259
public void testStartsEnds() {
5360
doTest("on*Event", "onMyEvent", true);
5461
doTest("on*Event", "onEvent", true);
5562
doTest("3*3", "3", false);
5663
doTest("3*3", "33", true);
5764
}
5865

66+
@Test
5967
public void testStartsEndsBetween() {
6068
doTest("12*45*78", "12345678", true);
6169
doTest("12*45*78", "123456789", false);
@@ -66,6 +74,7 @@ public void testStartsEndsBetween() {
6674
doTest("3*3*3", "333", true);
6775
}
6876

77+
@Test
6978
public void testRidiculous() {
7079
doTest("*1*2*3*", "0011002001010030020201030", true);
7180
doTest("1*2*3*4", "10300204", false);
@@ -74,6 +83,22 @@ public void testRidiculous() {
7483
doTest("*1*2*3*", "132", false);
7584
}
7685

86+
@Test
87+
public void testPatternVariants() {
88+
doTest("*a", "*", false);
89+
doTest("*a", "a", true);
90+
doTest("*a", "b", false);
91+
doTest("*a", "aa", true);
92+
doTest("*a", "ba", true);
93+
doTest("*a", "ab", false);
94+
doTest("**a", "*", false);
95+
doTest("**a", "a", true);
96+
doTest("**a", "b", false);
97+
doTest("**a", "aa", true);
98+
doTest("**a", "ba", true);
99+
doTest("**a", "ab", false);
100+
}
101+
77102
private void doTest(String pattern, String str, boolean shouldMatch) {
78103
assertEquals(shouldMatch, PatternMatchUtils.simpleMatch(pattern, str));
79104
}

0 commit comments

Comments
 (0)