`LanguageParser.Parse` infinite loops on malformed input
まだ誰も着手していません。
評価
調査の方向性
ColorCode.Parsing.LanguageParser.Parse から始めてください。これは Match.NextMatch() を駆動し、スキャン位置が進むことを前提とするループです。issue の最小 repro コンソールスニペットを json 言語で実行してハングするのを確認し、次に進捗なしガードを追加してください(マッチのインデックスと長さが動かない場合にループを抜ける)。これにより、不正な入力は回り続けるのではなく縮退します。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 にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- 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 1週間以上 初心者へのやさしさ 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 日以内に返信