Repository navigation
MINOR: [C++][Compute] Avoid signed overflow in slice kernel output sizing - #54510
Closed
Nayana-Naik73 wants to merge 1 commit into
Closed
Nayana-Naik73 wants to merge 1 commit into
Nayana-Naik73 wants to merge 1 commit into
Conversation
|
Thanks for opening a pull request! This pull request has been automatically closed because you currently have 4 open pull requests, which is more than the limit of 3. Due to the increase in pull requests opened by AI bots, and in order to keep the review queue manageable, Apache Arrow limits contributors without repository access to at most 3 concurrently open pull requests. This helps make sure each pull request gets the attention it needs and that work in progress does not go stale. Once one of your other open pull requests has been merged or closed, you are welcome to reopen this one. See also: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rationale for this change
SliceBytesTransform::MaxCodeunits(binary_slice) andSliceCodeunitsTransform::MaxCodeunits(utf8_slice_codeunits) size the output buffer from(stop - start + step - 1) / stepusing rawint64_tarithmetic on the user-suppliedSliceOptions, whosestart,stopandstepare unbounded. A largestop(binary) or a largestep/very negativestart(utf8, where onlystopis clamped) overflows the addition/subtraction, somax_slice_byteswraps negative,std::max<int64_t>(0, .)collapses it to0, and the kernel allocates a zero-length output buffer.Transformthen writes the real slice into that buffer, an out-of-bounds write.This is reachable from a default pyarrow call:
pc.binary_slice(arr, 0, step=2)leavesstopatsys.maxsize(INT64_MAX), sostop - start + step - 1isINT64_MAX + 1. Under a UBSan build the subtraction/addition is flagged as signed-integer-overflow; under ASan the subsequent copy is a heap-buffer-overflow.What changes are included in this PR?
Both
MaxCodeunitsoverrides compute the span and the finalninputs * estimateproduct withSubtractWithOverflow/AddWithOverflow/MultiplyWithOverflowand fall back to the input length on overflow. The input length is always a valid upper bound on the sliced output, so this matches the existing mismatched-sign fallback and leaves the value bit-identical for every non-overflowing input.Are these changes tested?
Yes.
SliceBytesOverflowandSliceCodeunitsOverflowinscalar_string_test.ccexerciseINT64_MAX/INT64_MINoptions (including the default pyarrowstop). They abort under UBSan/ASan on the current tree and pass with the fix; the fullarrow-compute-scalar-type-testsuite stays green (352 tests).Are there any user-facing changes?
No. Valid inputs produce the same results; only the pathological overflow case now allocates the safe worst-case buffer instead of corrupting memory.
This PR contains a "Critical Fix". An overflow in the slice output-size computation led to a zero-length allocation followed by an out-of-bounds write of the sliced bytes.
Was AI used for this PR?
PR code and description written by:
Reviewed before submission by: