Skip to content

Fix Markdown preview crash on UTF-8 files with >2MB size and <1.5M characters#47391

Merged
MuyuanMS merged 3 commits into
mainfrom
copilot/fix-preview-pane-crash-utf8-markdown
May 18, 2026
Merged

Fix Markdown preview crash on UTF-8 files with >2MB size and <1.5M characters#47391
MuyuanMS merged 3 commits into
mainfrom
copilot/fix-preview-pane-crash-utf8-markdown

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 29, 2026

NavigateToString throws ArgumentException when previewing Markdown files containing many multi-byte UTF-8 characters (e.g., CJK) — file size exceeds 2MB but character count stays under 1.5M, bypassing the guard.

Summary of the Pull Request

The size guard in MarkdownPreviewHandlerControl used markdownHTML.Length (character count / UTF-16 code units), but WebView2's NavigateToString limit is measured in bytes. A string with 700K CJK characters has only 700K .Length units but ~2.1MB of UTF-8 bytes — enough to crash the API while passing the old check.

Changes:

  • MarkdownPreviewHandlerControl.cs: Replace character-count guard with UTF-8 byte count:

    // Before
    if (markdownHTML.Length > 1_500_000)
    
    // After
    if (System.Text.Encoding.UTF8.GetByteCount(markdownHTML) > 1_500_000)

    When the byte threshold is exceeded, content is written to a temp file and loaded via _browser.Source instead of NavigateToString — existing fallback path, now correctly triggered.

  • MarkdownPreviewHandlerTest.cs: Added 3 regression tests to prevent this class of bug from recurring:

    1. Multi-byte UTF-8 content (CJK, <1.5M chars but >2MB bytes) → temp-file navigation path
    2. Small ASCII content within both thresholds → NavigateToString path
    3. Large ASCII content exceeding 1.5M chars → temp-file navigation path

PR Checklist

  • Communication: I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected
  • Tests: Added/updated and all pass
  • Localization: All end-user-facing strings can be localized
  • Dev docs: Added/updated
  • New binaries: Added on the required places
  • Documentation updated: If checked, please file a pull request on our docs repo and link it here: #xxx

Detailed Description of the Pull Request / Additional comments

The existing fallback (write HTML to a temp file, navigate via _browser.Source) was already correct and handles arbitrarily large content safely. The only bug was in the guard condition that decides when to use it — it measured the wrong unit (characters vs. bytes). Single-byte ASCII content is unaffected; only multi-byte Unicode content was under-counted.

The tests use reflection to read the private _localFileURI field. Since this field is set synchronously before Controls.Add(_browser), the check is race-free: once the wait loop exits with Controls.Count > 0, _localFileURI is guaranteed to have its final value.

Validation Steps Performed

  • Verified the fix by reasoning through the byte math: a file with 700K CJK characters → Length = 700K (passes old check) → UTF-8 bytes ≈ 2.1MB (fails new check → uses temp file path, no crash).
  • Added 3 targeted unit tests in UnitTests-MarkdownPreviewHandler covering the multi-byte threshold boundary; all 14 tests in the suite pass (14/14).

Copilot AI changed the title [WIP] Fix preview pane crash on large UTF-8 markdown files Fix Markdown preview crash on UTF-8 files with >2MB size and <1.5M characters Apr 29, 2026
Copilot AI requested a review from MuyuanMS April 29, 2026 11:33
@niels9001 niels9001 added the Product-File Explorer Power Toys that touch explorer like Preview Pane label Apr 29, 2026
@MuyuanMS MuyuanMS marked this pull request as ready for review May 13, 2026 07:42
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates Markdown preview sizing logic so WebView2 avoids NavigateToString for HTML whose UTF-8 byte size exceeds the safe threshold, preventing crashes with large multi-byte Unicode Markdown content.

Changes:

  • Replaces string.Length threshold checking with Encoding.UTF8.GetByteCount.
  • Updates comments to clarify the WebView2 limit is byte-size sensitive.

if (markdownHTML.Length > 1_500_000)
// While testing the limit, it turned out it is ~1.5MB of UTF-8 encoded content, so to be on the safe side we check the UTF-8 byte count.
// Using character count (string.Length) is not sufficient because multi-byte UTF-8 characters (e.g. CJK) can cause the byte size to exceed the limit even when the character count is below it.
if (System.Text.Encoding.UTF8.GetByteCount(markdownHTML) > 1_500_000)
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.

Added the requested coverage in MarkdownPreviewHandlerTest.cs:

  • multi-byte UTF-8 content under 1.5M chars but over 2MB -> temp-file navigation
  • content under both thresholds -> NavigateToString
  • ASCII content over 1.5M chars -> temp-file navigation

The tests verify the navigation path by checking whether _localFileURI is populated and, for temp-file cases, that the WebView source points to the generated file. I also rebuilt the handler/test projects with MSBuild and ran PreviewPaneUnitTests via vstest.console.exe (14/14 passing).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@MuyuanMS
Copy link
Copy Markdown
Contributor

@copilot please re-review the latest changes.

@MuyuanMS MuyuanMS merged commit fb59905 into main May 18, 2026
16 checks passed
@LegendaryBlair LegendaryBlair added this to the PowerToys 0.100 milestone May 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Product-File Explorer Power Toys that touch explorer like Preview Pane

Projects

None yet

Development

Successfully merging this pull request may close these issues.

File Explorer: Preview Pane crashes on UTF-8 Markdown files with >2MB size and <1.5M characters

5 participants