Skip to content

Fix panic when recording apps that output non-UTF-8 bytes#259

Merged
ibigbug merged 1 commit intoWatfaq:mainfrom
SteveSandersonMS:fix-non-utf8-stdout-panic
Jan 26, 2026
Merged

Fix panic when recording apps that output non-UTF-8 bytes#259
ibigbug merged 1 commit intoWatfaq:mainfrom
SteveSandersonMS:fix-non-utf8-stdout-panic

Conversation

@SteveSandersonMS
Copy link
Contributor

@SteveSandersonMS SteveSandersonMS commented Jan 23, 2026

Windows console in UTF-8 mode rejects non-UTF-8 byte sequences via std::io::stdout().write(), causing a panic.

This PR:

  • Uses WriteConsoleW to write directly to the console handle, bypassing Rust's UTF-8 validation
  • Buffers incomplete UTF-8 sequences across chunk boundaries to prevent replacement characters when multi-byte chars are split between reads

Fixes #243

- Use WriteConsoleW instead of std::io::stdout() to bypass UTF-8 validation
- Buffer incomplete UTF-8 sequences across chunk boundaries to prevent
  replacement characters when multi-byte chars are split

Fixes Watfaq#243
@SteveSandersonMS SteveSandersonMS force-pushed the fix-non-utf8-stdout-panic branch from 568d531 to ef2a6df Compare January 23, 2026 22:53
@ibigbug ibigbug requested a review from Copilot January 24, 2026 03:24
Copy link

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 fixes a panic that occurs when recording applications that output non-UTF-8 byte sequences on Windows. The issue stems from Windows console in UTF-8 mode rejecting invalid UTF-8 sequences through Rust's standard output validation.

Changes:

  • Replaces std::io::stdout().write() with direct WriteConsoleW calls to bypass Rust's UTF-8 validation
  • Implements buffering logic to handle incomplete UTF-8 sequences that may be split across chunk boundaries
  • Ensures proper Unicode handling by converting valid UTF-8 strings to UTF-16 before writing to console

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +127 to +134
#[cfg(windows)]
let stdout_handle: HANDLE =
unsafe { GetStdHandle(STD_OUTPUT_HANDLE).expect("failed to get stdout handle") };

// Buffer for incomplete UTF-8 sequences split across chunk boundaries
let mut pending_bytes: Vec<u8> = Vec::new();

loop {
Copy link

Copilot AI Jan 24, 2026

Choose a reason for hiding this comment

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

The stdout handle is retrieved once outside the loop, but if the handle becomes invalid during execution (e.g., console detachment), subsequent writes will fail. Consider retrieving the handle inside the loop or adding error handling to detect and recover from invalid handle states.

Suggested change
#[cfg(windows)]
let stdout_handle: HANDLE =
unsafe { GetStdHandle(STD_OUTPUT_HANDLE).expect("failed to get stdout handle") };
// Buffer for incomplete UTF-8 sequences split across chunk boundaries
let mut pending_bytes: Vec<u8> = Vec::new();
loop {
// Buffer for incomplete UTF-8 sequences split across chunk boundaries
let mut pending_bytes: Vec<u8> = Vec::new();
loop {
#[cfg(windows)]
let stdout_handle: HANDLE =
unsafe { GetStdHandle(STD_OUTPUT_HANDLE).expect("failed to get stdout handle") };

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

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

@SteveSandersonMS do you think this comment makes sense? (i'm not a Windows console expert :))

Copy link
Member

@ibigbug ibigbug left a comment

Choose a reason for hiding this comment

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

wow thank you so much! bar the Copilot comment

@ibigbug ibigbug merged commit 52322d5 into Watfaq:main Jan 26, 2026
2 checks passed
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.

Runtime Panic "Windows stdio in console mode does not support writing non-UTF-8 byte sequences"

2 participants