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

Aperta
#4,286 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 4 giorni

@Pastalikek65 ci sta già lavorando.

Dal 27/11/2026.

  • #4287 di @Pastalikek65 — aperta

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
25/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
node.js, typescript

Direzione di ricerca

The report identifies recursion from emStrong to inlineTokens and repeated lexing of nested text, but does not name source or test files. Start by tracing that call path and inspect the existing emphasis tests and lexer benchmarks. The issue is resolved when deeply nested emphasis lexes in linear time without stack overflow, with regression tests covering both behaviors.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

category: inline elements L2 - annoying

Marked version: 18.3.0 (also 17.0.6), Node 22.23.5, default options

Describe the bug
Properly nested emphasis lexes in superlinear (roughly quadratic) time, and past a depth that depends on the stack size marked.lexer() throws RangeError: Maximum call stack size exceeded. A few kilobytes of input are enough, so any application that lexes user-supplied Markdown can be stalled or made to throw.

To Reproduce

  1. npm install [email protected]
  2. Run:
import { marked } from "marked";

for (const n of [300, 600, 1200, 1500, 2000]) {
  const src = "*a ".repeat(n) + "b" + " c*".repeat(n);
  const start = process.cpuUsage();
  try {
    marked.lexer(src);
    const used = process.cpuUsage(start);
    console.log(n, (used.user + used.system) / 1000, "ms");
  } catch (e) {
    console.log(n, e.constructor.name, e.message);
  }
}
  1. Actual output (CPU ms per marked.lexer, best of 3):
nesting levels 300 600 1,200 1,500 2,000
marked 18.3.0 39 141 589 851 RangeError: Maximum call stack size exceeded

The stack shows the lexer recursing emStrong → inlineTokens once per nesting level and re-lexing the inner text each time, which explains both the quadratic time and a recursion depth equal to the nesting depth. On 17.0.6 the same input also overflows the stack (by 3,000 levels).

Expected behavior
Linear time and no stack overflow, as with CommonMark's delimiter-stack algorithm. For comparison, markdown-it-py 4.3.0 renders the same input in 23 ms at 1,200 levels and 543 ms at 20,000 levels, without overflowing.

(The unclosed-delimiter case, "*a ".repeat(n), is already fast on 18.3.0; thanks for that fix.)

Lingua principale
TypeScript
Stelle
37.3k
Fork
3.9k
Merge medio
5g 6h
PR unite (30g)
62

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di markedjs/marked

Tutte le issue di markedjs/marked

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.