GH-49470: [C++][Gandiva] Fix crashes in substring_index and truncate with extreme integer values#49471
Open
dmitry-chirkov-dremio wants to merge 1 commit intoapache:mainfrom
Conversation
…ncate with extreme integer values
|
|
lriggs
reviewed
Mar 9, 2026
|
|
||
| if (static_cast<int32_t>(abs(cnt)) <= static_cast<int32_t>(occ.size()) && cnt > 0) { | ||
| // Use int64_t to avoid undefined behavior with abs(INT_MIN) | ||
| int64_t abs_cnt = (cnt < 0) ? -static_cast<int64_t>(cnt) : static_cast<int64_t>(cnt); |
Contributor
There was a problem hiding this comment.
Is it possible to check the function parameter for the bad size and exit early like in the truncation fix? That seems simpler.
Contributor
Author
There was a problem hiding this comment.
I'd leave as is:
- The int64_t fix is more robust - it handles ALL negative values correctly, not just INT_MIN
- It's the same number of lines - early exit adds 5 lines, current fix adds 3 lines
- The current fix also simplifies the existing code - removes redundant static_cast<int32_t> casts
The early-exit approach only guards against the specific crash values, while the int64_t approach fixes the underlying type-safety issue.
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
Two Gandiva functions crash when called with extreme integer parameter values:
substring_index(VARCHAR, VARCHAR, INT)crashes with SIGBUS when count isINT_MINtruncate(BIGINT, INT)crashes with SIGSEGV when scale isINT_MAXorINT_MINWhat changes are included in this PR?
substring_index fix (
gdv_string_function_stubs.cc):abs(cnt)with safeint64_tcomputation to avoid undefined behavior whencnt == INT_MINtruncate fix (
precompiled/extended_math_ops.cc):GetScaleMultiplierAre these changes tested?
Yes. Added coverage for
INT_MAX/INT_MINvalues ingdv_function_stubs_test.ccandextended_math_ops_test.cc.Are there any user-facing changes?
No.
This PR contains a "Critical Fix". These changes fix crashes caused by:
abs(INT_MIN)triggering undefined behavior (integer overflow) insubstring_indexGetScaleMultiplierwhentruncatereceives extreme scale values