Skip to content

Conversation

@rhukster
Copy link
Member

@rhukster rhukster commented Feb 4, 2026

Summary

Fixes the regex patterns in Twig3CompatibilityTransformer that caused JIT stack exhaustion when processing content with many Unicode characters (e.g., middle-dots ·).

  • The original pattern ([\'"])(?:\\\\.|(?!\\1).)*\\1 used a negative lookahead which caused catastrophic backtracking
  • Changed to use possessive quantifiers (*+) which prevent backtracking entirely
  • Added fallback to return original content if regex fails

Methods fixed:

  • rewriteSameAsTests()
  • rewriteDivisibleByTests()
  • rewriteNoneTests()

Test plan

@NoNoNo - Could you please test this fix with your original test case from #4015?

#!/usr/bin/env sh
composer create-project 'getgrav/grav:dev-fix/twig3-regex-jit-stack-exhaustion' testcase && \
cd testcase && \
# ... rest of your test script

Or manually apply the changes from this PR to your existing installation.

Fixes #4015

🤖 Generated with Claude Code

The regex patterns in rewriteSameAsTests(), rewriteDivisibleByTests(),
and rewriteNoneTests() used negative lookahead `(?!\1)` which caused
catastrophic backtracking and JIT stack exhaustion when processing
content with many Unicode characters (e.g., middle-dots).

This fix uses possessive quantifiers (*+) to prevent backtracking:
- Handles single and double quotes separately (no backreference needed)
- Uses *+ possessive quantifiers to prevent backtracking
- Falls back to original content if regex fails

Fixes #4015

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes critical JIT stack exhaustion errors in Twig3 regex patterns that occurred when processing content with many Unicode characters (e.g., middle-dots). The issue manifested as blank pages when Twig processing was enabled.

Changes:

  • Replaced catastrophic backtracking regex patterns with possessive quantifiers (*+)
  • Inverted group capture logic to match the new pattern structure
  • Added fallback to return original content if regex fails

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

rhukster and others added 2 commits February 3, 2026 19:07
…ansformer.php

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…ansformer.php

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@rhukster
Copy link
Member Author

rhukster commented Feb 4, 2026

@claude you there?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant