FillConsoleOutputCharacterA crashes conhost when passed an invalid character #5981

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

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

Environment

Windows build number: Version 10.0.18362.535

Steps to reproduce

Compile and run the following C program in a conhost shell:

#include <windows.h>

void main() {
    SetConsoleOutputCP(50220);
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    DWORD written;
    FillConsoleOutputCharacterA(handle, 14, 1, COORD{ 0,0 }, &written); 
}

Expected behavior

I believe codepoint 14 is invalid in the given codepage, so I would expect it to write out something like the unicode replacement character in the top left corner of the screen buffer, or possibly nothing at all. The legacy console seems to write out a null character.

Actual behavior

The conhost crashes.

What's happening is that the FillConsoleOutputCharacterAImpl method is calling ConvertToW with a string that can't be converted in the given codepage. And ConvertToW then throws an exception when MultiByteToWideChar returns 0.

9e5792ba51/src/types/convert.cpp (L41-L42)

And note that FillConsoleOutputCharacterAImpl is declared as noexcept, even though it quite clearly is capable of throwing exceptions. I've actually noticed a few cases like that - we may need to do an audit of our noexcept usage, and make sure it's being applied appropriately.

Originally created by @j4james on GitHub (Jan 16, 2020). # Environment ```none Windows build number: Version 10.0.18362.535 ``` # Steps to reproduce Compile and run the following C program in a conhost shell: ```c #include <windows.h> void main() { SetConsoleOutputCP(50220); HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); DWORD written; FillConsoleOutputCharacterA(handle, 14, 1, COORD{ 0,0 }, &written); } ``` # Expected behavior I believe codepoint 14 is invalid in the given codepage, so I would expect it to write out something like the unicode replacement character in the top left corner of the screen buffer, or possibly nothing at all. The legacy console seems to write out a null character. # Actual behavior The conhost crashes. What's happening is that the `FillConsoleOutputCharacterAImpl` method is calling `ConvertToW` with a string that can't be converted in the given codepage. And `ConvertToW` then throws an exception when `MultiByteToWideChar` returns 0. https://github.com/microsoft/terminal/blob/9e5792ba51236945b942e560469e1abd7fb93c22/src/types/convert.cpp#L41-L42 And note that `FillConsoleOutputCharacterAImpl` is declared as `noexcept`, even though it quite clearly is capable of throwing exceptions. I've actually noticed a few cases like that - we may need to do an audit of our `noexcept` usage, and make sure it's being applied appropriately.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#5981