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

Đang mở
#4,286 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 4 ngày

@Pastalikek65 đang làm issue này rồi.

Từ ngày 27/11/2026.

  • #4287 của @Pastalikek65 — đang mở

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
25/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
node.js, typescript
Lĩnh vực
backend, performance

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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

Ngôn ngữ chính
TypeScript
Star
37.3k
Fork
3.9k
Merge trung bình
5 ngày 6 giờ
Pull request đã merge (30 ngày)
62

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của markedjs/marked

Tất cả issue của markedjs/marked

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.