Switching between buffers while a cooked read is active crashes conhost #21925

Closed
opened 2026-01-31 07:58:29 +00:00 by claunia · 0 comments
Owner

Originally created by @lhecker on GitHub (Jul 4, 2024).

Windows build number

10.0.26120.0

Steps to reproduce

Run the following code:

#include <Windows.h>

int main() {
    SetConsoleOutputCP(CP_UTF8);
    SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
    SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING | DISABLE_NEWLINE_AUTO_RETURN);

    // Disabled the alternate screen buffer while a cooked read is ongoing.
    CreateThread(
        nullptr,
        0,
        [](void*) -> DWORD {
            Sleep(1000);
            WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), "\x1b[?1049l", 8, nullptr, nullptr);
            return 0;
        },
        nullptr,
        0,
        nullptr
    );

    char buf[128];
    WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), "\x1b[?1049h", 8, nullptr, nullptr);
    ReadFile(GetStdHandle(STD_INPUT_HANDLE), &buf[0], 128, nullptr, nullptr);
    return 0;
}

and press any (visible character) key.

Actual Behavior

The crash occurs because we don't cancel (or update) the pending cooked read in SCREEN_INFORMATION::UseAlternateScreenBuffer / UseMainScreenBuffer. Any version of conhost that supports alt buffers crashes with the above repro. Since support for ASB has existed for a while I'd not give this a P0, even if it's a stale pointer read (= bad, because unpredictable).

Originally created by @lhecker on GitHub (Jul 4, 2024). ### Windows build number 10.0.26120.0 ### Steps to reproduce Run the following code: ```cpp #include <Windows.h> int main() { SetConsoleOutputCP(CP_UTF8); SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT); SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING | DISABLE_NEWLINE_AUTO_RETURN); // Disabled the alternate screen buffer while a cooked read is ongoing. CreateThread( nullptr, 0, [](void*) -> DWORD { Sleep(1000); WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), "\x1b[?1049l", 8, nullptr, nullptr); return 0; }, nullptr, 0, nullptr ); char buf[128]; WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), "\x1b[?1049h", 8, nullptr, nullptr); ReadFile(GetStdHandle(STD_INPUT_HANDLE), &buf[0], 128, nullptr, nullptr); return 0; } ``` and press any (visible character) key. ### Actual Behavior The crash occurs because we don't cancel (or update) the pending cooked read in `SCREEN_INFORMATION::UseAlternateScreenBuffer` / `UseMainScreenBuffer`. Any version of conhost that supports alt buffers crashes with the above repro. Since support for ASB has existed for a while I'd not give this a P0, even if it's a stale pointer read (= bad, because unpredictable).
claunia added the Product-ConhostIssue-BugNeeds-Tag-FixPriority-1Area-CookedRead labels 2026-01-31 07:58:29 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#21925