Crash when switching back from the alt buffer #22089

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

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

Originally assigned to: @lhecker on GitHub.

Windows Terminal version

Commit edfa3ea0f0

Windows build number

10.0.19045.4651

Other Software

No response

Steps to reproduce

It's not easy to reproduce, but I think all you really need to trigger the crash is to switch to the alt buffer with printf "\e[?1049h", and then switch back to the main buffer with printf "\e[?1049l".

This is in Windows Terminal using a build that includes the new VT passthrough (PR #17510).

Expected Behavior

The terminal should not crash.

Actual Behavior

I got an access violation accessing the screenInfo parameter in the WriteCharsVT function here:

0199ca33dd/src/host/_stream.cpp (L353)

What happens is that we first pass the \e[?1049l sequence through to conhost in the stateMachine.ProcessString call a couple of lines above. That ends up calling SCREEN_INFORMATION::UseMainScreenBuffer, which calls s_RemoveScreenBuffer(psiAlt), which deletes the active screenInfo instance here:

0199ca33dd/src/host/screenInfo.cpp (L231)

That's the same screenInfo instance that was passed to our WriteCharsVT function, so when we later try to reference that, we're referencing a deleted object. Sometimes we get lucky and nothing bad happens, but sometimes it will crash.

I think maybe all we need to do to prevent the crash is to save the screenInfo.OutputMode at the start of the function, and then use that saved value instead of trying to look it up via the screenInfo object.

Originally created by @j4james on GitHub (Aug 13, 2024). Originally assigned to: @lhecker on GitHub. ### Windows Terminal version Commit edfa3ea0f0080eadf7f01b463ed0a8638de6ce04 ### Windows build number 10.0.19045.4651 ### Other Software _No response_ ### Steps to reproduce It's not easy to reproduce, but I think all you really need to trigger the crash is to switch to the alt buffer with `printf "\e[?1049h"`, and then switch back to the main buffer with `printf "\e[?1049l"`. This is in Windows Terminal using a build that includes the new VT passthrough (PR #17510). ### Expected Behavior The terminal should not crash. ### Actual Behavior I got an access violation accessing the `screenInfo` parameter in the `WriteCharsVT` function here: https://github.com/microsoft/terminal/blob/0199ca33ddb4a02ca3549627d772ce6b330ed1ce/src/host/_stream.cpp#L353 What happens is that we first pass the `\e[?1049l` sequence through to conhost in the `stateMachine.ProcessString` call a couple of lines above. That ends up calling `SCREEN_INFORMATION::UseMainScreenBuffer`, which calls `s_RemoveScreenBuffer(psiAlt)`, which deletes the active `screenInfo` instance here: https://github.com/microsoft/terminal/blob/0199ca33ddb4a02ca3549627d772ce6b330ed1ce/src/host/screenInfo.cpp#L231 That's the same `screenInfo` instance that was passed to our `WriteCharsVT` function, so when we later try to reference that, we're referencing a deleted object. Sometimes we get lucky and nothing bad happens, but sometimes it will crash. I think maybe all we need to do to prevent the crash is to save the `screenInfo.OutputMode` at the start of the function, and then use that saved value instead of trying to look it up via the `screenInfo` object.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#22089