You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by ap...@apache.org on 2014/03/12 15:27:11 UTC

svn commit: r1576749 - in /hbase/branches/0.98: hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValue.java hbase-server/src/test/java/org/apache/hadoop/hbase/TestSerialization.java

Author: apurtell
Date: Wed Mar 12 14:27:11 2014
New Revision: 1576749

URL: http://svn.apache.org/r1576749
Log:
HBASE-10718 TestHLogSplit fails when it sets a KV size to be negative (Esteban Gutierrez)

Modified:
    hbase/branches/0.98/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValue.java
    hbase/branches/0.98/hbase-server/src/test/java/org/apache/hadoop/hbase/TestSerialization.java

Modified: hbase/branches/0.98/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValue.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.98/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValue.java?rev=1576749&r1=1576748&r2=1576749&view=diff
==============================================================================
--- hbase/branches/0.98/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValue.java (original)
+++ hbase/branches/0.98/hbase-common/src/main/java/org/apache/hadoop/hbase/KeyValue.java Wed Mar 12 14:27:11 2014
@@ -2759,7 +2759,12 @@ public class KeyValue implements Cell, H
    * @throws IOException
    */
   public static KeyValue create(int length, final DataInput in) throws IOException {
-    if (length == 0) return null;
+
+    if (length <= 0) {
+      if (length == 0) return null;
+      throw new IOException("Failed read " + length + " bytes, stream corrupt?");
+    }
+
     // This is how the old Writables.readFrom used to deserialize.  Didn't even vint.
     byte [] bytes = new byte[length];
     in.readFully(bytes);

Modified: hbase/branches/0.98/hbase-server/src/test/java/org/apache/hadoop/hbase/TestSerialization.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.98/hbase-server/src/test/java/org/apache/hadoop/hbase/TestSerialization.java?rev=1576749&r1=1576748&r2=1576749&view=diff
==============================================================================
--- hbase/branches/0.98/hbase-server/src/test/java/org/apache/hadoop/hbase/TestSerialization.java (original)
+++ hbase/branches/0.98/hbase-server/src/test/java/org/apache/hadoop/hbase/TestSerialization.java Wed Mar 12 14:27:11 2014
@@ -22,11 +22,13 @@ package org.apache.hadoop.hbase;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.DataInputStream;
 import java.io.DataOutputStream;
+import java.io.IOException;
 import java.util.List;
 import java.util.Map;
 import java.util.NavigableSet;
@@ -74,7 +76,48 @@ public class TestSerialization {
     assertEquals(kv.getOffset(), deserializedKv.getOffset());
     assertEquals(kv.getLength(), deserializedKv.getLength());
   }
-  
+
+  @Test public void testCreateKeyValueInvalidNegativeLength() {
+
+    KeyValue kv_0 = new KeyValue(Bytes.toBytes("myRow"), Bytes.toBytes("myCF"),       // 51 bytes
+                                 Bytes.toBytes("myQualifier"), 12345L, Bytes.toBytes("my12345"));
+
+    KeyValue kv_1 = new KeyValue(Bytes.toBytes("myRow"), Bytes.toBytes("myCF"),       // 49 bytes
+                                 Bytes.toBytes("myQualifier"), 12345L, Bytes.toBytes("my123"));
+
+    ByteArrayOutputStream baos = new ByteArrayOutputStream();
+    DataOutputStream dos = new DataOutputStream(baos);
+
+    long l = 0;
+    try {
+      l  = KeyValue.oswrite(kv_0, dos, false);
+      l += KeyValue.oswrite(kv_1, dos, false);
+      assertEquals(100L, l);
+    } catch (IOException e) {
+      fail("Unexpected IOException" + e.getMessage());
+    }
+
+    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+    DataInputStream dis = new DataInputStream(bais);
+
+    try {
+      KeyValue.create(dis);
+      assertTrue(kv_0.equals(kv_1));
+    } catch (Exception e) {
+      fail("Unexpected Exception" + e.getMessage());
+    }
+
+    // length -1
+    try {
+      // even if we have a good kv now in dis we will just pass length with -1 for simplicity
+      KeyValue.create(-1, dis);
+      fail("Expected corrupt stream");
+    } catch (Exception e) {
+      assertEquals("Failed read -1 bytes, stream corrupt?", e.getMessage());
+    }
+
+  }
+
   @Test
   public void testSplitLogTask() throws DeserializationException {
     SplitLogTask slt = new SplitLogTask.Unassigned(ServerName.valueOf("mgr,1,1"));