Skip to content

mingw: handle non-ASCII PATH components correctly #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions compat/mingw.c
Original file line number Diff line number Diff line change
Expand Up @@ -1161,14 +1161,21 @@ static char *lookup_prog(const char *dir, int dirlen, const char *cmd,
int isexe, int exe_only)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the Git mailing list, Junio C Hamano wrote (reply to this):

"Adam Roben via GitGitGadget" <[email protected]> writes:

> Note that the only problem in this function was calling
> `GetFileAttributes()` instead of `GetFileAttributesW()`. The calls to
> `access()` were fine because `access()` is a macro which resolves to
> `mingw_access()`, which already handles Unicode correctly. But
> `lookup_prog()` was changed to use `_waccess()` directly so that we only
> convert the path to UTF-16 once.

Nicely explained.  Thanks.

>
> To make things work correctly, we have to maintain UTF-8 and UTF-16
> versions in tandem in `lookup_prog()`.
>
> Signed-off-by: Adam Roben <[email protected]>
> Signed-off-by: Johannes Schindelin <[email protected]>
> ---
>  compat/mingw.c | 15 +++++++++++----
>  1 file changed, 11 insertions(+), 4 deletions(-)
>
> diff --git a/compat/mingw.c b/compat/mingw.c
> index 8141f77189..9f02403ebf 100644
> --- a/compat/mingw.c
> +++ b/compat/mingw.c
> @@ -1161,14 +1161,21 @@ static char *lookup_prog(const char *dir, int dirlen, const char *cmd,
>  			 int isexe, int exe_only)
>  {
>  	char path[MAX_PATH];
> +	wchar_t wpath[MAX_PATH];
>  	snprintf(path, sizeof(path), "%.*s\\%s.exe", dirlen, dir, cmd);
>  
> -	if (!isexe && access(path, F_OK) == 0)
> +	if (xutftowcs_path(wpath, path) < 0)
> +		return NULL;
> +
> +	if (!isexe && _waccess(wpath, F_OK) == 0)
>  		return xstrdup(path);
> -	path[strlen(path)-4] = '\0';
> -	if ((!exe_only || isexe) && access(path, F_OK) == 0)
> -		if (!(GetFileAttributes(path) & FILE_ATTRIBUTE_DIRECTORY))
> +	wpath[wcslen(wpath)-4] = '\0';
> +	if ((!exe_only || isexe) && _waccess(wpath, F_OK) == 0) {
> +		if (!(GetFileAttributesW(wpath) & FILE_ATTRIBUTE_DIRECTORY)) {
> +			path[strlen(path)-4] = '\0';
>  			return xstrdup(path);
> +		}
> +	}
>  	return NULL;
>  }

{
char path[MAX_PATH];
wchar_t wpath[MAX_PATH];
snprintf(path, sizeof(path), "%.*s\\%s.exe", dirlen, dir, cmd);

if (!isexe && access(path, F_OK) == 0)
if (xutftowcs_path(wpath, path) < 0)
return NULL;

if (!isexe && _waccess(wpath, F_OK) == 0)
return xstrdup(path);
path[strlen(path)-4] = '\0';
if ((!exe_only || isexe) && access(path, F_OK) == 0)
if (!(GetFileAttributes(path) & FILE_ATTRIBUTE_DIRECTORY))
wpath[wcslen(wpath)-4] = '\0';
if ((!exe_only || isexe) && _waccess(wpath, F_OK) == 0) {
if (!(GetFileAttributesW(wpath) & FILE_ATTRIBUTE_DIRECTORY)) {
path[strlen(path)-4] = '\0';
return xstrdup(path);
}
}
return NULL;
}

Expand Down