C0 characters in screen buffer are incorrectly interpreted as controls by conpty #6149

Open
opened 2026-01-31 00:31:05 +00:00 by claunia · 0 comments
Owner

Originally created by @j4james on GitHub (Jan 26, 2020).

Environment

Windows build number: Version 10.0.18362.535
Windows Terminal version (if applicable): 0.8.10091.0

Steps to reproduce

Compile the follow C++ program, and then run the resulting executable from a cmd shell in the Windows Terminal.

#include <windows.h>
#include <string>

void main() {
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);

    // Enable VT mode.
    DWORD mode;
    GetConsoleMode(handle, &mode);
    mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
    SetConsoleMode(handle, mode);

    // Write directly to the screen buffer with control chars.
    DWORD written = 0;
    const std::wstring text = L"\x1B[31mIs this text red?\nIs this on a new line?\n";
    WriteConsoleOutputCharacterW(handle, text.c_str(), text.length(), COORD{ 0, 0 }, &written);
}

Expected behavior

The WriteConsoleOutputCharacterW api writes directly to the screen buffer, without attempting to process any control characters. I would thus expect the text message to be output entirely on the first line of the screen buffer, with the ESC and LF control characters output as glyphs, and not interpreted. This is what it looks like in a conhost cmd shell:

image

Note that the ENABLE_VIRTUAL_TERMINAL_PROCESSING mode should have no effect on this API. I've only turned it on to make sure the code is running under the same conditions in conhost as it is in WT (which enables VT mode by default).

Actual behavior

In the Windows Terminal, the ESC and LF characters are interpreted as control characters instead of being output as glyphs. As a result, you get an escape sequence changing the text color to red, and the text will be output over multiple lines.

Depending on timing, you can also end up with other parts of the screen being redrawn incorrectly, as the terminal cursor position is now out of sync with where the conhost thinks it is (notice part of the copyright message being redrawn two lines down in red).

image

Originally created by @j4james on GitHub (Jan 26, 2020). # Environment ```none Windows build number: Version 10.0.18362.535 Windows Terminal version (if applicable): 0.8.10091.0 ``` # Steps to reproduce Compile the follow C++ program, and then run the resulting executable from a cmd shell in the Windows Terminal. ```cpp #include <windows.h> #include <string> void main() { HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); // Enable VT mode. DWORD mode; GetConsoleMode(handle, &mode); mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING; SetConsoleMode(handle, mode); // Write directly to the screen buffer with control chars. DWORD written = 0; const std::wstring text = L"\x1B[31mIs this text red?\nIs this on a new line?\n"; WriteConsoleOutputCharacterW(handle, text.c_str(), text.length(), COORD{ 0, 0 }, &written); } ``` # Expected behavior The `WriteConsoleOutputCharacterW` api writes directly to the screen buffer, without attempting to process any control characters. I would thus expect the text message to be output entirely on the first line of the screen buffer, with the `ESC` and `LF` control characters output as glyphs, and not interpreted. This is what it looks like in a conhost cmd shell: ![image](https://user-images.githubusercontent.com/4181424/73140021-13bcb000-406c-11ea-85ae-fe8f8f3cbc81.png) Note that the `ENABLE_VIRTUAL_TERMINAL_PROCESSING` mode should have no effect on this API. I've only turned it on to make sure the code is running under the same conditions in conhost as it is in WT (which enables VT mode by default). # Actual behavior In the Windows Terminal, the `ESC` and `LF` characters are interpreted as control characters instead of being output as glyphs. As a result, you get an escape sequence changing the text color to red, and the text will be output over multiple lines. Depending on timing, you can also end up with other parts of the screen being redrawn incorrectly, as the terminal cursor position is now out of sync with where the conhost thinks it is (notice part of the copyright message being redrawn two lines down in red). ![image](https://user-images.githubusercontent.com/4181424/73140041-59797880-406c-11ea-90cc-2e5270074b0d.png)
claunia added the Area-OutputIssue-BugIn-PRArea-VTNeeds-Tag-FixProduct-Conpty labels 2026-01-31 00:31:05 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#6149