Fix infinite loop / OOM in regex_match on overlapping matches#15
Open
thiagomkuhl wants to merge 1 commit into
Open
Fix infinite loop / OOM in regex_match on overlapping matches#15thiagomkuhl wants to merge 1 commit into
thiagomkuhl wants to merge 1 commit into
Conversation
regex_match tracked its scan cursor incorrectly: RegExp.firstMatch is run against a substring of the password, so rx_match.start is relative to that substring, not to the original password. The code used it as an absolute index and additionally re-included the match's last character in the next substring (`... - 1`). For overlapping matches (e.g. two year-like digit runs sharing a digit, such as "20112001") the cursor oscillates between two values forever, growing the matches list without bound and eventually exhausting memory. Track the absolute offset explicitly and advance strictly past each matched token so the loop always makes forward progress. Adds a regression test.
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
regex_matchinlib/src/matching.darttracks its scan cursor incorrectly:RegExp.firstMatchis run against a substring of the password (password.substring(last_index)), sorx_match.startis relative to that substring, not to the original password. The existing code used it as an absolute index and additionally re-included the match's last character in the next substring's start (... - 1).last_indexoscillate between two values forever — the loop never terminates andmatchesgrows without bound until the process runs out of memory.matching.regex_match('20112001Vi-')never returns. Therecent_yearregex matches"2011"at index 0, then (due to the bug) re-scans from index 3, matches"2001"at relative index 1 (absolute 4), computes a newlast_indexof 4, then re-scans from index 4 and matches"2001"again at relative index 0 (absolute 4), computinglast_indexback to 3 — and repeats forever.Zxcvbn().evaluate(password)).Fix
Track the absolute offset explicitly (
last_index + rx_match.start) and advance the cursor strictly past the end of the matched token (abs_end + 1, no- 1), so the loop always makes forward progress and never re-scans a position it has already consumed.Test plan
regex matching terminates on overlapping matches) reproducing the hang with'20112001Vi-', asserting it returns exactly the two expected non-overlapping matches.dart test) — all passing tests still pass. One pre-existing unrelated failure (scoring_test.dart: regex guesses) is present onmastertoo (it hardcodes an assumption about the current year vs.REFERENCE_YEAR = DateTime.now().year) and is untouched by this change.