This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit de0ef51fe8415988ad64549c877121eca829dbba Author: Mark Thomas <ma...@apache.org> AuthorDate: Mon Jun 24 17:39:37 2024 +0100 Fix possible NPE --- java/jakarta/servlet/ServletResponse.java | 6 ++++- test/jakarta/servlet/TestServletResponse.java | 36 +++++++++++++++++++++++++++ webapps/docs/changelog.xml | 6 +++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/java/jakarta/servlet/ServletResponse.java b/java/jakarta/servlet/ServletResponse.java index 0f55fc3fc8..2935289882 100644 --- a/java/jakarta/servlet/ServletResponse.java +++ b/java/jakarta/servlet/ServletResponse.java @@ -178,7 +178,11 @@ public interface ServletResponse { * @since Servlet 6.1 */ default void setCharacterEncoding(Charset encoding) { - setCharacterEncoding(encoding.name()); + if (encoding == null) { + setCharacterEncoding((String) null); + } else { + setCharacterEncoding(encoding.name()); + } } /** diff --git a/test/jakarta/servlet/TestServletResponse.java b/test/jakarta/servlet/TestServletResponse.java new file mode 100644 index 0000000000..b7475c7753 --- /dev/null +++ b/test/jakarta/servlet/TestServletResponse.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package jakarta.servlet; + +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; + +import org.junit.Test; + +import org.apache.catalina.filters.TesterHttpServletResponse; + +public class TestServletResponse { + + @Test + public void testSetCharacterEncodingNull() throws Exception { + ServletResponse servletResponse = new TesterHttpServletResponse(); + + servletResponse.setCharacterEncoding(StandardCharsets.UTF_8); + // Should not fail with an NPE + servletResponse.setCharacterEncoding((Charset) null); + } +} \ No newline at end of file diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index 40650c9bf4..97770e93e4 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -111,6 +111,12 @@ Allow <code>JAASRealm</code> to use the configuration source to load a configured <code>configFile</code>, for easier use with testing. (remm) </fix> + <fix> + Fix a potential <code>NullPointerException</code> in classes that extend + <code>ServletResponse</code> when + <code>setCharacterEncoding(Charset)</code> is called with + <code>null</code>. (markt) + </fix> </changelog> </subsection> <subsection name="Coyote"> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org