Skip to content

Commit 6d66f2e

Browse files
committed
issue #1655: prevent NullPointerException if contextPath is null
1 parent 7e74007 commit 6d66f2e

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

extensions/servlet/src/com/google/inject/servlet/ServletDefinition.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,9 @@ public String getPathInfo() {
212212
String servletPath = getServletPath();
213213
int servletPathLength = servletPath.length();
214214
String requestUri = getRequestURI();
215-
pathInfo = requestUri.substring(getContextPath().length()).replaceAll("[/]{2,}", "/");
215+
String contextPath = getContextPath();
216+
int contextPathLength = contextPath != null ? contextPath.length() : 0;
217+
pathInfo = requestUri.substring(contextPathLength).replaceAll("[/]{2,}", "/");
216218
// See: https://github.com/google/guice/issues/372
217219
if (pathInfo.startsWith(servletPath)) {
218220
pathInfo = pathInfo.substring(servletPathLength);

extensions/servlet/test/com/google/inject/servlet/ServletDefinitionPathsTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ protected void service(
113113
// Data-driven test.
114114
public final void testPathInfoWithServletStyleMatching() throws IOException, ServletException {
115115
pathInfoWithServletStyleMatching("/path/index.html", "/path", "/*", "/index.html", "");
116+
pathInfoWithServletStyleMatching("/index.html", null, "/*", "/index.html", "");
116117
pathInfoWithServletStyleMatching(
117118
"/path//hulaboo///index.html", "/path", "/*", "/hulaboo/index.html", "");
118119
pathInfoWithServletStyleMatching("/path/", "/path", "/*", "/", "");

0 commit comments

Comments
 (0)