Null character in the Windows Terminal causes subsequent characters to be dropped #2746

Open
opened 2026-01-30 23:04:02 +00:00 by claunia · 0 comments
Owner

Originally created by @j4james on GitHub (Jul 13, 2019).

Environment

Windows build number: 10.0.18362.175
Windows Terminal version (if applicable): Locally built from commit 3377f06

Steps to reproduce

  1. Build the Windows Terminal locally, and run Windows Terminal (Dev build) from the Start menu.

  2. When the default PowerShell tab opens, type the following command:

     echo "BEFORE `0 AFTER"
    

Expected behavior

I'd expect to see it output something like this:

BEFORE   AFTER

Actual behavior

The output ends at the null character, and the AFTER part of the string is not displayed.

image

Sometimes I don't even see the following command prompt - I assume it depends on how much content is buffered before it's output.

I can't reproduce this in the released version of Windows Terminal, though - it only happens for me with a locally built version. But looking at the code in the debugger, I can see why it is failing, so I'm really confused why it would ever work.

If you look at the TermControl::_InitializeTerminal method, there's a lambda expression which takes the terminal output, in the form of an hstring, and passes it on to the Terminal::Write method, which expects a wstring_view.

3377f06e52/src/cascadia/TerminalControl/TermControl.cpp (L418-L420)

But because it's using the c_str method on the hstring, it's losing the length of the data, so the wstring_view is forced to guess the length from the null terminator (thus losing half the characters). If that hstring had been passed directly to the Write method, it would actually have worked, because hstring has a cast operator which automatically converts to a wstring_view, and retains the real length.

Btw, I have actually built a version of the code with the c_str() call removed, and confirmed that it does fix the problem for me.

Originally created by @j4james on GitHub (Jul 13, 2019). # Environment Windows build number: 10.0.18362.175 Windows Terminal version (if applicable): Locally built from commit [3377f06](https://github.com/microsoft/terminal/commit/3377f06e52914fbe5b536f74b234c5e7173dfaf2) # Steps to reproduce 1. Build the Windows Terminal locally, and run _Windows Terminal (Dev build)_ from the _Start_ menu. 2. When the default PowerShell tab opens, type the following command: echo "BEFORE `0 AFTER" # Expected behavior I'd expect to see it output something like this: BEFORE AFTER # Actual behavior The output ends at the null character, and the `AFTER` part of the string is not displayed. ![image](https://user-images.githubusercontent.com/4181424/61172353-f36b4f00-a57a-11e9-9312-cda81a3ebb44.png) Sometimes I don't even see the following command prompt - I assume it depends on how much content is buffered before it's output. I can't reproduce this in the released version of Windows Terminal, though - it only happens for me with a locally built version. But looking at the code in the debugger, I can see why it is failing, so I'm really confused why it would ever work. If you look at the `TermControl::_InitializeTerminal` method, there's a lambda expression which takes the terminal output, in the form of an `hstring`, and passes it on to the `Terminal::Write` method, which expects a `wstring_view`. https://github.com/microsoft/terminal/blob/3377f06e52914fbe5b536f74b234c5e7173dfaf2/src/cascadia/TerminalControl/TermControl.cpp#L418-L420 But because it's using the `c_str` method on the `hstring`, it's losing the length of the data, so the `wstring_view` is forced to guess the length from the null terminator (thus losing half the characters). If that `hstring` had been passed directly to the `Write` method, it would actually have worked, because `hstring` has a cast operator which automatically converts to a `wstring_view`, and retains the real length. Btw, I have actually built a version of the code with the `c_str()` call removed, and confirmed that it does fix the problem for me.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#2746