Skip to content

GH-55834: [C++] Preserve child offset and handle null values buffers in tensor extensions - #55835

Open
m4ru1 wants to merge 2 commits into
apache:mainfrom
m4ru1:fix-fixed-shape-tensor-slice-buffer
Open

m4ru1 wants to merge 2 commits into
apache:mainfrom
m4ru1:fix-fixed-shape-tensor-slice-buffer

Conversation

@m4ru1

@m4ru1 m4ru1 commented Nov 28, 2026 •

Copy link
Copy Markdown

Rationale for this change

Calling FixedShapeTensorArray::ToTensor() (or Python to_tensor(), to_numpy_ndarray(), and DLPack 1.0+ export) when the underlying child values array is sliced ignores the child offset and silently returns preceding elements from the backing buffer, and retains trailing buffer bytes that cause valid zero-sized tensor shapes (such as (20, 0, 2) over a shorter sliced buffer) to fail validation with "strides must not involve buffer over run". In addition, converting valid zero-element tensor arrays or scalars via ToTensor() or MakeTensor() crashes with SIGSEGV when the zero-length values buffer is omitted.

What changes are included in this PR?

Whole-array tensor conversion now slices the child values array by the outer row range—combining outer and child element offsets with overflow checks—before delegating to the shared tensor-buffer extractor so both array and scalar conversions clamp the view to the exact tensor byte length in one place. When the underlying values buffer is omitted or shorter than a zero-element slice offset, the shared extractor returns a static immutable zero-byte buffer for zero-element tensors and Status::Invalid when elements are expected.

Non-null backing buffers keep zero-copy read-only view sharing, shapes, permutations, strides, dimension names, and existing bounds checks on non-empty slices. Using a non-null immutable zero-byte buffer for omitted-buffer empty tensors preserves read-only ownership and tensor strides across downstream consumers such as Tensor.to_numpy(), DLPack 1.0+, and IPC.

flowchart LR
  A["Sliced FixedShapeTensorArray"] -->|"Compose outer row range with child slice"| B["Shared tensor buffer extraction"]
  S["Tensor extension scalar"] --> B
  B -->|"Clamp exact byte range or use static 0-byte buffer"| C["Zero-copy Tensor at exact offset, or valid empty Tensor"]
Loading

Are these changes tested?

Yes. Added unit tests in cpp/src/arrow/extension/tensor_extension_array_test.cc covering child-only and combined outer + child slicing (including non-trivial permutation, dimension names, and zero-copy pointer/size checks), zero-sized tensor shapes over sliced non-empty buffers, and omitted or empty values buffers on zero-element arrays and scalars (plus Status::Invalid rejection when elements are expected).

Verified locally on macOS arm64 (Debug, -DARROW_BUILD_TESTS=ON): all 5 new tests fail or crash as expected against unpatched main, and the full arrow-canonical-extensions-test suite passes with this patch (45/45). clang-format, cpplint, and git diff --check are clean.

Are there any user-facing changes?

No public API changes. Valid FixedShapeTensorArray instances with sliced child arrays and valid zero-element tensor arrays and scalars now convert cleanly to arrow::Tensor (and Python Tensor / NumPy / DLPack 1.0+) instead of returning wrong elements, raising spurious stride-overrun errors, or crashing.

This PR contains a "Critical Fix". It fixes (b) silent wrong-offset data produced by FixedShapeTensorArray::ToTensor() when the child values array is sliced, and (c) a SIGSEGV crash on ValidateFull()-valid zero-element tensor arrays and scalars whose values buffer is omitted.

Was AI used for this PR?

In accordance to the AI generation guidelines, please disclose below whether and how AI was used in this PR.

AI coding agents assisted with source analysis, drafting the C++ fix and unit tests, and preparing this PR description. The author verified the root-cause analysis against the checked-out sources, reviewed the complete diff line by line, and ran the local C++ build and test suite reported above.

PR code and description written by:

  • Human
  • AI

Reviewed before submission by:

  • Human
  • AI
  • Not reviewed

…ffers in tensor extensions

Compose outer row slicing with the child values slice in whole-array fixed-shape tensor conversion so child offsets and exact byte lengths are respected with overflow checks, and return a static immutable zero-byte buffer when zero-element tensor arrays or scalars omit the values buffer.

Signed-off-by: mora <m4ru1@qq.com>
…et in SliceTensorBuffer

Return an immutable zero-byte buffer when a zero-element tensor slice starts past a non-null empty or shorter backing buffer, matching ValidateLayout's zero-length contract while keeping zero-copy view offsets for in-range slices.

Signed-off-by: mora <m4ru1@qq.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant