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
This commit is contained in:
larsh 2012-10-27 21:24:00 +00:00
parent cd572001f0
commit f4923b98a6
2 changed files with 28 additions and 1 deletions

View File

@ -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;

View File

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