fix: avoid panic in array_position start_from near i64::MIN#23433
Open
SAY-5 wants to merge 1 commit into
Open
Conversation
Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
xudong963
reviewed
Jul 10, 2026
xudong963
left a comment
Member
There was a problem hiding this comment.
I think the correct design is to retain the original 1-based value until it has been validated, then convert with checked arithmetic. Ideally, one shared helper should:
- Accept the original start_from.
- Use checked_sub(1).
- Convert with usize::try_from.
- Validate against the row length.
5,Construct any error from the untouched original value.
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.
Which issue does this PR close?
Rationale for this change
array_positionaccepts an optional 1-indexedstart_fromargument. When resolving it, the code converts to a 0-indexed offset with a plainvalue - 1. Forstart_from = i64::MINthis subtraction overflows, which panics in debug/overflow-checked builds and wraps in release. The out-of-bounds error path also computedfrom + 1, which overflows the same way. An invalidstart_fromshould return a normal error rather than panic.What changes are included in this PR?
Use
saturating_sub(1)when convertingstart_fromto a 0-indexed offset andsaturating_add(1)when reporting it back in the out-of-bounds error. A saturatedi64::MINstays negative, so the existingfrom >= 0bounds check now rejects it with the usualstart_from out of boundserror instead of overflowing.Are these changes tested?
Yes. Added a unit test that invokes
array_positionwithstart_from = i64::MINand asserts an error is returned. It panics onmainand passes with this change.Are there any user-facing changes?
array_position(..., i64::MIN)now returns an error instead of panicking. No API changes.