Skip to content

Conversation

@thavelick
Copy link
Contributor

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+)'
Copy link
Contributor

@andersk andersk May 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A negative lookahead assertion works in all Python versions.

    _space = r'(?:\s+(?!\s))'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice that makes sense. I fixed it up and it works as far as I can tell. Thanks for helping simplify.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested, works here - appreciate the investigation!

@thavelick thavelick force-pushed the fix_lua_runaway_regex branch from 1ad791f to 06b3ff0 Compare May 5, 2025 23:44
Use negative lookahead to avoid runway backtracking in whitespace.

Fixes pygments#2839
@thavelick thavelick force-pushed the fix_lua_runaway_regex branch from 06b3ff0 to b6a51ec Compare May 5, 2025 23:44
@Anteru Anteru merged commit 26634c8 into pygments:master Jun 8, 2025
@Anteru
Copy link
Collaborator

Anteru commented Jun 8, 2025

Queued for Pygments 2.19.2, thanks guys!

@Anteru Anteru added this to the 2.19.2 milestone Jun 8, 2025
@Anteru Anteru added the A-lexing area: changes to individual lexers label Jun 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-lexing area: changes to individual lexers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Lua code using spaces for indentation causes infinite loop.

3 participants