Deeply nested emphasis is quadratic and overflows the stack in the lexer
メンテナーはふだん 4 日以内に返信
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 25/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 停滞
- 技術スタック
- node.js, typescript
- 領域
- backend, performance
調査の方向性
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 の本文から書いたものです。
説明
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
npm install [email protected]- 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);
}
}
- 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
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
markedjs/marked のほかの issue
-
Inline raw HTML is escaped after an unclosed `_` / `*` / `~` (regression in 18.2.1)対応中かも @O1sumitkumar が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 4 日以内に返信
-
Lazy continuation line inside a quoted list item is parsed as a nested list対応中かも @amstedy が 1 日前に担当しました。 オープンL2 - annoying
難易度 3/5 1〜2日 初心者へのやさしさ 45/100
markedjs/marked#4293 · コメント 2 件 ·
メンテナーはふだん 4 日以内に返信
-
proposal
難易度 5/5 1週間以上 初心者へのやさしさ 22/100
markedjs/marked#4282 · コメント 8 件 ·
メンテナーはふだん 4 日以内に返信
-
Quadratic time in the inline `text` rule: its email look-ahead rereads the rest of the paragraph対応中かも @yxshee が 10 日前に担当しました。 オープンL2 - annoying
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
メンテナーはふだん 4 日以内に返信
-
List item ends at a lazy line after a paragraph line indented 4+ columns対応中かも @hakarrr が 14 日前に担当しました。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 35/100
メンテナーはふだん 4 日以内に返信
markedjs/marked の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
メンテナーはふだん 1 日以内に返信
-
Make the "%" unit spacing the same in the web and in A10対応中かも @kragent66-glitch が今日担当しました。 オープンbug good first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
maiychrus25/OpenTea-DX-OS#226 · コメント 1 件 ·
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
nextcloud-libraries/nextcloud-dialogs#2772 ·
メンテナーはふだん 1 日以内に返信
-
Editor: leave an uploaded or inserted image's alt text empty, so the preview asks for a descriptionオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 83/100
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1〜3時間 初心者へのやさしさ 65/100
sveltejs/svelte.dev#2417 ·
メンテナーはふだん 1 日以内に返信