Bug 55454 - NullPointerException caused by invalid contentType, e.g. trailing semicolon
Summary: NullPointerException caused by invalid contentType, e.g. trailing semicolon
Status: RESOLVED FIXED
Alias: None
Product: Tomcat 7
Classification: Unclassified
Component: Catalina (show other bugs)
Version: 7.0.42
Hardware: PC All
: P2 normal (vote)
Target Milestone: ---
Assignee: Tomcat Developers Mailing List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-08-20 09:30 UTC by Christian
Modified: 2013-08-20 16:40 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Christian 2013-08-20 09:30:09 UTC
possible related to 53353

When having an invalid contenttype like:
<jsp:directive.page language="java" contentType="text/html;"/>
(note the trailing ";") Tomcat throws a NullPointerException like this:

java.lang.NullPointerException
	org.apache.tomcat.util.http.parser.HttpParser.parseMediaType(HttpParser.java:217)
	org.apache.tomcat.util.http.parser.MediaTypeCache.parse(MediaTypeCache.java:54)
	org.apache.catalina.connector.Response.setContentType(Response.java:805)
	org.apache.catalina.connector.ResponseFacade.setContentType(ResponseFacade.java:245)
	javax.servlet.ServletResponseWrapper.setContentType(ServletResponseWrapper.java:123)

After removing the extra ";" or entering a charset like this:
<jsp:directive.page language="java" contentType="text/html; charset=UTF-8"/>
it works as expected.

My desired behaviour would be:
Don't crash with a NPE.
Other Tomcat versions seem to handly this different.
Comment 1 Mark Thomas 2013-08-20 11:22:28 UTC
I'm unable to reproduce this with a unit test on Tomcat 7.0.x or 8.0.x nor with a JSP with 7.0.x. I have tried with the example provided and with various combinations of whitespace around the ';'.

If you are sure this issue affects that latest stable Tomcat 7 release please re-open this issue and attach the simplest possible JSP that demonstrates the issue on a clean Tomcat 7 install of the latest stable release.
Comment 2 Violeta Georgieva 2013-08-20 13:26:26 UTC
(In reply to Mark Thomas from comment #1)
> I'm unable to reproduce this with a unit test on Tomcat 7.0.x or 8.0.x nor
> with a JSP with 7.0.x. I have tried with the example provided and with
> various combinations of whitespace around the ';'.
> 
> If you are sure this issue affects that latest stable Tomcat 7 release
> please re-open this issue and attach the simplest possible JSP that
> demonstrates the issue on a clean Tomcat 7 install of the latest stable
> release.

Mark
I took the <tomcat7.0.x-trunk>/webapps/examples/jsp/jsp2/jspx/basic.jspx and change it like this <jsp:directive.page contentType="text/html;" />

Then I can reproduce the NPE.

org.apache.tomcat.util.http.parser.HttpParser.parseMediaType(StringReader) 

row -> 211 String attribute = readToken(input);

the attribute is null and the input is "text/html;;charset=UTF-8"
Comment 3 Violeta Georgieva 2013-08-20 13:54:11 UTC
What about if we do a change like this?

--- org/apache/tomcat/util/http/parser/HttpParser.java	(revision 1515761)
+++ org/apache/tomcat/util/http/parser/HttpParser.java	(working copy)
@@ -210,11 +210,13 @@
         while (lookForSemiColon == SkipConstantResult.FOUND) {
             String attribute = readToken(input);
 
+            String value = "";
             if (skipConstant(input, "=") == SkipConstantResult.FOUND) {
-                String value = readTokenOrQuotedString(input, true);
+                value = readTokenOrQuotedString(input, true);
+            }
+
+            if (attribute != null) {
                 parameters.put(attribute.toLowerCase(Locale.ENGLISH), value);
-            } else {
-                parameters.put(attribute.toLowerCase(Locale.ENGLISH), "");
             }
 
             lookForSemiColon = skipConstant(input, ";");
Comment 4 Mark Thomas 2013-08-20 14:00:41 UTC
Works for me.
Comment 5 Violeta Georgieva 2013-08-20 16:40:58 UTC
(In reply to Mark Thomas from comment #4)
> Works for me.
thanks


Fixed in trunk and 7.0.x and will be included in 7.0.43 onwards.