瀏覽代碼

ZOOKEEPER-268. tostring on jute generated objects can cause NPE. (pat via mahadev)

git-svn-id: https://svn.apache.org/repos/asf/hadoop/zookeeper/trunk@737784 13f79535-47bb-0310-9956-ffa450edef68
Mahadev Konar 16 年之前
父節點
當前提交
f340e569fe
共有 3 個文件被更改,包括 57 次插入11 次删除
  1. 3 0
      CHANGES.txt
  2. 16 11
      src/java/main/org/apache/jute/Utils.java
  3. 38 0
      src/java/test/org/apache/zookeeper/server/ToStringTest.java

+ 3 - 0
CHANGES.txt

@@ -68,6 +68,9 @@ BUGFIXES:
 
   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:
    

+ 16 - 11
src/java/main/org/apache/jute/Utils.java

@@ -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 @@ public class Utils {
         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 @@ public class Utils {
      * @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 @@ public class Utils {
      * @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 @@ public class Utils {
      * @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();
     }

+ 38 - 0
src/java/test/org/apache/zookeeper/server/ToStringTest.java

@@ -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());
+    }
+}