Avoid infinite loop in PatternMatchUtils

Issue: SPR-12971
This commit is contained in:
Juergen Hoeller 2015-05-07 15:41:40 +02:00
parent 2c043ec9d7
commit db80378dbe
2 changed files with 34 additions and 6 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -50,6 +50,9 @@ public abstract class PatternMatchUtils {
return str.endsWith(pattern.substring(1));
}
String part = pattern.substring(1, nextIndex);
if ("".equals(part)) {
return simpleMatch(pattern.substring(nextIndex), str);
}
int partIndex = str.indexOf(part);
while (partIndex != -1) {
if (simpleMatch(pattern.substring(nextIndex), str.substring(partIndex + part.length()))) {
@ -74,8 +77,8 @@ public abstract class PatternMatchUtils {
*/
public static boolean simpleMatch(String[] patterns, String str) {
if (patterns != null) {
for (int i = 0; i < patterns.length; i++) {
if (simpleMatch(patterns[i], str)) {
for (String pattern : patterns) {
if (simpleMatch(pattern, str)) {
return true;
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,14 +16,17 @@
package org.springframework.util;
import junit.framework.TestCase;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* @author Juergen Hoeller
* @author Johan Gorter
*/
public class PatternMatchUtilsTests extends TestCase {
public class PatternMatchUtilsTests {
@Test
public void testTrivial() {
assertEquals(false, PatternMatchUtils.simpleMatch((String) null, ""));
assertEquals(false, PatternMatchUtils.simpleMatch("1", null));
@ -31,16 +34,19 @@ public class PatternMatchUtilsTests extends TestCase {
doTest("123", "123", true);
}
@Test
public void testStartsWith() {
doTest("get*", "getMe", true);
doTest("get*", "setMe", false);
}
@Test
public void testEndsWith() {
doTest("*Test", "getMeTest", true);
doTest("*Test", "setMe", false);
}
@Test
public void testBetween() {
doTest("*stuff*", "getMeTest", false);
doTest("*stuff*", "getstuffTest", true);
@ -49,6 +55,7 @@ public class PatternMatchUtilsTests extends TestCase {
doTest("*stuff*", "stuff", true);
}
@Test
public void testStartsEnds() {
doTest("on*Event", "onMyEvent", true);
doTest("on*Event", "onEvent", true);
@ -56,6 +63,7 @@ public class PatternMatchUtilsTests extends TestCase {
doTest("3*3", "33", true);
}
@Test
public void testStartsEndsBetween() {
doTest("12*45*78", "12345678", true);
doTest("12*45*78", "123456789", false);
@ -66,6 +74,7 @@ public class PatternMatchUtilsTests extends TestCase {
doTest("3*3*3", "333", true);
}
@Test
public void testRidiculous() {
doTest("*1*2*3*", "0011002001010030020201030", true);
doTest("1*2*3*4", "10300204", false);
@ -74,6 +83,22 @@ public class PatternMatchUtilsTests extends TestCase {
doTest("*1*2*3*", "132", false);
}
@Test
public void testPatternVariants() {
doTest("*a", "*", false);
doTest("*a", "a", true);
doTest("*a", "b", false);
doTest("*a", "aa", true);
doTest("*a", "ba", true);
doTest("*a", "ab", false);
doTest("**a", "*", false);
doTest("**a", "a", true);
doTest("**a", "b", false);
doTest("**a", "aa", true);
doTest("**a", "ba", true);
doTest("**a", "ab", false);
}
private void doTest(String pattern, String str, boolean shouldMatch) {
assertEquals(shouldMatch, PatternMatchUtils.simpleMatch(pattern, str));
}