Repository navigation
run-command: avoid NULL dereference for missing Windows shell - #6358
Conversation
Running Git for Windows in an environment where Therefore, this PR looks like a fix for something that is not considered a bug, but an incorrect usage of |
While that is true, I still think clearly telling the user that they messed up their setup is nicer than crashing with a |
dscho
left a comment
There was a problem hiding this comment.
While this is definitely outside of the supported use cases of Git for Windows, I am sympathetic to the desire. Maybe these two concerns could be addressed?
|
#6475 seems to be helped by this, so I am a lot more willing to merge this (after addressing the libification concern). |
On native Windows, a failed `sh` lookup can crash Git instead of
reporting that the shell could not be started. `locate_in_PATH("sh")`
can return NULL, which `convert_slashes()` then dereferences.
The crash was reported in `git-remote-https` and when launching the
rebase editor. Why the lookup failed in the latter case is unknown. This
can usually not happen because Git for Windows sets the `PATH` in such a
way that `sh.exe` is always included. But apparently, under certain
circumstances, people run Git for Windows in funny ways where the shell
is not on the path. This was reported in
git-for-windows#6475.
A simple reproducer in a Git Bash, which crashes with a `Segmentation
fault` before this patch, is:
PATH=$MINGW_PREFIX/bin:$WINDIR/system32 git.exe -c alias.p='!pwd' p
Treat a missing shell like any other `ENOENT` failure in
`start_command()`, so normal cleanup runs and `silent_exec_failure`
still controls whether a diagnostic is shown. That way, the command
above still fails, but this time with a proper error message (actually
two, for the price of one):
error: cannot spawn pwd: No such file or directory
fatal: while expanding alias 'p': 'pwd': No such file or directory
Assisted-by: GPT-6.2 Sol
Signed-off-by: Peter Kowalczyk <peter@slix.io>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
07096fc to
5f627c0
Compare
In some sandboxes (as AI agents increasingly use), Git for Windows' `sh.exe` is hidden, inadvertently [causing `git.exe` to crash](git-for-windows/git#6475). This bug has been fixed by [showing a proper error message instead](git-for-windows/git#6358). Signed-off-by: gitforwindowshelper[bot] <gitforwindowshelper-bot@users.noreply.github.com>
## Summary As reported in #6475, on native Windows, `git_shell_path()` assumes that `locate_in_PATH("sh")` always succeeds and passes its result directly to `convert_slashes()`. If `sh` is absent from `PATH`, the lookup returns `NULL` and Git terminates with an access violation. Check the lookup result and report a fatal error instead: ``` fatal: cannot find 'sh' in PATH ``` This prevents the NULL dereference. It does not add a shell fallback, so commands requiring a shell still fail when no shell is available. ## Crash analysis The failure was reproduced in `git-remote-https.exe` from Git for Windows 2.55.0.windows.3. ProcDump captured the unhandled exception: ```text Process: git-remote-https.exe (97196) Process image: C:\Program Files\Git\mingw64\libexec\git-core\git-remote-https.exe Exception monitor: Unhandled [21:32:43]Exception: C0000005.ACCESS_VIOLATION [21:32:43]Unhandled: C0000005.ACCESS_VIOLATION [21:32:43]Dump 1 initiated: D:\Libraries\Downloads\git-remote-https-crash.dmp [21:32:44]Dump 1 writing: Estimated dump file size is 47 MB. [21:32:44]Dump 1 complete: 47 MB written in 0.6 seconds ``` The dump confirms that the faulting instruction attempted to read through `RAX` while `RAX` was zero: ```text C0000005.ACCESS_VIOLATION RAX=0000000000000000 movzx edx, byte ptr [rax] ``` Correlation of the surrounding disassembly with the source shows it loading `"sh"`, performing the PATH lookup, and then entering the slash conversion. This matches the native Windows implementation: ```c char *p = locate_in_PATH("sh"); convert_slashes(p); ``` The reproduced missing-`sh` condition therefore leaves `p` as `NULL`, which is dereferenced by `convert_slashes()`. ## Regression test Add a MINGW-only regression test that clears `PATH` inside `test-tool run-command`, calls `git_shell_path()`, and verifies that Git reports the controlled fatal diagnostic instead of crashing. The helper clears `PATH` after process startup, making the missing-shell condition deterministic without depending on Windows startup PATH reconstruction. ## Testing Passed: - `git diff --check` - Shell syntax check for `t/t0061-run-command.sh` - Added-line formatting and commit-message checks The native Windows test suite was not run locally because a Git for Windows SDK/compiler was unavailable in the test environment. Windows CI is expected to build the change and run `t0061-run-command.sh`. <img width="500" height="230" alt="codex-clipboard-d4f6fbaa-5f73-4e36-8923-acc0596047db" src="https://github.com/user-attachments/assets/ffe18c92-32c0-4dd7-96fc-d7bc59d2a3b4" />
## Summary As reported in #6475, on native Windows, `git_shell_path()` assumes that `locate_in_PATH("sh")` always succeeds and passes its result directly to `convert_slashes()`. If `sh` is absent from `PATH`, the lookup returns `NULL` and Git terminates with an access violation. Check the lookup result and report a fatal error instead: ``` fatal: cannot find 'sh' in PATH ``` This prevents the NULL dereference. It does not add a shell fallback, so commands requiring a shell still fail when no shell is available. ## Crash analysis The failure was reproduced in `git-remote-https.exe` from Git for Windows 2.55.0.windows.3. ProcDump captured the unhandled exception: ```text Process: git-remote-https.exe (97196) Process image: C:\Program Files\Git\mingw64\libexec\git-core\git-remote-https.exe Exception monitor: Unhandled [21:32:43]Exception: C0000005.ACCESS_VIOLATION [21:32:43]Unhandled: C0000005.ACCESS_VIOLATION [21:32:43]Dump 1 initiated: D:\Libraries\Downloads\git-remote-https-crash.dmp [21:32:44]Dump 1 writing: Estimated dump file size is 47 MB. [21:32:44]Dump 1 complete: 47 MB written in 0.6 seconds ``` The dump confirms that the faulting instruction attempted to read through `RAX` while `RAX` was zero: ```text C0000005.ACCESS_VIOLATION RAX=0000000000000000 movzx edx, byte ptr [rax] ``` Correlation of the surrounding disassembly with the source shows it loading `"sh"`, performing the PATH lookup, and then entering the slash conversion. This matches the native Windows implementation: ```c char *p = locate_in_PATH("sh"); convert_slashes(p); ``` The reproduced missing-`sh` condition therefore leaves `p` as `NULL`, which is dereferenced by `convert_slashes()`. ## Regression test Add a MINGW-only regression test that clears `PATH` inside `test-tool run-command`, calls `git_shell_path()`, and verifies that Git reports the controlled fatal diagnostic instead of crashing. The helper clears `PATH` after process startup, making the missing-shell condition deterministic without depending on Windows startup PATH reconstruction. ## Testing Passed: - `git diff --check` - Shell syntax check for `t/t0061-run-command.sh` - Added-line formatting and commit-message checks The native Windows test suite was not run locally because a Git for Windows SDK/compiler was unavailable in the test environment. Windows CI is expected to build the change and run `t0061-run-command.sh`. <img width="500" height="230" alt="codex-clipboard-d4f6fbaa-5f73-4e36-8923-acc0596047db" src="https://github.com/user-attachments/assets/ffe18c92-32c0-4dd7-96fc-d7bc59d2a3b4" />
Summary
On native Windows,
git_shell_path()assumes thatlocate_in_PATH("sh")always succeeds and passes its result directly to
convert_slashes(). Ifshis absent fromPATH, the lookup returnsNULLand Git terminateswith an access violation.
Check the lookup result and report a fatal error instead:
This prevents the NULL dereference. It does not add a shell fallback, so
commands requiring a shell still fail when no shell is available.
Crash analysis
The failure was reproduced in
git-remote-https.exefrom Git for Windows2.55.0.windows.3. ProcDump captured the unhandled exception:
The dump confirms that the faulting instruction attempted to read through
RAXwhileRAXwas zero:Correlation of the surrounding disassembly with the source shows it
loading
"sh", performing the PATH lookup, and then entering the slashconversion. This matches the native Windows implementation:
The reproduced missing-
shcondition therefore leavespasNULL,which is dereferenced by
convert_slashes().Regression test
Add a MINGW-only regression test that clears
PATHinsidetest-tool run-command, callsgit_shell_path(), and verifies that Gitreports the controlled fatal diagnostic instead of crashing.
The helper clears
PATHafter process startup, making the missing-shellcondition deterministic without depending on Windows startup PATH
reconstruction.
Testing
Passed:
git diff --checkt/t0061-run-command.shThe native Windows test suite was not run locally because a Git for

Windows SDK/compiler was unavailable in the test environment. Windows CI
is expected to build the change and run
t0061-run-command.sh.