Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FillConsoleOutputCharacterA crashes conhost when passed an invalid character #4258

Closed
j4james opened this issue Jan 16, 2020 · 2 comments · Fixed by #4309
Closed

FillConsoleOutputCharacterA crashes conhost when passed an invalid character #4258

j4james opened this issue Jan 16, 2020 · 2 comments · Fixed by #4309
Labels
Area-Server Down in the muck of API call servicing, interprocess communication, eventing, etc. Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Tag-Fix Doesn't match tag requirements Priority-1 A description (P1) Product-Conhost For issues in the Console codebase Resolution-Fix-Committed Fix is checked in, but it might be 3-4 weeks until a release. Severity-Crash Crashes are real bad news.
Milestone

Comments

@j4james
Copy link
Collaborator

j4james commented 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.

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.

@ghost ghost added Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting Needs-Tag-Fix Doesn't match tag requirements labels Jan 16, 2020
@zadjii-msft zadjii-msft added Area-Server Down in the muck of API call servicing, interprocess communication, eventing, etc. Issue-Bug It either shouldn't be doing this or needs an investigation. Product-Conhost For issues in the Console codebase Severity-Crash Crashes are real bad news. labels Jan 16, 2020
@ghost ghost removed the Needs-Tag-Fix Doesn't match tag requirements label Jan 16, 2020
@zadjii-msft zadjii-msft added this to the 21H1 milestone Jan 16, 2020
@zadjii-msft zadjii-msft added Priority-1 A description (P1) Area-Server Down in the muck of API call servicing, interprocess communication, eventing, etc. and removed Area-Server Down in the muck of API call servicing, interprocess communication, eventing, etc. labels Jan 16, 2020
@DHowett-MSFT DHowett-MSFT removed the Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting label Jan 16, 2020
@mkitzan
Copy link
Contributor

mkitzan commented 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).

@ghost ghost added the In-PR This issue has a related PR label Jan 21, 2020
@ghost ghost added Needs-Tag-Fix Doesn't match tag requirements and removed In-PR This issue has a related PR labels Feb 10, 2020
miniksa pushed a commit that referenced this issue Feb 10, 2020
## Summary of the Pull Request
Despite being specified as `noexcept`, `FillConsoleOutputCharacterA` emits an exception when a call to `ConvetToW` is made with an argument character which can't be converted. This PR fixes this throw, by wrapping `ConvertToW` in a try-catch_return.

## PR Checklist
* [x] Closes #4258
* [x] CLA signed. If not, go over [here](https://cla.opensource.microsoft.com/microsoft/Terminal) and sign the CLA
* [x] Tests added/passed: thanks @miniksa 

## Detailed Description of the Pull Request / Additional comments
Following the semantics of other `FillConsoleOutputCharacter*` the output param `cellsModified` is set to `0`. The try-catch_return is also what other functions of this family perform in case of errors.

## Validation Steps Performed
Original repro no longer crashes.
@ghost ghost added the Resolution-Fix-Committed Fix is checked in, but it might be 3-4 weeks until a release. label Feb 10, 2020
@ghost
Copy link

ghost commented Feb 13, 2020

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

Handy links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Server Down in the muck of API call servicing, interprocess communication, eventing, etc. Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Tag-Fix Doesn't match tag requirements Priority-1 A description (P1) Product-Conhost For issues in the Console codebase Resolution-Fix-Committed Fix is checked in, but it might be 3-4 weeks until a release. Severity-Crash Crashes are real bad news.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants