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 16:42:53 UTC

svn commit: r1576789 - in /hbase/branches/0.94/src: main/java/org/apache/hadoop/hbase/KeyValue.java test/java/org/apache/hadoop/hbase/TestSerialization.java

Author: apurtell
Date: Wed Mar 12 15:42:52 2014
New Revision: 1576789

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

Modified:
    hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/KeyValue.java
    hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/TestSerialization.java

Modified: hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/KeyValue.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/KeyValue.java?rev=1576789&r1=1576788&r2=1576789&view=diff
==============================================================================
--- hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/KeyValue.java (original)
+++ hbase/branches/0.94/src/main/java/org/apache/hadoop/hbase/KeyValue.java Wed Mar 12 15:42:52 2014
@@ -2254,6 +2254,11 @@ public class KeyValue implements Writabl
   // and it expects the length of the KeyValue to be explicitly passed
   // to it.
   public void readFields(int length, final DataInput in) throws IOException {
+
+    if (length <= 0) {
+      throw new IOException("Failed read " + length + " bytes, stream corrupt?");
+    }
+
     this.length = length;
     this.offset = 0;
     this.bytes = new byte[this.length];

Modified: hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/TestSerialization.java
URL: http://svn.apache.org/viewvc/hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/TestSerialization.java?rev=1576789&r1=1576788&r2=1576789&view=diff
==============================================================================
--- hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/TestSerialization.java (original)
+++ hbase/branches/0.94/src/test/java/org/apache/hadoop/hbase/TestSerialization.java Wed Mar 12 15:42:52 2014
@@ -22,7 +22,9 @@ package org.apache.hadoop.hbase;
 
 import static org.junit.Assert.*;
 
+import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
 import java.io.DataOutputStream;
 import java.io.IOException;
 import java.util.ArrayList;
@@ -89,6 +91,51 @@ public class TestSerialization {
     assertTrue(KeyValue.COMPARATOR.compare(original, newone) == 0);
   }
 
+  @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"));
+
+    KeyValue kv_i = new KeyValue();
+
+    ByteArrayOutputStream baos = new ByteArrayOutputStream();
+    DataOutputStream dos = new DataOutputStream(baos);
+
+    long l = 0;
+    try {
+      kv_0.write(dos);
+      l  = dos.size();
+      kv_1.write(dos);
+      l += dos.size();
+      assertEquals(151L, l);
+    } catch (IOException e) {
+      fail("Unexpected IOException" + e.getMessage());
+    }
+
+    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+    DataInputStream dis = new DataInputStream(bais);
+
+    try {
+      kv_i.readFields(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
+      kv_i.readFields(-1, dis);
+      fail("Expected corrupt stream");
+    } catch (Exception e) {
+      assertEquals("Failed read -1 bytes, stream corrupt?", e.getMessage());
+    }
+
+  }
+
   @SuppressWarnings("unchecked")
   @Test public void testHbaseMapWritable() throws Exception {
     HbaseMapWritable<byte [], byte []> hmw =