Skip to content

[1294] 修复自递归/循环宏 LaTeX 粘贴导致进程崩溃(用例 249/289/pattern_c) - #4535

Merged
da-liii merged 7 commits into
mainfrom
da/1294/latex_crash_1
Sep 10, 2026
Merged

[1294] 修复自递归/循环宏 LaTeX 粘贴导致进程崩溃(用例 249/289/pattern_c)#4535
da-liii merged 7 commits into
mainfrom
da/1294/latex_crash_1

Conversation

@da-liii

@da-liii da-liii commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

问题

LaTeX 宏递归崩溃用例:

  • 用例 249 (1294_1.tex): 无参自递归宏 \def\crashMacro24{\crashMacro24} \crashMacro24
  • 用例 289 (1294_2.tex): 带参自递归宏 \def\paramRec28#1{\paramRec28{#1}#1} \paramRec28{x}
  • 用例 pattern_c (1294_3.tex): 相互递归宏 \newcommand{\macroA}{\macroB} \newcommand{\macroB}{\macroA} \macroA

GUI「编辑 → 粘贴自 → LaTeX」后进程崩溃退出(SIGSEGV 栈溢出或未捕获异常终止进程)。

根因

  1. LaTeX 转换层面
    递归宏会被转换解析为带有引用环路的宏定义。
  2. 排版引擎层面
    在文档被插入或排版时,concater_rep::typeset_compound 展开宏体,遇到递归宏无展开深度限制,调用栈超过 47,000+ 帧直至栈溢出(SIGSEGV)。
  3. 字体解析兜底
    缺 TeX 字体时 load_tex 抛出 string 异常穿过 scheme 边界导致进程终止;find_font 规则成环导致栈溢出。

改动

  • 排版引擎层保护
    • src/Typeset/env.hpp & src/Typeset/Env/env.cppedit_env 增加 macro_depth 深度计数。
    • src/Typeset/Concat/concat_macro.cppmacro_depth_guard 限制宏展开深度(上限 100),超限降级占位 box,杜绝无限递归死循环。
  • LaTeX 转换层保护
    • src/Plugins/Tex/fromtex_post.cpphas_macro_cycle 拓扑成环检测,检测到递归宏定义时直接丢弃并警告返回空文档,避免向用户文档插入死循环内容;入口兜底捕获异常。
    • src/Graphics/Fonts/find_font.cppfind_font 增加递归深度上限。
  • 测试与验证
    • tests/Plugins/Tex/parsetex_test.cpp:C++ 回归测试,覆盖用例 249、289、1294_3。
    • TeXmacs/tests/tex/1294_{1,2,3}.tex:三个复现用例样本落盘。
    • 1294.py / TeXmacs/tests/python/1294.py:自动化 GUI 回归测试脚本。
    • devel/1294.md:任务文档更新。

测试

  • xmake b parsetex_test && xmake r parsetex_test:5 passed
  • python3 1294.py:1294_1.tex、1294_2.tex、1294_3.tex 均 PASS
  • xmake r 0620 & xmake r 0631:通过

da-liii and others added 6 commits September 10, 2026 10:11
- latex_to_tree 入口兜底捕获底层字体解析抛出的 string 异常,
  降级返回空文档,避免异常穿过 scheme 边界终止进程
- find_font 增加递归深度上限,规则成环时按未找到降级,防止栈溢出

Co-Authored-By: Claude <noreply@anthropic.com>
- concat_macro.cpp: max_macro_depth 提升为文件级常量,两处超限检查收敛为
  concater_rep::macro_depth_exceeded(消除 typeset_auto 静默不降级的漂移)
- find_font.cpp: 手写计数+try/catch 回退改为 RAII find_font_guard,
  与 macro_depth_guard 同构
- fromtex_post.cpp: tree_calls_macro 删除被子节点递归覆盖的 COMPOUND/APPLY
  特判及 per-node as_string 标签比较
- parsetex_test.cpp: 三个用例收敛为 check_crash_case helper,删除空的
  cleanupTestCase
- 删除根目录重复的 1294.py,保留 TeXmacs/tests/python/ 下的规范位置副本

Co-Authored-By: Claude <noreply@anthropic.com>
@da-liii da-liii changed the title [1294] 修复 LaTeX 粘贴用例 249 导致进程崩溃 [1294] 修复自递归/循环宏 LaTeX 粘贴导致进程崩溃(用例 249/289/pattern_c) Sep 10, 2026

@MoonL79 MoonL79 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@da-liii
da-liii merged commit 2a829f3 into main Sep 10, 2026
6 checks passed
@da-liii
da-liii deleted the da/1294/latex_crash_1 branch September 10, 2026 06:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants