Repository navigation
Fix regex backtracking into a nested counted loop - #2277
Open
JoaoPauloCMarra wants to merge 1 commit into
Open
JoaoPauloCMarra wants to merge 1 commit into
JoaoPauloCMarra wants to merge 1 commit into
Conversation
BeginLoop reset the loop's iteration count to 0 without saving the old
value. When an outer loop entered the inner loop again, backtracking
into the inner loop's previous entry resumed with a count of 0, so a
{1} loop ran its body again instead of exiting. For example,
/(?:(?:a+){1}){2}/ did not match "aa", and /(?:(?:a?){1}){1,3}c/
threw RangeError on "a".
Save the loop state on the backtrack stack before resetting it when the
count is not already 0.
Fixes meta#2232
Fixes meta#2235
This branch has not been deployed
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.
Summary
Fixes #2232. Fixes #2235.
When the regex executor enters a counted loop (
BeginLoop), it resets the loop's iteration count to 0 without saving the old value on the backtrack stack. If that loop is nested in another loop, the next outer iteration enters it again and overwrites the count from the previous entry. When a later failure backtracks into that previous entry (for example a greedya+giving back a character), the loop resumes with a count of 0, so a{1}loop runs its body again instead of exiting. Depending on the pattern this gives a wrong result or runs until the backtracking limit.The fix pushes a
SetLoopDataentry before resetting the count. The push is skipped when the count is already 0, because the reset then changes nothing. So the only cost is one backtrack entry when a loop with a non-zero count is entered again, which only happens for loops nested in other loops. Simple loops and width-1 loops do not useLoopDataand are not affected.Test Plan
Added
test/hermes/regexp-nested-loop-backtrack.js. It covers both issues (the ASCII and UTF-16 matchers for #2235), capture groups, a failed first start position, a non-greedy outer loop, a lookbehind and theRangeErrorcase. Without the fix 10 of its 11 checks fail. With the fix all pass, and every expected value matches V8.ASan Debug build (
-O1) on macOS arm64:check-hermes: 4364 passed, 0 unexpected failures