Skip to content

Commit 52d1dc0

Browse files
comnetworkApache9
authored andcommitted
HBASE-26344 Fix Bug for MultiByteBuff.put(int, byte) (#3741)
Signed-off-by: Duo Zhang <[email protected]>
1 parent c42d6cc commit 52d1dc0

File tree

3 files changed

+100
-24
lines changed

3 files changed

+100
-24
lines changed

hbase-common/src/main/java/org/apache/hadoop/hbase/nio/MultiByteBuff.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ public byte getByteAfterPosition(int offset) {
181181
* Returns in which sub ByteBuffer, the given element index will be available.
182182
*/
183183
private int getItemIndex(int elemIndex) {
184+
if (elemIndex < 0) {
185+
throw new IndexOutOfBoundsException();
186+
}
184187
int index = 1;
185188
while (elemIndex >= this.itemBeginPos[index]) {
186189
index++;
@@ -721,15 +724,16 @@ public MultiByteBuff put(byte b) {
721724
}
722725

723726
/**
724-
* Writes a byte to this MBB at the given index
725-
* @param index
726-
* @param b
727+
* Writes a byte to this MBB at the given index and won't affect the position of any of the
728+
* buffers.
727729
* @return this object
730+
* @throws IndexOutOfBoundsException If <tt>index</tt> is negative or not smaller than the
731+
* {@link MultiByteBuff#limit}
728732
*/
729733
@Override
730734
public MultiByteBuff put(int index, byte b) {
731735
checkRefCount();
732-
int itemIndex = getItemIndex(limit);
736+
int itemIndex = getItemIndex(index);
733737
ByteBuffer item = items[itemIndex];
734738
item.put(index - itemBeginPos[itemIndex], b);
735739
return this;

hbase-common/src/test/java/org/apache/hadoop/hbase/nio/TestMultiByteBuff.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,4 +594,58 @@ private void doTestPositionalPutByteBuff(ByteBuff srcByteBuff) throws Exception
594594
assertTrue(e != null);
595595
}
596596
}
597+
598+
@Test
599+
public void testPositionalPutByte() throws Exception {
600+
ByteBuffer bb1 = ByteBuffer.allocate(50);
601+
ByteBuffer bb2 = ByteBuffer.allocate(50);
602+
ByteBuffer bb3 = ByteBuffer.allocate(50);
603+
ByteBuffer bb4 = ByteBuffer.allocate(50);
604+
MultiByteBuff srcMultiByteBuff = new MultiByteBuff(bb1, bb2, bb3, bb4);
605+
for (int i = 1; i <= 200; i++) {
606+
srcMultiByteBuff.put((byte) 0xff);
607+
}
608+
609+
srcMultiByteBuff.put(20, (byte) 0);
610+
byte val = srcMultiByteBuff.get(20);
611+
assertTrue(val == 0);
612+
613+
srcMultiByteBuff.put(50, (byte) 0);
614+
val = srcMultiByteBuff.get(50);
615+
assertTrue(val == 0);
616+
617+
srcMultiByteBuff.put(80, (byte) 0);
618+
val = srcMultiByteBuff.get(80);
619+
assertTrue(val == 0);
620+
621+
srcMultiByteBuff.put(100, (byte) 0);
622+
val = srcMultiByteBuff.get(100);
623+
assertTrue(val == 0);
624+
625+
srcMultiByteBuff.put(121, (byte) 0);
626+
val = srcMultiByteBuff.get(121);
627+
assertTrue(val == 0);
628+
629+
srcMultiByteBuff.put(150, (byte) 0);
630+
val = srcMultiByteBuff.get(150);
631+
assertTrue(val == 0);
632+
633+
srcMultiByteBuff.put(180, (byte) 0);
634+
val = srcMultiByteBuff.get(180);
635+
assertTrue(val == 0);
636+
637+
try {
638+
srcMultiByteBuff.put(200, (byte) 0);
639+
fail();
640+
} catch (IndexOutOfBoundsException e) {
641+
assertTrue(e != null);
642+
}
643+
644+
try {
645+
srcMultiByteBuff.put(260, (byte) 0);
646+
fail();
647+
} catch (IndexOutOfBoundsException e) {
648+
assertTrue(e != null);
649+
}
650+
}
597651
}

hbase-common/src/test/java/org/apache/hadoop/hbase/util/TestOrderedBytes.java

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,26 +1263,44 @@ public void testEncodedValueCheck() {
12631263
int cnt = 0;
12641264
PositionedByteRange buff = new SimplePositionedMutableByteRange(1024);
12651265
for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
1266-
int o;
1267-
o = OrderedBytes.encodeNull(buff, ord); cnt++;
1268-
o = OrderedBytes.encodeNumeric(buff, negInf, ord); cnt++;
1269-
o = OrderedBytes.encodeNumeric(buff, negLarge, ord); cnt++;
1270-
o = OrderedBytes.encodeNumeric(buff, negMed, ord); cnt++;
1271-
o = OrderedBytes.encodeNumeric(buff, negSmall, ord); cnt++;
1272-
o = OrderedBytes.encodeNumeric(buff, zero, ord); cnt++;
1273-
o = OrderedBytes.encodeNumeric(buff, posSmall, ord); cnt++;
1274-
o = OrderedBytes.encodeNumeric(buff, posMed, ord); cnt++;
1275-
o = OrderedBytes.encodeNumeric(buff, posLarge, ord); cnt++;
1276-
o = OrderedBytes.encodeNumeric(buff, posInf, ord); cnt++;
1277-
o = OrderedBytes.encodeNumeric(buff, nan, ord); cnt++;
1278-
o = OrderedBytes.encodeInt8(buff, int8, ord); cnt++;
1279-
o = OrderedBytes.encodeInt16(buff, int16, ord); cnt++;
1280-
o = OrderedBytes.encodeInt32(buff, int32, ord); cnt++;
1281-
o = OrderedBytes.encodeInt64(buff, int64, ord); cnt++;
1282-
o = OrderedBytes.encodeFloat32(buff, float32, ord); cnt++;
1283-
o = OrderedBytes.encodeFloat64(buff, float64, ord); cnt++;
1284-
o = OrderedBytes.encodeString(buff, text, ord); cnt++;
1285-
o = OrderedBytes.encodeBlobVar(buff, blobVar, ord); cnt++;
1266+
OrderedBytes.encodeNull(buff, ord);
1267+
cnt++;
1268+
OrderedBytes.encodeNumeric(buff, negInf, ord);
1269+
cnt++;
1270+
OrderedBytes.encodeNumeric(buff, negLarge, ord);
1271+
cnt++;
1272+
OrderedBytes.encodeNumeric(buff, negMed, ord);
1273+
cnt++;
1274+
OrderedBytes.encodeNumeric(buff, negSmall, ord);
1275+
cnt++;
1276+
OrderedBytes.encodeNumeric(buff, zero, ord);
1277+
cnt++;
1278+
OrderedBytes.encodeNumeric(buff, posSmall, ord);
1279+
cnt++;
1280+
OrderedBytes.encodeNumeric(buff, posMed, ord);
1281+
cnt++;
1282+
OrderedBytes.encodeNumeric(buff, posLarge, ord);
1283+
cnt++;
1284+
OrderedBytes.encodeNumeric(buff, posInf, ord);
1285+
cnt++;
1286+
OrderedBytes.encodeNumeric(buff, nan, ord);
1287+
cnt++;
1288+
OrderedBytes.encodeInt8(buff, int8, ord);
1289+
cnt++;
1290+
OrderedBytes.encodeInt16(buff, int16, ord);
1291+
cnt++;
1292+
OrderedBytes.encodeInt32(buff, int32, ord);
1293+
cnt++;
1294+
OrderedBytes.encodeInt64(buff, int64, ord);
1295+
cnt++;
1296+
OrderedBytes.encodeFloat32(buff, float32, ord);
1297+
cnt++;
1298+
OrderedBytes.encodeFloat64(buff, float64, ord);
1299+
cnt++;
1300+
OrderedBytes.encodeString(buff, text, ord);
1301+
cnt++;
1302+
OrderedBytes.encodeBlobVar(buff, blobVar, ord);
1303+
cnt++;
12861304
}
12871305

12881306
buff.setPosition(0);

0 commit comments

Comments
 (0)