From 56ddb84e4c8d1c062f98d7bd3013677f369dd69a Mon Sep 17 00:00:00 2001 From: ghoshp83 Date: Thu, 17 Sep 2026 20:19:56 +0100 Subject: [PATCH] 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. --- pyiceberg/table/deletion_vector.py | 6 ++++++ tests/table/test_deletion_vector.py | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/pyiceberg/table/deletion_vector.py b/pyiceberg/table/deletion_vector.py index 8b0ef0602f..7429dd1282 100644 --- a/pyiceberg/table/deletion_vector.py +++ b/pyiceberg/table/deletion_vector.py @@ -57,6 +57,12 @@ def _deserialize_bitmap(payload: bytes) -> list[BitMap]: offset = _ROARING_BITMAP_COUNT_SIZE_BYTES number_of_bitmaps = int.from_bytes(payload_view[:offset], byteorder="little") + # Every bitmap contributes at least a 4-byte key, so a count that cannot fit + # in the remaining payload is invalid and must not be used as a loop bound. + remaining = len(payload_view) - offset + if number_of_bitmaps * _ROARING_BITMAP_KEY_SIZE_BYTES > remaining: + raise ValueError(f"Payload declares {number_of_bitmaps} bitmaps, but only holds {remaining} bytes") + bitmaps = [] last_key = -1 diff --git a/tests/table/test_deletion_vector.py b/tests/table/test_deletion_vector.py index 8bc294b50c..6e642d8f6f 100644 --- a/tests/table/test_deletion_vector.py +++ b/tests/table/test_deletion_vector.py @@ -219,6 +219,15 @@ def serialize(self) -> bytes: assert DeletionVector._deserialize_bitmap(_bitmap_payload()) == [BitMap([1, 3, 5])] +def test_map_declared_count_exceeds_payload() -> None: + # A truncated payload that claims a large number of bitmaps must be rejected, + # rather than driving the deserialization loop on data that is not there. + puffin = (2**32).to_bytes(8, byteorder="little") + b"\x00\x00\x00\x00" + + with pytest.raises(ValueError, match="Payload declares 4294967296 bitmaps, but only holds 4 bytes"): + _ = DeletionVector._deserialize_bitmap(puffin) + + def test_map_high_vals() -> None: puffin = _open_file("64maphighvals.bin")