From 81b8b1b11ce09a2778440a5890dfd2de0065855b Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Tue, 11 Feb 2025 19:12:58 -0500 Subject: [PATCH] Do not validate a backreference if the group that it refers to is not valid --- regex/matching.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/regex/matching.go b/regex/matching.go index 3dba3f0..e1e7d9b 100644 --- a/regex/matching.go +++ b/regex/matching.go @@ -319,7 +319,7 @@ func findAllSubmatchHelper(start *nfaState, str []rune, offset int, numGroups in if currentState.contentContains(str, idx, preferLongest) { nextStates = addStateToList(str, idx+1, nextStates, *currentState.next, currentState.threadGroups, nil, preferLongest) } - } else if currentState.isBackreference { + } else if currentState.isBackreference && currentState.threadGroups[currentState.referredGroup].IsValid() { groupLength := currentState.threadGroups[currentState.referredGroup].EndIdx - currentState.threadGroups[currentState.referredGroup].StartIdx if currentState.threadBackref == groupLength { currentState.threadBackref = 0