From 23632e9104b6506d5bee0d85b807c2351617d226 Mon Sep 17 00:00:00 2001 From: Morten Engelhardt Olsen Date: Wed, 29 Jan 2025 14:51:06 -0700 Subject: [PATCH] win: check cwd length before spawning a child process The CreateProcess API on Windows is still not longPathAware, even if the process itself is. So, if the cwd used for CreateProcess is too long, then the call fails with a 'INVALID_DIRECTORY' error. To deal with this, check the length of the cwd and shorten it if it is longer than MAX_PATH. --- src/win/process.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/win/process.c b/src/win/process.c index 9d48ddc6..27605ca3 100644 --- a/src/win/process.c +++ b/src/win/process.c @@ -898,7 +898,7 @@ int uv_spawn(uv_loop_t* loop, *env = NULL, *cwd = NULL; STARTUPINFOW startup; PROCESS_INFORMATION info; - DWORD process_flags; + DWORD process_flags, cwd_len; BYTE* child_stdio_buffer; uv__process_init(loop, process); @@ -947,9 +947,10 @@ int uv_spawn(uv_loop_t* loop, if (err) goto done_uv; + cwd_len = wcslen(cwd); } else { /* Inherit cwd */ - DWORD cwd_len, r; + DWORD r; cwd_len = GetCurrentDirectoryW(0, NULL); if (!cwd_len) { @@ -970,6 +971,15 @@ int uv_spawn(uv_loop_t* loop, } } + /* If cwd is too long, shorten it */ + if (cwd_len >= MAX_PATH) { + cwd_len = GetShortPathNameW(cwd, cwd, cwd_len); + if (cwd_len == 0) { + err = GetLastError(); + goto done; + } + } + /* Get PATH environment variable. */ path = find_path(env); if (path == NULL) {