Author: mahadev Date: Mon Jan 26 19:21:38 2009 New Revision: 737784 URL: http://svn.apache.org/viewvc?rev=737784&view=rev Log: ZOOKEEPER-268. tostring on jute generated objects can cause NPE. (pat via mahadev)
Added: hadoop/zookeeper/trunk/src/java/test/org/apache/zookeeper/server/ToStringTest.java Modified: hadoop/zookeeper/trunk/CHANGES.txt hadoop/zookeeper/trunk/src/java/main/org/apache/jute/Utils.java Modified: hadoop/zookeeper/trunk/CHANGES.txt URL: http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/CHANGES.txt?rev=737784&r1=737783&r2=737784&view=diff ============================================================================== --- hadoop/zookeeper/trunk/CHANGES.txt (original) +++ hadoop/zookeeper/trunk/CHANGES.txt Mon Jan 26 19:21:38 2009 @@ -68,6 +68,9 @@ ZOOKEEPER-273. Zookeeper c client build should not depend on CPPUNIT. (pat and runping via mahadev) + + ZOOKEEPER-268. tostring on jute generated objects can cause NPE. (pat via +mahadev) IMPROVEMENTS: Modified: hadoop/zookeeper/trunk/src/java/main/org/apache/jute/Utils.java URL: http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/src/java/main/org/apache/jute/Utils.java?rev=737784&r1=737783&r2=737784&view=diff ============================================================================== --- hadoop/zookeeper/trunk/src/java/main/org/apache/jute/Utils.java (original) +++ hadoop/zookeeper/trunk/src/java/main/org/apache/jute/Utils.java Mon Jan 26 19:21:38 2009 @@ -19,20 +19,17 @@ package org.apache.jute; import java.io.ByteArrayOutputStream; -import java.io.DataInput; -import java.io.DataOutput; import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.nio.charset.CharacterCodingException; /** - * Various utility functions for Hadooop record I/O runtime. + * Various utility functions for Hadoop record I/O runtime. * @author Milind Bhandarkar */ public class Utils { /** Cannot create a new instance of Utils */ private Utils() { + super(); } /** @@ -56,7 +53,7 @@ return true; } - public static final char[] hexchars = { '0', '1', '2', '3', '4', '5', + private static final char[] hexchars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; /** @@ -64,8 +61,10 @@ * @param s * @return */ - static String toXMLString(String t) { - String s = t.toString(); + static String toXMLString(String s) { + if (s == null) + return ""; + StringBuffer sb = new StringBuffer(); for (int idx = 0; idx < s.length(); idx++) { char ch = s.charAt(idx); @@ -197,9 +196,12 @@ * @return */ static String toXMLBuffer(byte barr[]) { + if (barr == null || barr.length == 0) { + return ""; + } StringBuffer sb = new StringBuffer(2*barr.length); for (int idx = 0; idx < barr.length; idx++) { - sb.append(Integer.toHexString((int)barr[idx])); + sb.append(Integer.toHexString(barr[idx])); } return sb.toString(); } @@ -231,10 +233,13 @@ * @return */ static String toCSVBuffer(byte barr[]) { - StringBuffer sb = new StringBuffer(barr.length+1); + if (barr == null || barr.length == 0) { + return ""; + } + StringBuffer sb = new StringBuffer(barr.length + 1); sb.append('#'); for(int idx = 0; idx < barr.length; idx++) { - sb.append(Integer.toHexString((int)barr[idx])); + sb.append(Integer.toHexString(barr[idx])); } return sb.toString(); } Added: hadoop/zookeeper/trunk/src/java/test/org/apache/zookeeper/server/ToStringTest.java URL: http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/src/java/test/org/apache/zookeeper/server/ToStringTest.java?rev=737784&view=auto ============================================================================== --- hadoop/zookeeper/trunk/src/java/test/org/apache/zookeeper/server/ToStringTest.java (added) +++ hadoop/zookeeper/trunk/src/java/test/org/apache/zookeeper/server/ToStringTest.java Mon Jan 26 19:21:38 2009 @@ -0,0 +1,38 @@ +/** + * 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.zookeeper.server; + +import junit.framework.TestCase; + +import org.apache.zookeeper.proto.SetDataRequest; +import org.junit.Test; + +/** + * A misc place to verify toString methods - mainly to make sure they don't + * fail. + */ +public class ToStringTest extends TestCase { + /** Verify jute - which we've had particular problems with in the past + * wrt null fields */ + @Test + public void testJuteToString() { + SetDataRequest req = new SetDataRequest(null, null, 0); + assertNotSame("ERROR", req.toString()); + } +}