Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pgvector/bit.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def from_binary(cls, value: bytes | bytearray | memoryview, /) -> Bit:
length, = unpack_from('>i', value)
data = memoryview(value)[4:].tobytes()

if len(data) != (length + 7) // 8:
if length < 0 or len(data) != (length + 7) // 8:
raise ValueError('invalid length')

bit = cls.__new__(cls)
Expand Down
10 changes: 10 additions & 0 deletions tests/test_bit.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@


class TestBit:
@pytest.mark.parametrize('length', [-1, -2, -7, -8])
def test_from_binary_negative_length(self, length: int) -> None:
with pytest.raises(ValueError, match='invalid length'):
Bit.from_binary(pack('>i', length))

@pytest.mark.parametrize('length', [0, 1, 7, 8, 9])
def test_from_binary_length_boundaries(self, length: int) -> None:
bit = Bit('1' * length)
assert Bit.from_binary(bit.to_binary()) == bit

def test_list(self) -> None:
assert Bit([True, False, True]).to_list() == [True, False, True]

Expand Down