m4ru1 opened a new pull request, #55835:
URL: https://github.com/apache/arrow/pull/55835
### 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, 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
out-of-range offsets. 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.
```mermaid
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"]
```
### 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 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](https://arrow.apache.org/docs/dev/developers/overview.html#ai-generated-code),
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
- [x] AI
**Reviewed before submission by:**
- [x] Human
- [x] AI
- [ ] Not reviewed
* GitHub Issue: Closes #55834
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]