Skip to content

FillConsoleOutputCharacterA crashes conhost when passed an invalid character #4258

@j4james

Description

@j4james

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.

int const iTarget = MultiByteToWideChar(codePage, 0, source.data(), iSource, nullptr, 0);
THROW_LAST_ERROR_IF(0 == iTarget);

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-ServerDown in the muck of API call servicing, interprocess communication, eventing, etc.Issue-BugIt either shouldn't be doing this or needs an investigation.Needs-Tag-FixDoesn't match tag requirementsPriority-1A description (P1)Product-ConhostFor issues in the Console codebaseResolution-Fix-CommittedFix is checked in, but it might be 3-4 weeks until a release.Severity-CrashCrashes are real bad news.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions