fix lua regex causing runaway backtracking. by thavelick · Pull Request #2882 · pygments/pygments

@thavelick

This was referenced

Apr 5, 2025

andersk

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+)'

Choose a reason for hiding this comment

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

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.

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

Use negative lookahead to avoid runway backtracking in whitespace.

Fixes pygments#2839

@Anteru

Queued for Pygments 2.19.2, thanks guys!