Author: markt Date: Wed Nov 8 11:06:39 2017 New Revision: 1814567 URL: http://svn.apache.org/viewvc?rev=1814567&view=rev Log: Fix https://bz.apache.org/bugzilla/show_bug.cgi?id=61668 Refactor StringUtils to better handle null inputs. Add test cases for bug and all StringUtils methods.
Added: tomcat/trunk/test/org/apache/coyote/http11/TestAbstractHttp11Protocol.java (with props) tomcat/trunk/test/org/apache/tomcat/util/buf/TestStringUtils.java (with props) Modified: tomcat/trunk/java/org/apache/tomcat/util/buf/StringUtils.java tomcat/trunk/webapps/docs/changelog.xml Modified: tomcat/trunk/java/org/apache/tomcat/util/buf/StringUtils.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/buf/StringUtils.java?rev=1814567&r1=1814566&r2=1814567&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/util/buf/StringUtils.java (original) +++ tomcat/trunk/java/org/apache/tomcat/util/buf/StringUtils.java Wed Nov 8 11:06:39 2017 @@ -23,7 +23,8 @@ import java.util.function.Function; /** * Utility methods to build a separated list from a given set (not * java.util.Set) of inputs and return that list as a string or append it to an - * existing StringBuilder. + * existing StringBuilder. If the given set is null or empty, an empty string + * will be returned. */ public final class StringUtils { @@ -35,11 +36,17 @@ public final class StringUtils { public static String join(String[] array) { + if (array == null) { + return EMPTY_STRING; + } return join(Arrays.asList(array)); } public static void join(String[] array, char separator, StringBuilder sb) { + if (array == null) { + return; + } join(Arrays.asList(array), separator, sb); } @@ -51,7 +58,7 @@ public final class StringUtils { public static String join(Collection<String> collection, char separator) { // Shortcut - if (collection.isEmpty()) { + if (collection == null || collection.isEmpty()) { return EMPTY_STRING; } @@ -68,12 +75,18 @@ public final class StringUtils { public static <T> void join(T[] array, char separator, Function<T,String> function, StringBuilder sb) { + if (array == null) { + return; + } join(Arrays.asList(array), separator, function, sb); } public static <T> void join(Iterable<T> iterable, char separator, Function<T,String> function, StringBuilder sb) { + if (iterable == null) { + return; + } boolean first = true; for (T value : iterable) { if (first) { Added: tomcat/trunk/test/org/apache/coyote/http11/TestAbstractHttp11Protocol.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/coyote/http11/TestAbstractHttp11Protocol.java?rev=1814567&view=auto ============================================================================== --- tomcat/trunk/test/org/apache/coyote/http11/TestAbstractHttp11Protocol.java (added) +++ tomcat/trunk/test/org/apache/coyote/http11/TestAbstractHttp11Protocol.java Wed Nov 8 11:06:39 2017 @@ -0,0 +1,28 @@ +/* + * 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 org.apache.coyote.http11; + +import org.junit.Test; + +public class TestAbstractHttp11Protocol { + + @Test + public void testGetSslProtocol() { + Http11Nio2Protocol protocol = new Http11Nio2Protocol(); + protocol.getSSLProtocol(); + } +} Propchange: tomcat/trunk/test/org/apache/coyote/http11/TestAbstractHttp11Protocol.java ------------------------------------------------------------------------------ svn:eol-style = native Added: tomcat/trunk/test/org/apache/tomcat/util/buf/TestStringUtils.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/tomcat/util/buf/TestStringUtils.java?rev=1814567&view=auto ============================================================================== --- tomcat/trunk/test/org/apache/tomcat/util/buf/TestStringUtils.java (added) +++ tomcat/trunk/test/org/apache/tomcat/util/buf/TestStringUtils.java Wed Nov 8 11:06:39 2017 @@ -0,0 +1,77 @@ +/* + * 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 org.apache.tomcat.util.buf; + +import java.util.Collection; + +import org.junit.Assert; +import org.junit.Test; + +/* + * None of these tests should throw a NPE. + */ +public class TestStringUtils { + + @Test + public void testNullArray() { + Assert.assertEquals("", StringUtils.join((String[]) null)); + } + + + @Test + public void testNullArrayCharStringBuilder() { + StringBuilder sb = new StringBuilder(); + StringUtils.join((String[]) null, ',', sb); + Assert.assertEquals("", sb.toString()); + } + + + @Test + public void testNullCollection() { + Assert.assertEquals("", StringUtils.join((Collection<String>) null)); + } + + + @Test + public void testNullCollectionChar() { + Assert.assertEquals("", StringUtils.join(null, ',')); + } + + + @Test + public void testNullIterableCharStringBuilder() { + StringBuilder sb = new StringBuilder(); + StringUtils.join((Iterable<String>) null, ',', sb); + Assert.assertEquals("", sb.toString()); + } + + + @Test + public void testNullArrayCharFunctionStringBuilder() { + StringBuilder sb = new StringBuilder(); + StringUtils.join((String[]) null, ',', null, sb); + Assert.assertEquals("", sb.toString()); + } + + + @Test + public void testNullIterableCharFunctionStringBuilder() { + StringBuilder sb = new StringBuilder(); + StringUtils.join((Iterable<String>) null, ',', null, sb); + Assert.assertEquals("", sb.toString()); + } +} Propchange: tomcat/trunk/test/org/apache/tomcat/util/buf/TestStringUtils.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: tomcat/trunk/webapps/docs/changelog.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/changelog.xml?rev=1814567&r1=1814566&r2=1814567&view=diff ============================================================================== --- tomcat/trunk/webapps/docs/changelog.xml (original) +++ tomcat/trunk/webapps/docs/changelog.xml Wed Nov 8 11:06:39 2017 @@ -88,6 +88,10 @@ from 200 to 20. (remm) </fix> <fix> + <bug>61668</bug>: Avoid a possible NPE when calling + <code>AbstractHttp11Protocol.getSSLProtocol()</code>. (markt) + </fix> + <fix> <bug>61719</bug>: Avoid possible NPE calling InputStream.setReadListener with HTTP/2. (remm) </fix> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org