Fix IndexOutOfBounds when reading a multiple of 8 number of bits
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=163455269
This commit is contained in:
parent
5bd5af8c60
commit
bb04456324
@ -87,9 +87,13 @@ public final class ParsableBitArrayTest extends TestCase {
|
|||||||
result[1] = 0;
|
result[1] = 0;
|
||||||
testArray.readBits(result, 1, 0);
|
testArray.readBits(result, 1, 0);
|
||||||
assertEquals(0, result[1]);
|
assertEquals(0, result[1]);
|
||||||
|
// Test reading a number of bits divisible by 8.
|
||||||
|
testArray.setPosition(0);
|
||||||
|
testArray.readBits(result, 0, 16);
|
||||||
|
assertEquals(TEST_DATA[0], result[0]);
|
||||||
|
assertEquals(TEST_DATA[1], result[1]);
|
||||||
// Test least significant bits are unmodified.
|
// Test least significant bits are unmodified.
|
||||||
result[1] = (byte) 0xFF;
|
result[1] = (byte) 0xFF;
|
||||||
testArray.setPosition(16);
|
|
||||||
testArray.readBits(result, 0, 9);
|
testArray.readBits(result, 0, 9);
|
||||||
assertEquals(0x5F, result[0]);
|
assertEquals(0x5F, result[0]);
|
||||||
assertEquals(0x7F, result[1]);
|
assertEquals(0x7F, result[1]);
|
||||||
|
@ -192,6 +192,9 @@ public final class ParsableBitArray {
|
|||||||
}
|
}
|
||||||
// Trailing bits.
|
// Trailing bits.
|
||||||
int bitsLeft = numBits & 7 /* numBits % 8 */;
|
int bitsLeft = numBits & 7 /* numBits % 8 */;
|
||||||
|
if (bitsLeft == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
buffer[to] &= 0xFF >> bitsLeft; // Set to 0 the bits that are going to be overwritten.
|
buffer[to] &= 0xFF >> bitsLeft; // Set to 0 the bits that are going to be overwritten.
|
||||||
if (bitOffset + bitsLeft > 8) {
|
if (bitOffset + bitsLeft > 8) {
|
||||||
// We read the rest of data[byteOffset] and increase byteOffset.
|
// We read the rest of data[byteOffset] and increase byteOffset.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user