Potential crash when calling ITerminalConnection::WriteInput #22082

Closed
opened 2026-01-31 08:02:59 +00:00 by claunia · 0 comments
Owner

Originally created by @j4james on GitHub (Aug 10, 2024).

Windows Terminal version

Commit edfa3ea0f0

Windows build number

10.0.19045.4651

Other Software

No response

Steps to reproduce

  1. Patch the DeviceAttributes implementation in adaptDispatch.cpp to respond like this:
    _api.ReturnResponse({L"ABCD", 3});
    
  2. Open a WSL shell in OpenConsole and execute this:
    printf "\e[c"; read
    
  3. Note that it successfully responds with ABC.
  4. Now try the same thing in a WSL shell from within Windows Terminal.

Expected Behavior

It should also respond successfully with ABC.

Actual Behavior

The terminal crashes.

The ReturnResponse method takes a std::wstring_view, but the Windows Terminal implementation calls ControlCore::_sendInputToConnection, which calls ITerminalConnection::WriteInput, which expects an hstring. When constructing an hstring from a std::wstring_view it's required that the string be null-terminated, otherwise it aborts. And since std::wstring_view is obviously not guaranteed to be null-terminated, this is asking for trouble.

I know the example I've given above is contrived, but that's just because it was the easiest way to demonstrate the problem. There are a number of places in the AdaptDispatch class that are calling ReturnResponse with a fmt::basic_memory_buffer, and that's not guaranteed to be null-terminated. It's just a matter of luck whether that code crashes or not.

Originally created by @j4james on GitHub (Aug 10, 2024). ### Windows Terminal version Commit edfa3ea0f0080eadf7f01b463ed0a8638de6ce04 ### Windows build number 10.0.19045.4651 ### Other Software _No response_ ### Steps to reproduce 1. Patch the `DeviceAttributes` implementation in adaptDispatch.cpp to respond like this: ```cpp _api.ReturnResponse({L"ABCD", 3}); ``` 2. Open a WSL shell in OpenConsole and execute this: ``` printf "\e[c"; read ``` 3. Note that it successfully responds with `ABC`. 4. Now try the same thing in a WSL shell from within Windows Terminal. ### Expected Behavior It should also respond successfully with `ABC`. ### Actual Behavior The terminal crashes. The `ReturnResponse` method takes a `std::wstring_view`, but the Windows Terminal implementation calls `ControlCore::_sendInputToConnection`, which calls `ITerminalConnection::WriteInput`, which expects an `hstring`. When constructing an `hstring` from a `std::wstring_view` it's required that the string be null-terminated, otherwise it aborts. And since `std::wstring_view` is obviously not guaranteed to be null-terminated, this is asking for trouble. I know the example I've given above is contrived, but that's just because it was the easiest way to demonstrate the problem. There are a number of places in the `AdaptDispatch` class that are calling `ReturnResponse` with a `fmt::basic_memory_buffer`, and that's not guaranteed to be null-terminated. It's just a matter of luck whether that code crashes or not.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#22082