Skip to content

regexp engine OOMs on huge repeat count (e.g. b\{1000000000\}) #2

Description

@Haoxincode

Summary

MoonBit 的 @moonbitlang/core/regexp 引擎在遇到超大重复次数(如 b\{1000000000\})时,会尝试分配十亿级别的内存,导致 V8 进程 OOM 崩溃。

复现

grep spec 测试 tests/spec/grep/cases/gnu-bre.tests 第 86 行:

2@b\{1000000000\}@ESIZE

期望行为:grep 应返回 exit code 2 并报告 ESIZE 错误(正则表达式太大)。
实际行为:MoonBit regexp 引擎尝试编译该正则,V8 进程 OOM 崩溃。

根因分析

@moonbitlang/core/regexp 在编译 {n} 重复量词时,没有对 n 设置上限。当 n = 1000000000 时,引擎可能尝试展开或分配与重复次数成正比的内部结构,导致内存耗尽。

建议修复

在 MoonBash 的 grep 命令实现中,编译正则之前检查重复量词的上界。如果超过合理阈值(如 10000),直接返回错误而不传给 regexp 引擎。

或者,向 @moonbitlang/core/regexp 上游提 issue,请求增加编译期的重复次数上限保护。

临时规避

已在 tests/spec/grep/skips.ts 中跳过该测试用例。

环境

  • MoonBash main 分支
  • Node.js v24, V8 heap 4096MB–6144MB
  • 16GB RAM 机器上 100% 复现

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions