Skip to content

Commit a755137

Browse files
ankanebeyondsundial
andcommitted
Fixed Bit.from_binary with negative length - resolves #173
Co-authored-by: beyondsundial <67931863+beyondsundial@users.noreply.github.com>
1 parent e0f3ac3 commit a755137

2 files changed

Lines changed: 6 additions & 1 deletion

File tree

‎pgvector/bit.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def from_binary(cls, value: bytes | bytearray | memoryview, /) -> Bit:
8888
length, = unpack_from('>i', value)
8989
data = memoryview(value)[4:].tobytes()
9090

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

9494
bit = cls.__new__(cls)

‎tests/test_bit.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,8 @@ def test_from_binary(self) -> None:
110110
if np is not None:
111111
assert np.array_equal(vec.to_numpy(), [True, False, True])
112112
assert vec.to_binary() == data
113+
114+
def test_from_binary_negative_length(self) -> None:
115+
with pytest.raises(ValueError) as error:
116+
Bit.from_binary(pack('>i', -1))
117+
assert str(error.value) == 'invalid length'

0 commit comments

Comments
 (0)