fix lua regex causing runaway backtracking.#2882
Merged
Anteru merged 1 commit intopygments:masterfrom Jun 8, 2025
Merged
Conversation
This was referenced Apr 5, 2025
andersk
reviewed
May 5, 2025
pygments/lexers/scripting.py
Outdated
Comment on lines
+61
to
+67
| if sys.version_info >= (3, 11): | ||
| # Use a possessive quantifier to prevent greediness from causing runaway backtracking. | ||
| _space = r'(?:\s++)' | ||
| else: | ||
| # Possessive quantifiers are not available in Python < 3.11. Complex Lua is likely to | ||
| # cause any regex that includes _space to take an exponentially long time. | ||
| _space = r'(?:\s+)' |
Contributor
There was a problem hiding this comment.
A negative lookahead assertion works in all Python versions.
_space = r'(?:\s+(?!\s))'
Contributor
Author
There was a problem hiding this comment.
Nice that makes sense. I fixed it up and it works as far as I can tell. Thanks for helping simplify.
Collaborator
There was a problem hiding this comment.
Tested, works here - appreciate the investigation!
1ad791f to
06b3ff0
Compare
Use negative lookahead to avoid runway backtracking in whitespace. Fixes pygments#2839
06b3ff0 to
b6a51ec
Compare
Collaborator
|
Queued for Pygments 2.19.2, thanks guys! |
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.
Use possessive quantifier if possible, otherwise fallback to old, buggy regex.
Fixes #2839
For reference, see: