Skip to content

Commit 56ddb84

Browse files
committed
fix(deletion-vector): reject a bitmap count larger than the payload
_deserialize_bitmap read an 8-byte count and used it directly as a loop bound, so a small blob could declare a large number of bitmaps. Reading past the payload surfaced as an IndexError from the native bitmap decoder. Validate the count against the remaining bytes, since every bitmap contributes at least a 4-byte key.
1 parent 07216ed commit 56ddb84

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

‎pyiceberg/table/deletion_vector.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ def _deserialize_bitmap(payload: bytes) -> list[BitMap]:
5757
offset = _ROARING_BITMAP_COUNT_SIZE_BYTES
5858
number_of_bitmaps = int.from_bytes(payload_view[:offset], byteorder="little")
5959

60+
# Every bitmap contributes at least a 4-byte key, so a count that cannot fit
61+
# in the remaining payload is invalid and must not be used as a loop bound.
62+
remaining = len(payload_view) - offset
63+
if number_of_bitmaps * _ROARING_BITMAP_KEY_SIZE_BYTES > remaining:
64+
raise ValueError(f"Payload declares {number_of_bitmaps} bitmaps, but only holds {remaining} bytes")
65+
6066
bitmaps = []
6167
last_key = -1
6268

‎tests/table/test_deletion_vector.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,15 @@ def serialize(self) -> bytes:
219219
assert DeletionVector._deserialize_bitmap(_bitmap_payload()) == [BitMap([1, 3, 5])]
220220

221221

222+
def test_map_declared_count_exceeds_payload() -> None:
223+
# A truncated payload that claims a large number of bitmaps must be rejected,
224+
# rather than driving the deserialization loop on data that is not there.
225+
puffin = (2**32).to_bytes(8, byteorder="little") + b"\x00\x00\x00\x00"
226+
227+
with pytest.raises(ValueError, match="Payload declares 4294967296 bitmaps, but only holds 4 bytes"):
228+
_ = DeletionVector._deserialize_bitmap(puffin)
229+
230+
222231
def test_map_high_vals() -> None:
223232
puffin = _open_file("64maphighvals.bin")
224233

0 commit comments

Comments
 (0)