avoid quadratic trailing-punctuation scan in urlize#2204
Closed
zayeem06 wants to merge 1 commit into
Closed
Conversation
Author
|
All good if this isn't something worth taking on. Leaving the context here in case it's useful: the slow input is an untrusted value passed to |
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.
urlize strips trailing punctuation from each word with an unanchored
re.search(r"([)>.,\n]|>)+\$", middle), which retries the greedy match from every position. When a word is a long run of those characters (a typed>is escaped to>first) followed by one non-matching character, the search runs in quadratic time, so a ~32 KB value passed through the filter stalls a single render for ~12s while linkifying otherwise harmless text. This replaces the suffix search with a backward scan that trims the same characters plus the>entity in linear time, so the output is identical but the work is O(n).I added a test covering trailing
>/entity stripping so the behavior is pinned, and the existing urlize tests plus the rest of the suite still pass. Worth verifying the scan handles the>boundary the same way the old alternation did, which the new test exercises.