FillConsoleOutputCharacterA crashes conhost when passed an invalid character #5984

Closed
opened 2026-01-31 00:27:09 +00:00 by claunia · 2 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.
Author
Owner

@mkitzan commented on GitHub (Jan 21, 2020):

I'll PR a fix to this issue by wrapping the throwing call in a try-catch and making sure the output parameter cellsModified is set to 0 (which is what other FillConsoleOutputCharacter* do).

I'm down to do a noexcept audit, but it'll tie me up for bit. @DHowett-MSFT, let me know if it'd be more helpful to improve the settings warnings (#4299, #4239, #2440) for the time being instead (re:Email).

@mkitzan commented on GitHub (Jan 21, 2020): I'll PR a fix to this issue by wrapping the throwing call in a try-catch and making sure the output parameter `cellsModified` is set to `0` (which is what other `FillConsoleOutputCharacter*` do). I'm down to do a `noexcept` audit, but it'll tie me up for bit. @DHowett-MSFT, let me know if it'd be more helpful to improve the settings warnings (#4299, #4239, #2440) for the time being instead (re:Email).
Author
Owner

@ghost commented on GitHub (Feb 13, 2020):

:tada:This issue was addressed in #4309, which has now been successfully released as Windows Terminal Preview v0.9.433.0.🎉

Handy links:

@ghost commented on GitHub (Feb 13, 2020): :tada:This issue was addressed in #4309, which has now been successfully released as `Windows Terminal Preview v0.9.433.0`.:tada: Handy links: * [Release Notes](https://github.com/microsoft/terminal/releases/tag/v0.9.433.0) * [Store Download](https://www.microsoft.com/store/apps/9n0dx20hk701?cid=storebadge&ocid=badge)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#5984