From c97017dd1d8cdd02c1a7aeae39b7336babc313d7 Mon Sep 17 00:00:00 2001 From: Kyle Edwards Date: Mon, 14 Aug 2023 15:25:11 -0400 Subject: [PATCH] win,spawn: allow `%PATH%` to be unset (#4116) Fix: https://github.com/libuv/libuv/issues/4115 --- src/win/process.c | 29 +++++++++++++---------------- test/test-list.h | 2 ++ test/test-spawn.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 16 deletions(-) diff --git a/src/win/process.c b/src/win/process.c index 3e451e22..3cfa89e7 100644 --- a/src/win/process.c +++ b/src/win/process.c @@ -396,7 +396,7 @@ static WCHAR* search_path(const WCHAR *file, name_has_ext); while (result == NULL) { - if (*dir_end == L'\0') { + if (dir_end == NULL || *dir_end == L'\0') { break; } @@ -1025,22 +1025,19 @@ int uv_spawn(uv_loop_t* loop, DWORD path_len, r; path_len = GetEnvironmentVariableW(L"PATH", NULL, 0); - if (path_len == 0) { - err = GetLastError(); - goto done; - } + if (path_len != 0) { + alloc_path = (WCHAR*) uv__malloc(path_len * sizeof(WCHAR)); + if (alloc_path == NULL) { + err = ERROR_OUTOFMEMORY; + goto done; + } + path = alloc_path; - alloc_path = (WCHAR*) uv__malloc(path_len * sizeof(WCHAR)); - if (alloc_path == NULL) { - err = ERROR_OUTOFMEMORY; - goto done; - } - path = alloc_path; - - r = GetEnvironmentVariableW(L"PATH", path, path_len); - if (r == 0 || r >= path_len) { - err = GetLastError(); - goto done; + r = GetEnvironmentVariableW(L"PATH", path, path_len); + if (r == 0 || r >= path_len) { + err = GetLastError(); + goto done; + } } } diff --git a/test/test-list.h b/test/test-list.h index 6e6e0824..36de006f 100644 --- a/test/test-list.h +++ b/test/test-list.h @@ -504,6 +504,7 @@ TEST_DECLARE (listen_with_simultaneous_accepts) TEST_DECLARE (listen_no_simultaneous_accepts) TEST_DECLARE (fs_stat_root) TEST_DECLARE (spawn_with_an_odd_path) +TEST_DECLARE (spawn_no_path) TEST_DECLARE (ipc_listen_after_bind_twice) TEST_DECLARE (win32_signum_number) #else @@ -1021,6 +1022,7 @@ TASK_LIST_START TEST_ENTRY (listen_no_simultaneous_accepts) TEST_ENTRY (fs_stat_root) TEST_ENTRY (spawn_with_an_odd_path) + TEST_ENTRY (spawn_no_path) TEST_ENTRY (ipc_listen_after_bind_twice) TEST_ENTRY (win32_signum_number) #else diff --git a/test/test-spawn.c b/test/test-spawn.c index 5e84dcdb..066d5d62 100644 --- a/test/test-spawn.c +++ b/test/test-spawn.c @@ -1366,6 +1366,34 @@ TEST_IMPL(spawn_with_an_odd_path) { MAKE_VALGRIND_HAPPY(uv_default_loop()); return 0; } + + +TEST_IMPL(spawn_no_path) { + char* env[1]; + WCHAR* old_path = NULL; + DWORD old_path_len; + + if ((old_path_len = GetEnvironmentVariableW(L"PATH", NULL, 0)) > 0) { + old_path = malloc(old_path_len * sizeof(WCHAR)); + GetEnvironmentVariableW(L"PATH", old_path, old_path_len); + SetEnvironmentVariableW(L"PATH", NULL); + } + + init_process_options("spawn_helper1", exit_cb); + options.env = env; + env[0] = NULL; + + ASSERT(0 == uv_spawn(uv_default_loop(), &process, &options)); + ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT)); + + ASSERT(exit_cb_called == 1); + ASSERT(close_cb_called == 1); + + SetEnvironmentVariableW(L"PATH", old_path); + + MAKE_VALGRIND_HAPPY(uv_default_loop()); + return 0; +} #endif #ifndef _WIN32