Skip to content

Bound el and pt affix regex runs to prevent quadratic backtracking - #14021

Open
Fazel94 wants to merge 1 commit into
explosion:masterfrom
Fazel94:fix/el-pt-affix-redos
Open

Bound el and pt affix regex runs to prevent quadratic backtracking#14021
Fazel94 wants to merge 1 commit into
explosion:masterfrom
Fazel94:fix/el-pt-affix-redos

Conversation

@Fazel94

@Fazel94 Fazel94 commented Aug 21, 2026

Copy link
Copy Markdown

Continuation of issue #14020

Description

Some affix regexes in el and pt languages would scale quadratically with input,
The fix tries to bound them 64 to alleviate its possible problems.

import re
import time

el_infix = re.compile(r"([a-zA-Z]+)\/([a-zA-Z]+)\/([a-zA-Z]+)")
pt_infix = re.compile(r"(\w+-\w+(-\w+)*)")

for name, pattern in [("el", el_infix), ("pt", pt_infix)]:
    print(name)
    for n in (5_000, 10_000, 20_000, 40_000):
        text = "a" * n  # no "/" or "-" -> never matches
        start = time.perf_counter()
        list(pattern.finditer(text))
        elapsed = time.perf_counter() - start
        print(f"  n={n:>6}  {elapsed:6.2f}s")

Output:

el
  n=  5000    0.24s
  n= 10000    1.03s
  n= 20000    4.09s
  n= 40000   17.11s
pt
  n=  5000    0.11s
  n= 10000    0.39s
  n= 20000    1.54s
  n= 40000    7.95s

Types of change

I have bounded the + to only {1,64} to prevent from catastrophic backtracking.

Checklist

  • I confirm that I have the right to submit this contribution under the project's MIT license.
  • I ran the tests, and all new and existing tests passed.
  • My changes don't require a change to the documentation, or if they do, I've added all required information.

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