Skip to content

[C++][Python] Converting FixedShapeTensorArray to Tensor reads wrong values on sliced child arrays and crashes on empty tensors #55834

Backlog

Description

@m4ru1

Describe the bug, including details regarding any error messages, version, and platform.

Converting a FixedShapeTensorArray to a Tensor or NumPy ndarray (via to_tensor() / to_numpy_ndarray() in Python or ToTensor() in C++) ignores the child values slice offset and silently reads from the beginning of the backing buffer whenever the child array is sliced. In addition, valid zero-element tensor arrays and scalars either raise ArrowInvalid: strides must not involve buffer over run when backed by a shorter sliced buffer or terminate with SIGSEGV when the zero-length values buffer is omitted (tested on PyArrow 26.0.1 and main on macOS arm64):

values = pa.array(np.arange(12, dtype=np.int64)).slice(4, 8)  # [4, 5, ..., 11]
arr = pa.ExtensionArray.from_storage(pa.fixed_shape_tensor(pa.int64(), (2, 2)), pa.FixedSizeListArray.from_arrays(values, 4))
arr[0].to_numpy()            # OK:    [[4, 5], [6, 7]]
arr.to_numpy_ndarray()[0]    # Wrong: [[0, 1], [2, 3]]

Self-contained reproduction (pyarrow 26.0.1, macOS arm64):

import subprocess
import sys
import numpy as np
import pyarrow as pa

# 1. Sliced child values array -> wrong tensor elements
values = pa.array(np.arange(12, dtype=np.int64)).slice(4, 8)
storage = pa.FixedSizeListArray.from_arrays(values, 4)
arr = pa.ExtensionArray.from_storage(pa.fixed_shape_tensor(pa.int64(), (2, 2)), storage)
print("per-row:", [row.to_numpy().tolist() for row in arr])
print("batch:  ", arr.to_numpy_ndarray().tolist())

# 2. Zero-sized tensor shape over shorter sliced buffer -> spurious overrun error
empty_slice = pa.array([0, 1], type=pa.int64()).slice(1, 0)
zero_arr = pa.ExtensionArray.from_storage(
    pa.fixed_shape_tensor(pa.int64(), (0, 2)),
    pa.Array.from_buffers(pa.list_(pa.int64(), 0), 20, [None], children=[empty_slice]),
)
zero_arr.validate(full=True)
try:
    zero_arr.to_tensor()
except pa.ArrowInvalid as exc:
    print("zero-shape error:", exc)

# 3. Valid empty tensor array & scalar with omitted values buffer -> SIGSEGV
for expr in ["arr.to_tensor()", "arr[0].to_tensor()"]:
    code = f"""
import pyarrow as pa, resource
resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
empty_values = pa.Array.from_buffers(pa.int64(), 0, [None, None])
arr = pa.ExtensionArray.from_storage(
    pa.fixed_shape_tensor(pa.int64(), (0, 2)),
    pa.Array.from_buffers(pa.list_(pa.int64(), 0), 1, [None], children=[empty_values]),
)
arr.validate(full=True)
{expr}
"""
    proc = subprocess.run([sys.executable, "-c", code], capture_output=True, text=True)
    print(f"{expr} returncode:", proc.returncode)

Observed output:

per-row: [[[4, 5], [6, 7]], [[8, 9], [10, 11]]]
batch:   [[[0, 1], [2, 3]], [[4, 5], [6, 7]]]
zero-shape error: strides must not involve buffer over run
arr.to_tensor() returncode: -11
arr[0].to_tensor() returncode: -11

Expected behavior:
Converting a FixedShapeTensorArray to a Tensor or NumPy ndarray should return values starting at the sliced child offset (matching per-row scalar conversion), and valid zero-element tensor arrays and scalars should convert cleanly to empty tensors without stride-overrun errors or crashes.

Cause: FixedShapeTensorArray::ToTensorWithNulls in cpp/src/arrow/extension/fixed_shape_tensor.cc omits the child array's offset and exact byte length when slicing the values buffer, and both it and internal::SliceTensorBuffer in cpp/src/arrow/extension/tensor_internal.cc dereference null values buffers on zero-element inputs.

Component(s)

C++, Python

Activity

  1. added a commit that references this issue on Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions