This is an automated email from the ASF dual-hosted git repository. rmaucher pushed a commit to branch 9.0.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit 88206c72e6667a83e31e17bb5959aad8eacb4266 Author: opencode <[email protected]> AuthorDate: Wed Oct 7 10:49:43 2026 +0200 Fix NPE in WebappClassLoaderBase.getResources() for leading-slash names The resource name rework in eb91c3e64b made findResources() return null when the name was null or started with '/'. This broke the Enumeration contract of ClassLoader.findResources() and caused getResources() to throw a NullPointerException from CombinedEnumeration.inc() on the first hasMoreElements()/nextElement() call, since getResources() passes the findResources() result directly into CombinedEnumeration. Return an empty enumeration instead, matching the JDK behaviour where findResources() returns an empty (not null) enumeration for names that cannot be found, including leading-slash names. The other lookups (findResource(), getResource(), getResourceAsStream()) already return null consistently for these names. Add regression tests for findResources() and getResources() with leading-slash names. --- .../catalina/loader/WebappClassLoaderBase.java | 4 ++++ .../catalina/loader/TestWebappClassLoader.java | 28 ++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/java/org/apache/catalina/loader/WebappClassLoaderBase.java b/java/org/apache/catalina/loader/WebappClassLoaderBase.java index f6fe6d0f0b..8085d93b9d 100644 --- a/java/org/apache/catalina/loader/WebappClassLoaderBase.java +++ b/java/org/apache/catalina/loader/WebappClassLoaderBase.java @@ -1021,6 +1021,10 @@ public abstract class WebappClassLoaderBase extends URLClassLoader checkStateForResourceLoading(name); + if (name == null || name.startsWith("/")) { + return Collections.emptyEnumeration(); + } + String path = nameToPath(name); WebResource[] webResources; diff --git a/test/org/apache/catalina/loader/TestWebappClassLoader.java b/test/org/apache/catalina/loader/TestWebappClassLoader.java index b2d7ee9d9e..839e906309 100644 --- a/test/org/apache/catalina/loader/TestWebappClassLoader.java +++ b/test/org/apache/catalina/loader/TestWebappClassLoader.java @@ -244,4 +244,32 @@ public class TestWebappClassLoader extends TomcatBaseTest { Assert.assertFalse(urls.hasMoreElements()); } + + + @Test + public void testFindResourcesLeadingSlash() throws Exception { + Tomcat tomcat = getTomcatInstanceTestWebapp(false, true); + + Context c = (Context) tomcat.getHost().findChildren()[0]; + WebappClassLoaderBase cl = (WebappClassLoaderBase) c.getLoader().getClassLoader(); + + Enumeration<URL> urls = cl.findResources("/org/apache/tomcat/Bug58096.java"); + Assert.assertNotNull(urls); + + Assert.assertFalse(urls.hasMoreElements()); + } + + + @Test + public void testGetResourcesLeadingSlash() throws Exception { + Tomcat tomcat = getTomcatInstanceTestWebapp(false, true); + + Context c = (Context) tomcat.getHost().findChildren()[0]; + WebappClassLoaderBase cl = (WebappClassLoaderBase) c.getLoader().getClassLoader(); + + Enumeration<URL> urls = cl.getResources("/org/apache/tomcat/Bug58096.java"); + Assert.assertNotNull(urls); + + Assert.assertFalse(urls.hasMoreElements()); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
