Bug 53411 - NullPointerException in org.apache.tomcat.util.buf.CharChunk
Summary: NullPointerException in org.apache.tomcat.util.buf.CharChunk
Status: RESOLVED FIXED
Alias: None
Product: Tomcat 7
Classification: Unclassified
Component: Catalina (show other bugs)
Version: 7.0.27
Hardware: Other Linux
: P2 enhancement (vote)
Target Milestone: ---
Assignee: Tomcat Developers Mailing List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-06-13 16:22 UTC by kubak
Modified: 2018-06-28 12:14 UTC (History)
1 user (show)



Attachments
Patch for the enhancements mentioned above (1.87 KB, patch)
2014-10-22 14:58 UTC, Hariharan, R
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description kubak 2012-06-13 16:22:07 UTC
I can see in my log file a regular exception throwed by AbstractHttp11Processor, with this stack trace:

org.apache.coyote.http11.AbstractHttp11Processor process
Error processing request
java.lang.NullPointerException
        at org.apache.tomcat.util.buf.CharChunk.append(CharChunk.java:355)
        at org.apache.tomcat.util.http.mapper.Mapper.map(Mapper.java:667)
        at org.apache.catalina.connector.CoyoteAdapter.postParseRequest(CoyoteAdapter.java:646)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:402)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
        at org.apache.tomcat.util.net.AprEndpoint$SocketWithOptionsProcessor.run(AprEndpoint.java:1770)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
        at java.lang.Thread.run(Thread.java:722)


I cannot find the request which makes this exception, it's rather rare, but still, I think it's worth to look at.
Comment 1 Mark Thomas 2012-06-13 17:43:56 UTC
The NPE is triggered by the default host name being null. Such a configuration is invalid. Please follow up on the users list for assistance.
Comment 2 kubak 2012-06-13 17:58:58 UTC
Well, I think you've changed the status too early.

My Apache Tomcat 7 server is running, all web apps are working and defaultHost parameter in server.xml is set to "localhost":  <Engine name="Catalina" defaultHost="localhost">

So, this exception doesn't stop tomcat or any context, I don't even noticed if this exception is visible for any user. From the user point of view everything works fine.
I've just noticed this strange stack trace in the log file and this is the only one exception which I can find there - that's why it caught my attention. 


Even if the problem is in the configuration - Tomcat should not start or should give a clear message during the startup, in my opinion.
Comment 3 Mark Thomas 2012-06-13 19:37:13 UTC
There is insufficient information provided to reproduce the issue. The only information provided - the stack trace - points to a configuration problem. 

Please do not re-open this issue without providing either:
- sufficient information for it to be produced
- analysis that identifies a code path that code trigger this issue

The users list is the place to seek help with obtaining either of the above.

A warning is already logged if the default host cannot be identified.
Comment 4 Konstantin Kolinko 2012-06-13 20:49:32 UTC
Just for reference, confirming what Mark already wrote,
in tc7.0.x/tags/TOMCAT_7_0_27

CharChunk.java line 355
[[[
354 	public void append(String s) throws IOException {
355 	append(s, 0, s.length());
356 	}
]]]

So "s" is null.

Mapper.java line 667
[[[
666 	if (host.isNull()) {
667 	host.getCharChunk().append(defaultHostName);
668 	}
]]]

So "defaultHostName" is null.

The "if(host.isNull())" branch is executed when it is an HTTP/1.0 request that does not have a "Host" header. Such requests are rare nowadays.


(In reply to comment #2)
> <Engine name="Catalina" defaultHost="localhost">

There must exist <Host name="localhost" > for that. If it does not, the defaultHost value will be ignored.

MapperListener.java
[[[
        if(found) {
            mapper.setDefaultHostName(defaultHost);
        } else {
            log.warn(sm.getString("mapperListener.unknownDefaultHost",
                    defaultHost, connector));
        }
]]]
Comment 5 Konstantin Kolinko 2012-06-13 21:55:10 UTC
Reopening this as enhancement.

Looking at AbstractHttp11Processor#process() this error is correctly written to access log, etc, so I do not see much concern.

Still I think there is a room for improvement, and I noted several minor issues from my code review.


1) in MapperListener#findDefaultHost()

First, maybe 
s/log.warn(sm.getString("mapperListener.unknownDefaultHost",/log.error/ 

Second, maybe mention something like " Tomcat will not be able to process HTTP/1.0 requests that do not specify a Host header" in the message.

Third, I am a bit wondering why not to call mapper.setDefaultHost() unconditionally. What is wrong with passing a name there?

Hosts can be added and removed through JMX calls on StandardEngine#addChild()/removeChild() and it seems that MapperListener fails to update defaultHost setting on the Mapper when it happens.

So why not to pass the defaultHost name to the Mapper as is and let it handle missing matches (like it already does)?


2) in Mapper#map(MB,MB,S,MD)

Fourth,
Maybe just return without mapping here, as if the Host is not found. We already do if(defaultHostName==null){ return; } in its #internalMap(CC,CC,S,MD) method.

It will need some update to its caller though, which is
CoyoteAdapter#postParseRequest()


3) In CoyoteAdapter#postParseRequest()

This request could be rejected with error 404, instead of 400 that exception handling in AbstractHttp11Processor#process() does. The postParseRequest() already has code for handling it as 404, but

Fifth,
access logging needs to be changed a bit. The current code:

