This is an automated email from the ASF dual-hosted git repository.

paleolimbot pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-nanoarrow.git


The following commit(s) were added to refs/heads/main by this push:
     new 91be268f fix(python): Don't crash when shallow copying string/binary 
view arrays with variadic buffers (#1018)
91be268f is described below

commit 91be268f7460acbe94bca3e220d03282ba42e07c
Author: 0z5a <[email protected]>
AuthorDate: Mon Nov 16 21:18:43 2026 +0100

    fix(python): Don't crash when shallow copying string/binary view arrays 
with variadic buffers (#1018)
    
    `c_array_shallow_copy()` reserves the extra buffers needed by a string
    view or binary view array by calling `ArrowArrayAddVariadicBuffers()`,
    but it calls it on `dst` (the not-yet-initialized output array) instead
    of on `tmp` (the array it is building). `dst` is usually uninitialized
    memory at that point, so this dereferences a garbage `private_data`
    pointer, and `tmp` never gets room for the variadic buffers, so the loop
    that follows writes past the end of its buffer arrays.
    
    Any string view or binary view array with at least one variadic data
    buffer goes through this path, i.e. any array with a value longer than
    12 bytes, and every such array exported by pyarrow (which always
    includes a data buffer). The process segfaults or aborts, for example:
    
    ```python
    import nanoarrow as na
    
    array = na.c_array(["ab", None, "a string longer than 12 bytes"], 
na.string_view())
    list(na.iterator.iter_py(array))  # crashes
    array[1:]                         # crashes
    ```
    
    The same crash happens for `na.Array(pa.array([...],
    pa.string_view())).to_pylist()`, for exporting such an array to pyarrow
    via `__arrow_c_array__`, and when a view array is used as a child in
    `c_array_from_buffers()`. The branch was added in #808, so 0.8.0 and
    0.9.0 are affected (I also see the crash with the 0.9.0 wheel). The
    existing iterator tests only use values of 12 bytes or fewer, which are
    inlined and give three buffers, so they never reach this branch.
    
    The fix passes `tmp` instead of `dst`. With it, the examples above
    return the same values as pyarrow's `to_pylist()`, and buffers are still
    released once the copies are dropped. I added a regression test that
    iterates and slices string view and binary view arrays with a
    non-inlined value; it crashes the test process without the fix and
    passes with it. The rest of the Python test suite passes.
    
    This change was drafted with Claude Code (claude-opus-5-5), which found
    the bug and generated the fix, the regression test and this description;
    the test results above are from the runs described.
---
 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 63943b46..33fdb132 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 41565d9c..bc4e0cfb 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()]
 )

Reply via email to