From f4923b98a613866901802795bcc731345b81b312 Mon Sep 17 00:00:00 2001 From: larsh Date: Sat, 27 Oct 2012 21:24:00 +0000 Subject: [PATCH] HBASE-6846 BitComparator bug - ArrayIndexOutOfBoundsException (Lucian George Iordache) git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1402882 13f79535-47bb-0310-9956-ffa450edef68 --- .../hadoop/hbase/filter/BitComparator.java | 2 +- .../hbase/filter/TestBitComparator.java | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/filter/BitComparator.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/filter/BitComparator.java index 77214845bf6..30bf1feae1c 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/filter/BitComparator.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/filter/BitComparator.java @@ -114,7 +114,7 @@ public class BitComparator extends ByteArrayComparable { } int b = 0; //Iterating backwards is faster because we can quit after one non-zero byte. - for (int i = value.length - 1; i >= 0 && b == 0; i--) { + for (int i = length - 1; i >= 0 && b == 0; i--) { switch (bitOperator) { case AND: b = (this.value[i] & value[i+offset]) & 0xff; diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/filter/TestBitComparator.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/filter/TestBitComparator.java index 302bd219f1a..423b7a57711 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/filter/TestBitComparator.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/filter/TestBitComparator.java @@ -33,6 +33,11 @@ public class TestBitComparator { private static byte[] data1 = new byte[]{15, 0, 0, 0, 0, 0}; private static byte[] data2 = new byte[]{0, 0, 0, 0, 0, 15}; private static byte[] data3 = new byte[]{15, 15, 15, 15, 15}; + + // data for testing compareTo method with offset and length parameters + private static byte[] data1_2 = new byte[]{15, 15, 0, 0, 0, 0, 0, 15}; + private static byte[] data2_2 = new byte[]{15, 0, 0, 0, 0, 0, 15, 15}; + private final int Equal = 0; private final int NotEqual = 1; @@ -69,5 +74,27 @@ public class TestBitComparator { assertEquals(comparator.compareTo(data), expected); } + @Test + public void testANDOperationWithOffset() { + testOperationWithOffset(data1_2, ones, BitComparator.BitwiseOp.AND, Equal); + testOperationWithOffset(data1_2, data0, BitComparator.BitwiseOp.AND, NotEqual); + testOperationWithOffset(data2_2, data1, BitComparator.BitwiseOp.AND, NotEqual); + } + + @Test + public void testOROperationWithOffset() { + testOperationWithOffset(data1_2, zeros, BitComparator.BitwiseOp.OR, Equal); + testOperationWithOffset(data2_2, data1, BitComparator.BitwiseOp.OR, Equal); + } + + @Test + public void testXOROperationWithOffset() { + testOperationWithOffset(data2_2, data1, BitComparator.BitwiseOp.XOR, Equal); + } + + private void testOperationWithOffset(byte[] data, byte[] comparatorBytes, BitComparator.BitwiseOp operator, int expected) { + BitComparator comparator = new BitComparator(comparatorBytes, operator); + assertEquals(comparator.compareTo(data, 1, comparatorBytes.length), expected); + } }