Bug 58581 - StandardHostValve#custom throws NPE if custom error page is incorrectly configured
StandardHostValve#custom throws NPE if custom error page is incorrectly confi...
Product: Tomcat 9
Classification: Unclassified
Component: Catalina
All All
: P2 normal (vote)
: -----
Assigned To: Tomcat Developers Mailing List
Depends on:
  Show dependency tree
Reported: 2015-11-04 14:35 UTC by Huxing Zhang
Modified: 2015-11-06 15:15 UTC (History)
0 users

proposed fix to avoid NPE (1.96 KB, patch)
2015-11-04 14:35 UTC, Huxing Zhang
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
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
│   └── web.xml
├── error.html
└── test.html

the web.xml is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0">

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]
    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).