Guard against negative capture group in regexp string transform#332
Open
arpitjain099 wants to merge 1 commit into
Open
Guard against negative capture group in regexp string transform#332arpitjain099 wants to merge 1 commit into
arpitjain099 wants to merge 1 commit into
Conversation
The regexp string transform checks that the requested capture group index is within the upper bound of the matched groups, but not that it is non-negative. A negative group value in the Composition (Group is *int) slips past len(groups) == 0 || g >= len(groups) and then panics on groups[g] with an index out of range. Add the lower-bound check so a negative group returns the existing no-match error instead of panicking. Includes a test covering a negative group value. Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
arpitjain099
requested review from
negz,
phisco,
sergenyalcin,
turkenf and
ulucinar
as code owners
July 23, 2026 16:43
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.
What this does
stringRegexpTransformpicks a capture group by index. The bounds check only covers the upper end:Groupis a*int, so a Composition can set it to a negative value. A negativegpasses bothlen(groups) == 0andg >= len(groups), thengroups[g]panics with an index out of range.This adds the missing lower-bound check (
g < 0) so a negative group returns the existing no-match error instead of panicking.Testing
Added a test case with
Group: -1. Without the fix it panics:With the fix
go test ./...passes and the negative group returns the no-match error like an out-of-range positive group already does.