diff --git a/spring-web/src/main/java/org/springframework/web/util/patterns/PathPattern.java b/spring-web/src/main/java/org/springframework/web/util/patterns/PathPattern.java index 932d8098de..71d4e2be73 100644 --- a/spring-web/src/main/java/org/springframework/web/util/patterns/PathPattern.java +++ b/spring-web/src/main/java/org/springframework/web/util/patterns/PathPattern.java @@ -232,7 +232,7 @@ public class PathPattern implements Comparable { while (separatorCount > 0 && pos < len) { if (path.charAt(pos++) == separator) { // Skip any adjacent separators - while (path.charAt(pos) == separator) { + while (pos < len && path.charAt(pos) == separator) { pos++; } separatorCount--; @@ -240,7 +240,7 @@ public class PathPattern implements Comparable { } int end = len; // Trim trailing separators - while (path.charAt(end - 1) == separator) { + while (end > 0 && path.charAt(end - 1) == separator) { end--; } // Check if multiple separators embedded in the resulting path, if so trim them out. diff --git a/spring-web/src/test/java/org/springframework/web/util/patterns/PathPatternMatcherTests.java b/spring-web/src/test/java/org/springframework/web/util/patterns/PathPatternMatcherTests.java index 431ce27fdb..af1870fe0e 100644 --- a/spring-web/src/test/java/org/springframework/web/util/patterns/PathPatternMatcherTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/patterns/PathPatternMatcherTests.java @@ -467,6 +467,14 @@ public class PathPatternMatcherTests { } } + @Test + public void extractPathWithinPattern_spr15259() { + checkExtractPathWithinPattern("/**","/",""); + checkExtractPathWithinPattern("/**","//",""); + checkExtractPathWithinPattern("/**","",""); + checkExtractPathWithinPattern("/**","/foobar","foobar"); + } + @Test public void extractPathWithinPattern() throws Exception { checkExtractPathWithinPattern("/welcome*/", "/welcome/", "welcome");