From 07096fc1c2953d9dc4a3bb33904c6594e372f688 Mon Sep 17 00:00:00 2001 From: Peter Kowalczyk Date: Mon, 3 Aug 2026 22:16:16 -0500 Subject: [PATCH] run-command: avoid NULL dereference for missing Windows shell When Git's native Windows environment omits its usr/bin directory from PATH, locate_in_PATH("sh") returns NULL. git_shell_path() passes that value to convert_slashes(), causing git-remote-https.exe to terminate with an access violation while preparing a credential helper. Report the missing shell as a fatal Git error instead. This avoids the NULL-pointer dereference and adds a Windows-only regression test that clears PATH inside test-tool and verifies the diagnostic. Signed-off-by: Peter Kowalczyk --- run-command.c | 2 ++ t/helper/test-run-command.c | 12 ++++++++++++ t/t0061-run-command.sh | 5 +++++ 3 files changed, 19 insertions(+) diff --git a/run-command.c b/run-command.c index e70a8a387b9042..8b319630953a7d 100644 --- a/run-command.c +++ b/run-command.c @@ -280,6 +280,8 @@ char *git_shell_path(void) return xstrdup(SHELL_PATH); #else char *p = locate_in_PATH("sh"); + if (!p) + die(_("cannot find 'sh' in PATH")); convert_slashes(p); return p; #endif diff --git a/t/helper/test-run-command.c b/t/helper/test-run-command.c index 4a56456894ccff..11da5f75b291ed 100644 --- a/t/helper/test-run-command.c +++ b/t/helper/test-run-command.c @@ -103,6 +103,16 @@ static int test_stdin_pipe_feed(int hook_stdin_fd, void *cb UNUSED, void *task_c return !(*lines_remaining); } +static int shell_path_without_path(void) +{ + char *path; + + unsetenv("PATH"); + path = git_shell_path(); + free(path); + return 0; +} + struct testsuite { struct string_list tests, failed; int next; @@ -450,6 +460,8 @@ int cmd__run_command(int argc, const char **argv) if (argc > 1 && !strcmp(argv[1], "testsuite")) return testsuite(argc - 1, argv + 1); + if (argc > 1 && !strcmp(argv[1], "shell-path-without-path")) + return shell_path_without_path(); if (!strcmp(argv[1], "inherited-handle")) return inherit_handle(argv[0]); if (!strcmp(argv[1], "inherited-handle-child")) diff --git a/t/t0061-run-command.sh b/t/t0061-run-command.sh index 905e90e1f72541..b74a767bfea64a 100755 --- a/t/t0061-run-command.sh +++ b/t/t0061-run-command.sh @@ -16,6 +16,11 @@ test_expect_success MINGW 'subprocess inherits only std handles' ' test-tool run-command inherited-handle ' +test_expect_success MINGW 'missing shell path is reported' ' + test_must_fail test-tool run-command shell-path-without-path 2>err && + test_grep "cannot find .sh. in PATH" err +' + test_expect_success 'start_command reports ENOENT (slash)' ' test-tool run-command start-command-ENOENT ./does-not-exist 2>err && test_grep "\./does-not-exist" err