microsoft / terminal

The new Windows Terminal and the original Windows console host, all in the same place!
MIT License
95.26k stars 8.27k forks source link

`CreatePseudoConsole` crashes due to long path to conpty.dll #16860

Closed segrey closed 1 month ago

segrey commented 7 months ago

Windows Terminal version

1.19.10573.0

Windows build number

10.0.22631.0

Other Software

No response

Steps to reproduce

  1. Install Windows Terminal v1.19.10573.0 to a directory with long full path: the parent directory of WindowsTerminal.exe should have length 167 chars or higher.
  2. Run WindowsTerminal.exe

Please see https://github.com/segrey/CreatePseudoConsole-crash-with-long-path, it automates the reproduction of this issue.

Expected Behavior

Windows Terminal runs normally.

Actual Behavior

Windows Terminal crashes at startup and finishes with exit code -1073740791 (hex: c0000409)

Event Viewer (Windows Log | Application) contains the following error:

Faulting application name: WindowsTerminal.exe, version: 1.19.2402.26003, time stamp: 0x65dce3ea
Faulting module name: ucrtbase.dll, version: 10.0.22621.2506, time stamp: 0x097c794c
Exception code: 0xc0000409
Fault offset: 0x000000000007df28
Faulting process id: 0x0x4424
Faulting application start time: 0x0x1DA739C0BB31B88
Faulting application path: D:\CreatePseudoConsole-crash-with-long-path\tmp\aaaaaaaa\aaaaaaaa\aaaaaaaa\aaaaaaaa\aaaaaaaa\aaaaaaaa\aaaaaaaa\aaaaaaaa\aaaaaaaa\aaaaaaaa\aaaaaaa\terminal-1.19.10573.0\WindowsTerminal.exe
Faulting module path: C:\WINDOWS\System32\ucrtbase.dll
Report Id: 201c4d23-ff42-474b-8764-703686bc06cc
Faulting package full name: 
Faulting package-relative application ID: 

Running the WindowsTerminal.exe with WinDbg shows that the problem is in CreatePseudoConsole function:

image

Looks like it's caused by crashing swprintf_s, because MAX_PATH (260) is too small to accomodate the full path. This is indirectly confirmed by the following: WindowsTerminal.exe doesn't crash when it's located in a directory with full path length 162 chars -- in this case the command line of OpenConsole.exe is 259 chars and everything works correctly:

"D:\CreatePseudoConsole-crash-with-long-path\tmp\aaaaaaaa\aaaaaaaa\aaaaaaaa\aaaaaaaa\aaaaaaaa\aaaaaaaa\aaaaaaaa\aaaaaaaa\aaaaaaaa\aaaaaaaa\aa\terminal-1.19.10573.0\OpenConsole.exe" --headless --resizeQuirk --width 120 --height 30 --signal 0xd28 --server 0xd20

It'd be great to fix this issue, because sometimes conpty.dll / OpenConsole.exe are located deep inside the file system.

DHowett commented 7 months ago

Wow, great catch!

a-usr commented 6 months ago

Im wondering, how did you even discover this in the first place? Are you one of those people that name their directories like Java Classes?

hurricup commented 6 months ago

@a-usr you may check the linked issue https://github.com/JetBrains/pty4j/issues/145 for the history

a-usr commented 6 months ago

@a-usr you may check the linked issue JetBrains/pty4j#145 for the history

To be honest I thought there was more to it. But thanks for the link!

hurricup commented 6 months ago

Nope, just tests involving running from gradle long paths failed all the sudden

segrey commented 6 months ago

@a-usr Initially, it was discovered by IntelliJ plugin developers. Also, please see IDEA-347968 for more details.

a-usr commented 6 months ago

Anyhow, awesome that you found/helped to find this bug!

segrey commented 1 month ago

@lhecker Thanks for fixing it. Do you know in when a stable Terminal v1.21 with the fix will be released? Seems the latest release (Windows Terminal v1.21.2361.0 as of now) doesn't contain it.

lhecker commented 1 month ago

I'll mark it up for a backport to 1.21. That will take probably about 2 weeks.

DHowett commented 3 weeks ago

I just pulled this into 1.21 after conflict resolution.