Skip to content

Commit

Permalink
catch dest_offset < 0
Browse files Browse the repository at this point in the history
  • Loading branch information
paleolimbot committed May 2, 2024
1 parent 81c76d3 commit 1bddc8d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 3 additions & 2 deletions python/src/nanoarrow/_lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1834,11 +1834,12 @@ cdef class CBufferView:
PyBuffer_Release(&buffer)
raise ValueError("Destination buffer has itemsize != 1")

if buffer.len < (dest_offset + length):
if dest_offset < 0 or buffer.len < (dest_offset + length):
buffer_len = buffer.len
PyBuffer_Release(&buffer)
raise IndexError(
f"Can't unpack {length} elements into buffer of size {buffer_len}"
f"Can't unpack {length} elements into buffer of size {buffer_len} "
f"with dest_offset = {dest_offset}"
)

ArrowBitsUnpackInt8(
Expand Down
4 changes: 4 additions & 0 deletions python/tests/test_c_buffer_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ def test_buffer_view_bool_unpack():
view.unpack_bits_into(out, dest_offset=2)
assert list(out) == [255, 255, 1, 0, 0, 1, 0, 0, 0, 0]

# Check error requesting out-of-bounds dest_offset
with pytest.raises(IndexError, match="Can't unpack"):
view.unpack_bits_into(out, dest_offset=-1)

# Check errors from requesting out-of-bounds slices
msg = "do not describe a valid slice"
with pytest.raises(IndexError, match=msg):
Expand Down

0 comments on commit 1bddc8d

Please sign in to comment.