This is an automated email from the ASF dual-hosted git repository. rmaucher pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit 31645db8c35fb0cdff9226a7cb4f11973ac46c6a 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 | 2 +- .../catalina/loader/TestWebappClassLoader.java | 28 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/java/org/apache/catalina/loader/WebappClassLoaderBase.java b/java/org/apache/catalina/loader/WebappClassLoaderBase.java index b65cae7f37..d196ebc113 100644 --- a/java/org/apache/catalina/loader/WebappClassLoaderBase.java +++ b/java/org/apache/catalina/loader/WebappClassLoaderBase.java @@ -840,7 +840,7 @@ public abstract class WebappClassLoaderBase extends URLClassLoader checkStateForResourceLoading(name); if (name == null || name.startsWith("/")) { - return null; + return Collections.emptyEnumeration(); } String path = nameToPath(name); diff --git a/test/org/apache/catalina/loader/TestWebappClassLoader.java b/test/org/apache/catalina/loader/TestWebappClassLoader.java index 191a7850cb..cc46eaa779 100644 --- a/test/org/apache/catalina/loader/TestWebappClassLoader.java +++ b/test/org/apache/catalina/loader/TestWebappClassLoader.java @@ -241,4 +241,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]
