`LanguageParser.Parse` infinite loops on malformed input
还没有人认领这个 Issue。
评估
调研方向
从 ColorCode.Parsing.LanguageParser.Parse 开始,这是驱动 Match.NextMatch() 并假定扫描位置会前进的循环。用 json 语言运行 issue 中最小 repro 控制台片段,观察它卡住,然后添加一个无进展保护(当匹配索引和长度不再移动时跳出),使格式错误的输入降级而不是空转。当 repro 返回且现有 parser 测试仍然通过时即完成。
由索引模型根据 Issue 内容生成。
描述
AI Disclosure: The bug report below is being co-written by AI.
ColorCode.Core 2.0.15 (CommunityToolkit/ColorCode-Universal)
ColorCode.Parsing.LanguageParser.Parse enters a non-terminating loop for certain malformed source strings, burning a CPU core forever on the calling thread. In our case this was on the UI thread, so the entire app froze.
Why is this important
There are many cases where there can be invalid input passed into the library. For example, AI chat rendering that AI writes the code that need to be syntax highlighted.
Reproduction
Highlighting the following source with the json language:
state.status = "completed"
state.metadata = { "status": "running", "shellID": "sh_1df528…", "pid": 133242 }
content = [
"Command moved to the background (shell ID: sh_1df528…).…\nOutput is streaming to: …/sh_1df528…out",
"You will be notified automatically when the command finishes.…"
]
The body is not valid JSON. LanguageParser.Parse does not return.
Minimal repro:
using System;
using System.Collections.Generic;
using System.Threading;
using ColorCode;
using ColorCode.Common;
using ColorCode.Compilation;
using ColorCode.Parsing;
var malformed = """
state.status = "completed"
state.metadata = { "status": "running", "shellID": "sh_1df528…", "pid": 133242 }
content = [
"Command moved to the background (shell ID: sh_1df528…).…\nOutput is streaming to: …/sh_1df528…out",
"You will be notified automatically when the command finishes.…"
]
""";
var compiler = new LanguageCompiler(new Dictionary<string, CompiledLanguage>(), new ReaderWriterLockSlim());
var repo = new LanguageRepository(new Dictionary<string, ILanguage> { ["json"] = Languages.FindById("json") });
var parser = new LanguageParser(compiler, repo);
parser.Parse(malformed, Languages.FindById("json"), (parsed, scopes) => { }); // never returns
Expected behavior
Parse returns (the input is simply malformed; highlighting should degrade gracefully or throw a parse error, not hang).
Actual behavior
Parse spins forever in Regex.Match.NextMatch() and never terminates. The thread is unkillable and consumes 100% CPU indefinitely.
Stack trace (from a real hang)
System.Text.RegularExpressions.RegexInterpreter.TryMatchAtCurrentPosition
System.Text.RegularExpressions.RegexInterpreter.Scan
System.Text.RegularExpressions.Regex.ScanInternal
System.Text.RegularExpressions.Regex.RunSingleMatch
System.Text.RegularExpressions.Match.NextMatch // non-terminating loop
ColorCode.Parsing.LanguageParser.Parse
Root-cause hypothesis
A language rule matches without advancing the scan position, so Match.NextMatch() keeps returning matches at the same position forever. I suspect one of the json language's regex rules can produce a zero-width match on this input, causing the loop in LanguageParser.Parse (which drives the regex and relies on position advancing) to never make progress.
Suggested fix area
LanguageParser.Parse: guard against no-progress iterations (e.g. bail ifmatch.Index/ match length doesn't advance), or add a safety bound on iterations.- Alternatively, consider a timeout/token the caller can use — though note .NET has no way to abort a non-terminating regex, so a prevention guard (zero-width match detection) is more robust than cancellation.
- 主要语言
- C#
- 星标
- 267
- 派生
- 59
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
CommunityToolkit/ColorCode-Universal 的其他 Issue
-
Json.cs: Catastrophic regex backtracking in Regex_String causes extreme slowdown with JSON arrays可能已有人在做 @sandrock 于 62 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 74/100
CommunityToolkit/ColorCode-Universal#45 · 2 条评论 ·
-
难度 5/5 一周以上 新手友好度 20/100
-
难度 3/5 1-2 天 新手友好度 30/100
-
难度 3/5 1-2 天 新手友好度 38/100
-
难度 3/5 1-2 天 新手友好度 38/100
查看 CommunityToolkit/ColorCode-Universal 的全部 Issue
相似的 Issue
-
needs-triage
难度 2/5 1-3 小时 新手友好度 64/100
microsoft/WinUI-Gallery#2290 ·
-
难度 2/5 1-3 小时 新手友好度 63/100
维护者通常 1 天内回复
-
quality 提案 要トリアージ
难度 2/5 1-3 小时 新手友好度 64/100
维护者通常 1 天内回复
-
The Language and Region lists in Options read to a screen reader as "LanguageInfo { Id = ar, ... }"未关闭bug localization small ui
难度 2/5 1-3 小时 新手友好度 72/100
hexide-io/HexIDE#4176 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 1 天内回复