`LanguageParser.Parse` infinite loops on malformed input
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 72/100
Hướng nghiên cứu
Bắt đầu tại ColorCode.Parsing.LanguageParser.Parse, vòng lặp điều khiển Match.NextMatch() và giả định rằng vị trí quét tiến lên. Chạy đoạn mã console repro tối thiểu của issue với ngôn ngữ json để thấy nó bị treo, sau đó thêm một guard chống không tiến triển (dừng vòng lặp khi chỉ số và độ dài của match không di chuyển) để đầu vào sai định dạng bị suy giảm thay vì quay vòng. Xong khi repro trả về và các test parser hiện có vẫn pass.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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.
- Ngôn ngữ chính
- C#
- Star
- 267
- Fork
- 59
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của CommunityToolkit/ColorCode-Universal
-
Json.cs: Catastrophic regex backtracking in Regex_String causes extreme slowdown with JSON arraysCó thể đã có người làm @sandrock đã nhận 62 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
CommunityToolkit/ColorCode-Universal#45 · 2 bình luận ·
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 20/100
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 30/100
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 38/100
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 38/100
Tất cả issue của CommunityToolkit/ColorCode-Universal
Issue tương tự
-
needs-triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 64/100
microsoft/WinUI-Gallery#2290 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 63/100
Maintainer thường phản hồi trong vòng 1 ngày
-
quality 提案 要トリアージ
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 64/100
Maintainer thường phản hồi trong vòng 1 ngày
-
The Language and Region lists in Options read to a screen reader as "LanguageInfo { Id = ar, ... }"Đang mởbug localization small ui
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
hexide-io/HexIDE#4176 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày