This is an automated email from the ASF dual-hosted git repository. rmaucher pushed a commit to branch 11.0.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit 17c52f24472333a341703d7018b8bf4baeb28cda 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 4a29e31e53..c76853d889 100644 --- a/java/org/apache/catalina/loader/WebappClassLoaderBase.java +++ b/java/org/apache/catalina/loader/WebappClassLoaderBase.java @@ -881,6 +881,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 2850f27b20..bcde963fbb 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]
