GH-50539: [C++][Gandiva] size varlen cast output from the formatter#50540
Open
Arawoof06 wants to merge 1 commit into
Open
GH-50539: [C++][Gandiva] size varlen cast output from the formatter#50540Arawoof06 wants to merge 1 commit into
Arawoof06 wants to merge 1 commit into
Conversation
|
|
|
|
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
castVARCHAR/castVARBINARYfromdate64size their output block with a hard-coded 10 (YYYY-MM-DD), but the copy that follows is bounded by the caller-supplied length rather than by what was allocated.arrow::internal::StringFormatter<Date64Type>goes past 10 bytes in two ways: years outside 0-9999 need 11-12, and values it cannot represent go throughdetail::FormatOutOfRange, which emits up to 42 bytes.So
castVARCHAR(<date64>, 64)on an out-of-range value writes 42 bytes into a 10-byte arena block and setsout_lento 42, corrupting whatever the execution context hands out next.The float macro has the same shape, a 24-byte constant against a formatter documented to use a 50-byte buffer, so it carries the same mismatch even though current Java-style output happens to stay under 24.
What changes are included in this PR?
The arena allocation moves inside the formatter callback, where the real formatted size is known, so the allocation and the copy use one bound. That drops the guessed constant from both the date64 and float macros.
Are these changes tested?
Yes.
TestCastVARCHARFromMillisecondsLongOutputcasts an out-of-range date64 and then allocates again from the same context. On the unpatched tree it reads back:It passes after the change, and needs no sanitizer to fail.
gandiva-internals-test,gandiva-precompiled-testandgandiva-projector-testall pass locally.Are there any user-facing changes?
Dates and out-of-range values that format to more than 10 bytes now return the full string, truncated to the requested length, instead of writing past the block. Dates that already fit are unaffected.
This PR contains a "Critical Fix". Heap buffer overflow reachable from SQL
castVARCHARon adate64value.Disclosure of AI usage, per the AI-generated code guidance: this was produced with AI assistance, using BugQore's multi-agent redemption system. That covered locating the mismatch, writing the patch, and writing the regression test. Verified before opening: the new test fails on the unpatched tree with the corrupted output shown above and passes after, the three Gandiva suites are green, and clang-format 18.1.8 (the version pinned in
.pre-commit-config.yaml) reports no changes. Happy to walk through any part of the reasoning in review.