Skip to content

Commit f44e97b

Browse files
committed
ensure truncated bitmap size isn't larger than it should be
1 parent 7ac081a commit f44e97b

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

js/src/util/bit.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,19 @@ export function truncateBitmap(offset: number, length: number, bitmap: Uint8Arra
3737
const alignedSize = (bitmap.byteLength + 7) & ~7;
3838
if (offset > 0 || bitmap.byteLength < alignedSize) {
3939
const bytes = new Uint8Array(alignedSize);
40-
bytes.set((offset % 8 === 0)
41-
// If the offset is a multiple of 8 bits, it's safe to slice the bitmap
42-
? bitmap.subarray(offset >> 3)
40+
// If the offset is a multiple of 8 bits, it's safe to slice the bitmap
41+
bytes.set(offset % 8 === 0 ? bitmap.subarray(offset >> 3) :
4342
// Otherwise iterate each bit from the offset and return a new one
44-
: packBools(iterateBits(bitmap, offset, length, null, getBool)));
43+
packBools(iterateBits(bitmap, offset, length, null, getBool)).subarray(0, alignedSize));
4544
return bytes;
4645
}
4746
return bitmap;
4847
}
4948

5049
/** @ignore */
5150
export function packBools(values: Iterable<any>) {
52-
let n = 0, i = 0;
5351
let xs: number[] = [];
54-
let bit = 0, byte = 0;
52+
let i = 0, bit = 0, byte = 0;
5553
for (const value of values) {
5654
value && (byte |= 1 << bit);
5755
if (++bit === 8) {
@@ -60,10 +58,9 @@ export function packBools(values: Iterable<any>) {
6058
}
6159
}
6260
if (i === 0 || bit > 0) { xs[i++] = byte; }
63-
if (i % 8 && (n = i + 8 - i % 8)) {
64-
do { xs[i] = 0; } while (++i < n);
65-
}
66-
return new Uint8Array(xs);
61+
let b = new Uint8Array((xs.length + 7) & ~7);
62+
b.set(xs);
63+
return b;
6764
}
6865

6966
/** @ignore */

0 commit comments

Comments
 (0)