Skip to content

Fix UTF-8 BOM handling in MonacoPreviewHandler to prevent crashes#17

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/fix-16
Draft

Fix UTF-8 BOM handling in MonacoPreviewHandler to prevent crashes#17
Copilot wants to merge 2 commits intomainfrom
copilot/fix-16

Conversation

Copy link

Copilot AI commented May 25, 2025

Issue

PowerToys.MonacoPreviewHandler crashes when trying to render files with UTF-8 BOM (Byte Order Mark). This issue affects users who want to preview text files with UTF-8 BOM encoding in File Explorer's preview pane.

Root cause

When reading a file with UTF-8 BOM using StreamReader:

  1. In some cases, the BOM character (\uFEFF) can remain in the string content after ReadToEnd()
  2. When this content is later re-encoded to UTF-8 during base64 conversion:
    _base64FileCode = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(fileContent));
  3. The incorrectly handled BOM causes issues during rendering, leading to a crash

Solution

The fix adds an explicit check to detect and remove the BOM character after reading the file content:

// Remove BOM character if present to avoid encoding issues when converting to base64
// BOM character might be present as U+FEFF even though StreamReader should normally handle it
if (fileContent.Length > 0 && fileContent[0] == '\uFEFF')
{
    fileContent = fileContent.Substring(1);
    Logger.LogInfo("Removed UTF-8 BOM character from file content");
}

This ensures that any UTF-8 BOM is properly handled before base64 conversion, preventing the crash while maintaining correct rendering of the file content.

Fixes #16.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • cdn.fwupd.org
    • Triggering command: /usr/bin/fwupdmgr refresh (dns block)
  • crl3.digicert.com
    • Triggering command: dotnet build src/modules/previewpane/MonacoPreviewHandler/MonacoPreviewHandler.csproj (dns block)
  • ocsp.digicert.com
    • Triggering command: dotnet build src/modules/previewpane/MonacoPreviewHandler/MonacoPreviewHandler.csproj (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: lei9444 <39758135+lei9444@users.noreply.github.com>
Copilot AI changed the title [WIP] PowerToys.MonacoPreviewHandler crashes when rendering UTF-8 BOM Fix UTF-8 BOM handling in MonacoPreviewHandler to prevent crashes May 25, 2025
Copilot AI requested a review from lei9444 May 25, 2025 12:53
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.

PowerToys.MonacoPreviewHandler crashes when rendering UTF-8 BOM

2 participants