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

Abierto
#4,286 1 comentario 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 4 días

@Pastalikek65 ya está trabajando en esto.

Desde el 27/11/2026.

  • #4287 de @Pastalikek65 — abierto

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
25/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Estancado
Stack tecnológico
node.js, typescript

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.)

Lenguaje dominante
TypeScript
Estrellas
37.3k
Forks
3.9k
Merge medio
5 d 6 h
PR fusionados (30 d)
62

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de markedjs/marked

Todos los issues de markedjs/marked

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.