Skip to content

fix: avoid quadratic scans and stack overflow on nested emphasis - #4287

Open
Pastalikek65 wants to merge 2 commits into
markedjs:masterfrom
Pastalikek65:fix/emstrong-nested-depth
Open

Pastalikek65 wants to merge 2 commits into
markedjs:masterfrom
Pastalikek65:fix/emstrong-nested-depth

Conversation

@Pastalikek65

Copy link
Copy Markdown

Marked version: 18.3.0 (1ac4b95)

Markdown flavor: CommonMark|GitHub Flavored Markdown

Description

Deeply nested emphasis (and strikethrough) previously hit two scaling bottlenecks in the inline lexer:

  1. Tokenizer.emStrong and Tokenizer.del scanned forward with endReg on every opener and recursed into this.lexer.inlineTokens(text) at each level, causing O(n^2) scanning/re-masking and a RangeError stack overflow around 2,000 nesting levels.
  2. Lexer.#tokenizeInlines now processes default emStrong and del child tokens iteratively on an explicit frame stack, reusing the parent maskedSrc slice when no emStrongMask hook is active.
  3. For inputs with more than 16 delimiter matches using built-in emphasis/strikethrough rules, Tokenizer.emStrong and Tokenizer.del lazily index delimiter runs once per pass and look up matching closers in O(log k) via prefix balances and a min-segment tree. Short inputs stay on the existing loop without extra allocations.

With these changes, 20,000 nested *a ... c* levels lex without overflowing the stack while keeping AST and HTML output identical.

Contributor

  • Test(s) exist to ensure functionality and minimize regression (if no tests added, list tests covering this PR); or,
  • no tests required for this PR.
  • If submitting new feature, it has been documented in the appropriate places.

Committer

In most cases, this should be a different person than the contributor.

@vercel

vercel Bot commented Nov 27, 2026

Copy link
Copy Markdown

@Pastalikek65 is attempting to deploy a commit to the MarkedJS Team on Vercel.

A member of the Team first needs to authorize it.

@vercel

vercel Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
marked-website Ready Ready Preview Nov 28, 2026 4:05am UTC

Request Review

@shubham21155102

Copy link
Copy Markdown

Also fixes another quadratic case: '**a*b '.repeat(n) takes 321 / 1131 / 4402 ms on master for n = 1k / 2k / 4k, and 20 / 13 / 12 ms with this PR. It might be worth adding to test/specs/redos/.

@Pastalikek65

Copy link
Copy Markdown
Author

Thanks @shubham21155102! Added that **a*b case to test/specs/redos/quadratic_nested_emphasis.cjs.

This branch was successfully deployed

1 active (outdated) deployment
Preview — 1ac4b959 Deployed Nov 28, 2026 by vercel[bot]
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.

Deeply nested emphasis is quadratic and overflows the stack in the lexer

2 participants