From 8d79551bc44384ffe375a9462addd843d55ec277 Mon Sep 17 00:00:00 2001 From: 0z5a Date: Sun, 15 Nov 2026 14:14:22 +0100 Subject: [PATCH] fix(python): Don't crash when shallow copying string/binary view arrays with variadic buffers c_array_shallow_copy() called ArrowArrayAddVariadicBuffers() on the uninitialized output array instead of the array being built, so any string or binary view array with a non-inlined value crashed and the copy loop wrote past the end of the buffer arrays. Pass tmp instead and add a regression test with values longer than 12 bytes. --- python/src/nanoarrow/_utils.pyx | 2 +- python/tests/test_iterator.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/python/src/nanoarrow/_utils.pyx b/python/src/nanoarrow/_utils.pyx index 63943b46d..33fdb1321 100644 --- a/python/src/nanoarrow/_utils.pyx +++ b/python/src/nanoarrow/_utils.pyx @@ -366,7 +366,7 @@ cdef void c_array_shallow_copy(object base, const ArrowArray* src, ArrowArray* d # We might need some more buffers if we are shallowly copying a string/binary view if src.n_buffers > 3: - code = ArrowArrayAddVariadicBuffers(dst, src.n_buffers - 3) + code = ArrowArrayAddVariadicBuffers(tmp, src.n_buffers - 3) Error.raise_error_not_ok("ArrowArrayAddVariadicBuffers()", code) for i in range(src.n_buffers): diff --git a/python/tests/test_iterator.py b/python/tests/test_iterator.py index 20fe3bd41..ffd8982f7 100644 --- a/python/tests/test_iterator.py +++ b/python/tests/test_iterator.py @@ -92,6 +92,22 @@ def test_iterator_nullable_string(arrow_type): assert list(iter_py(sliced)) == ["cde", None] +def test_iterator_view_with_variadic_buffers(): + # Values longer than 12 bytes are not inlined, so these arrays have + # one or more variadic buffers (i.e., more than three buffers) + long_value = "a string longer than 12 bytes" + + array = na.c_array(["ab", None, long_value], na.string_view()) + assert array.n_buffers == 4 + assert list(iter_py(array)) == ["ab", None, long_value] + assert list(iter_py(array[1:])) == [None, long_value] + + array = na.c_array([b"ab", None, long_value.encode()], na.binary_view()) + assert array.n_buffers == 4 + assert list(iter_py(array)) == [b"ab", None, long_value.encode()] + assert list(iter_py(array[1:])) == [None, long_value.encode()] + + @pytest.mark.parametrize( "arrow_type", [na.binary(), na.large_binary(), na.binary_view()] )