threads / patch / 57270

patchgetcwd(mingw): handle the case when there is no cwd

Subject: [PATCH] getcwd(mingw): handle the case when there is no cwd

## tl;dr

3 messages between Jan 19, 2022 and Jan 19, 2022. Diffs are folded; open one to read it.

replies: 2people: 2as markdown or json

Johannes Schindelin via GitGitGadget· Jan 19, 2022, 18:56 UTC · lore
From: Johannes Schindelin <johannes.schindelin@gmx.de>

A recent upstream topic introduced checks for certain Git commands that prevent them from deleting the current working directory, introducing also a regression test that ensures that commands such as `git version` _can_ run without a current working directory.

While technically not possible on Windows via the regular Win32 API, we do run the regression tests in an MSYS2 Bash which uses a POSIX emulation layer (the MSYS2/Cygwin runtime) where a really evil hack _does_ allow to delete a directory even if it is the current working directory.

Therefore, Git needs to be prepared for a missing working directory, even on Windows.

This issue was not noticed in upstream Git because there was no caller that tried to discover a Git directory with a deleted current working directory in the test suite. But in the microsoft/git fork, we do want to run `pre-command`/`post-command` hooks for every command, even for `git version`, which means that we make precisely such a call. The bug is not in that `pre-command`/`post-command` feature, though, but in `mingw_getcwd()` and needs to be addressed there.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
    getcwd(mingw): handle the case when there is no current working
    directory
    
    The bug fixed by this topic was noticed due to test failures while
    rebasing Microsoft's fork of Git onto v2.35.0-rc1. It may not be
    desirable to take it into Git v2.35.0 this late in the -rc phase, even
    though I do plan on integrating it into Git for Windows v2.35.0
    [https://github.com/git-for-windows/git/pull/3641].
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1120%2Fdscho%2Fmingw-getcwd-without-cwd-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1120/dscho/mingw-getcwd-without-cwd-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/1120
 compat/mingw.c | 4 ++++
 1 file changed, 4 insertions(+)
Show changes to compat/mingw.c +4 −0
diff --git a/compat/mingw.c b/compat/mingw.c
index 640dcb11de0..03af369b2b9 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -1127,6 +1127,10 @@ char *mingw_getcwd(char *pointer, int len)
 	}
 	if (!ret || ret >= ARRAY_SIZE(wpointer))
 		return NULL;
+	if (GetFileAttributesW(wpointer) == INVALID_FILE_ATTRIBUTES) {
+		errno = ENOENT;
+		return NULL;
+	}
 	if (xwcstoutf(pointer, wpointer, len) < 0)
 		return NULL;
 	convert_slashes(pointer);

base-commit: af4e5f569bc89f356eb34a9373d7f82aca6faa8a
-- 
gitgitgadget
Junio C Hamano· Jan 19, 2022, 19:27 UTC · re: Johannes Schindelin via GitGitGadget · lore

Re: [PATCH] getcwd(mingw): handle the case when there is no cwd

"Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com> writes:

Show 5 quoted lines
> While technically not possible on Windows via the regular Win32 API, we
> do run the regression tests in an MSYS2 Bash which uses a POSIX
> emulation layer (the MSYS2/Cygwin runtime) where a really evil hack
> _does_ allow to delete a directory even if it is the current working
> directory.
Wow.  I am not sure if I should frown or smile ;-)
>     The bug fixed by this topic was noticed due to test failures while
>     rebasing Microsoft's fork of Git onto v2.35.0-rc1. It may not be
>     desirable to take it into Git v2.35.0 this late in the -rc phase, even
>     though I do plan on integrating it into Git for Windows v2.35.0

I think we can and should take it in my tree (even direct to 'master'), as it is very clear it affects nobody other than Windows, and even if this has any unintended negative effect, having that common with "Git for Windows" would only help.

Thanks.
Johannes Schindelin· Jan 19, 2022, 22:39 UTC · re: Junio C Hamano · lore

Re: [PATCH] getcwd(mingw): handle the case when there is no cwd

Hi Junio,
On Wed, 19 Jan 2022, Junio C Hamano wrote:
Show 12 quoted lines
> "Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com>
> writes:
>
> >     The bug fixed by this topic was noticed due to test failures while
> >     rebasing Microsoft's fork of Git onto v2.35.0-rc1. It may not be
> >     desirable to take it into Git v2.35.0 this late in the -rc phase, even
> >     though I do plan on integrating it into Git for Windows v2.35.0
>
> I think we can and should take it in my tree (even direct to
> 'master'), as it is very clear it affects nobody other than Windows,
> and even if this has any unintended negative effect, having that
> common with "Git for Windows" would only help.

Thank you! Dscho

← back to recent threads