Bug 58581

Summary: StandardHostValve#custom throws NPE if custom error page is incorrectly configured
Product: Tomcat 9 Reporter: Huxing Zhang <huxing.zhang>
Component: CatalinaAssignee: Tomcat Developers Mailing List <dev>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: unspecified   
Target Milestone: -----   
Hardware: All   
OS: All   
Attachments: proposed fix to avoid NPE

Description Huxing Zhang 2015-11-04 14:35:20 UTC
Created attachment 33255 [details]
proposed fix to avoid NPE

Minimal files to reproduce this issue:
1) create a directory named test and deploy it under webapps
test
├── WEB-INF
│   └── web.xml
├── error.html
└── test.html

the web.xml is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0">
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>/test.html</url-pattern>
    </servlet-mapping>

    <error-page>
        <error-code>404</error-code>
        <location>/error.html</location>
    </error-page>
</web-app>
2 ) start tomcat and visit http://localhost:8080/test/abc.html

The exception is thrown as follows:

04-Nov-2015 22:24:04.984 SEVERE [http-nio-8080-exec-1] org.apache.catalina.core.StandardHostValve.custom Exception Processing                      ErrorPage[errorCode=404, location=/error.html]
 java.lang.NullPointerException
    at org.apache.catalina.core.StandardHostValve.custom(StandardHostValve.java:391)
    at org.apache.catalina.core.StandardHostValve.status(StandardHostValve.java:257)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:180)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:361)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:1057)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:75)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:737)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1531)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

The exception is not user-friendly, and somehow misleading. From the stack trace, user is likely to consider it as a tomcat bug. 

The solution might be adding url-pattern /error.html to the default servlet, or just change the url-pattern to match all the html resources.

My suggestion is to avoid such NPE and tell user what to do.

I have attached a proposed fix against trunk.
Comment 1 Mark Thomas 2015-11-06 15:15:04 UTC
Thanks for the report and the patch. I've applied the patch (with an additional fix that enabled thre default error page to be displayed if the custom page is not available) to trunk, 8.0.x (for 8.0.29) and 7.0.x (for 7.0.66).