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

オープン
#4,286 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 4 日以内に返信

@Pastalikek65 がすでに取り組んでいます。

2026年11月27日 から。

  • #4287 @Pastalikek65 による — オープン

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
25/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
停滞
技術スタック
node.js, typescript

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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

主要言語
TypeScript
スター
37.3k
フォーク
3.9k
平均マージ
5日 6時間
マージ済み PR(30日)
62

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

markedjs/marked のほかの issue

markedjs/marked の issue をすべて見る

似ている issue

TypeScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。