[[[
                // Make sure there is a host (might not be during shutdown)
                if (host != null) {
                    host.logAccess(request, response, 0, true);
                }
]]]

I think that it should fallback to CoyoteAdapter#log(..) when host is null.
Comment 6 kubak 2012-06-14 13:24:25 UTC
(In reply to comment #5)
> Reopening this as enhancement.
> 
> Looking at AbstractHttp11Processor#process() this error is correctly written
> to access log, etc, so I do not see much concern.
> 
> Still I think there is a room for improvement, and I noted several minor
> issues from my code review.
> 
> 
> 1) in MapperListener#findDefaultHost()
> 
> First, maybe 
> s/log.warn(sm.getString("mapperListener.unknownDefaultHost",/log.error/ 
> 
> Second, maybe mention something like " Tomcat will not be able to process
> HTTP/1.0 requests that do not specify a Host header" in the message.
> 
> Third, I am a bit wondering why not to call mapper.setDefaultHost()
> unconditionally. What is wrong with passing a name there?
> 
> Hosts can be added and removed through JMX calls on
> StandardEngine#addChild()/removeChild() and it seems that MapperListener
> fails to update defaultHost setting on the Mapper when it happens.
> 
> So why not to pass the defaultHost name to the Mapper as is and let it
> handle missing matches (like it already does)?
> 
> 
> 2) in Mapper#map(MB,MB,S,MD)
> 
> Fourth,
> Maybe just return without mapping here, as if the Host is not found. We
> already do if(defaultHostName==null){ return; } in its
> #internalMap(CC,CC,S,MD) method.
> 
> It will need some update to its caller though, which is
> CoyoteAdapter#postParseRequest()
> 
> 
> 3) In CoyoteAdapter#postParseRequest()
> 
> This request could be rejected with error 404, instead of 400 that exception
> handling in AbstractHttp11Processor#process() does. The postParseRequest()
> already has code for handling it as 404, but
> 
> Fifth,
> access logging needs to be changed a bit. The current code:
> 
> [[[
>                 // Make sure there is a host (might not be during shutdown)
>                 if (host != null) {
>                     host.logAccess(request, response, 0, true);
>                 }
> ]]]
> 
> I think that it should fallback to CoyoteAdapter#log(..) when host is null.



In my opinion if Tomcat cannot process some requests because of wrong configuration - Tomcat should not start and give an error with clear message at start up. Current message doesn't say anything about after-effects, is easy to ignore.
Comment 7 Hariharan, R 2014-10-22 14:58:52 UTC
Created attachment 32138 [details]
Patch for the enhancements mentioned above

I have incorporated the changes mentioned above, with this patch.

P.S. This is my first patch submission. Let me know if I could do anything to help better.
Comment 8 Mark Thomas 2014-10-22 19:46:51 UTC
Thanks for the patch. Don't be concerned about how much of the patch I am suggesting you change (most of it). The first patch I proposed was torn apart beyond recognition before it got anywhere near the Tomcat code base.

The view expressed by Kubak that Tomcat should not start if the default host is not valid is not the consensus opinion of the Tomcat developers. It is safe to assume that Konstatin's comments (as a committer) does represent the consensus opinion unless another committer comments otherwise (in which case expect a discussion on the dev list to reach a consensus).

I suggest you go through Konstantin's comments in comment #5 one by one and address each of them in your patch.
Comment 9 VERISHAL 2018-05-02 05:43:03 UTC
Hello Last 3 day's i'm facing ths issue in Apache server  . I can see in my log file a regular exception throwed by AbstractHttp11Processor, with this stack trace: i need help how can resolve this error . please suggest me
*NullPointerException in org.apache.tomcat.util.buf.CharChunk*
org.apache.coyote.http11.AbstractHttp11Processor process
Error processing request
java.lang.NullPointerException
      at org.apache.tomcat.util.buf.CharChunk.append(CharChunk.java:355)
      at org.apache.tomcat.util.http.mapper.Mapper.map(Mapper.java:667)
      at org.apache.catalina.connector.CoyoteAdapter.postParseRequest(CoyoteAdapter.java:646)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:402)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
      at org.apache.tomcat.util.net.AprEndpoint$SocketWithOptionsProcessor.run(AprEndpoint.java:1770)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
      at java.lang.Thread.run(Thread.java:722)
                                       
               Any one please help me
Comment 10 VERISHAL 2018-05-02 05:50:17 UTC
Hello Last 3 day's i'm facing ths issue in Apache server  . I can see in my log file a regular exception throwed by AbstractHttp11Processor, with this stack trace: i need help how can resolve this error . please suggest me
*NullPointerException in org.apache.tomcat.util.buf.CharChunk*
org.apache.coyote.http11.AbstractHttp11Processor process
Error processing request
java.lang.NullPointerException
      at org.apache.tomcat.util.buf.CharChunk.append(CharChunk.java:355)
      at org.apache.tomcat.util.http.mapper.Mapper.map(Mapper.java:667)
      at org.apache.catalina.connector.CoyoteAdapter.postParseRequest(CoyoteAdapter.java:646)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:402)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
      at org.apache.tomcat.util.net.AprEndpoint$SocketWithOptionsProcessor.run(AprEndpoint.java:1770)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
      at java.lang.Thread.run(Thread.java:722)
                                       
               Any one please help me
Comment 11 Mark Thomas 2018-06-28 12:14:33 UTC
The improvements suggested in comment #5 have been implemented on 9.0.x and will be included in 9.0.11 onwards.