From 8346fc0d439d7866bc443865b4241185b9b02058 Mon Sep 17 00:00:00 2001 From: Bryan Berns Date: Tue, 16 Jul 2019 14:42:02 -0400 Subject: [PATCH] Correct Unicode Path Handling (#388) Adjusted realpath() to use _wfullpath() to handle cases where paths may contain unicode characters. Addresses PowerShell/Win32-OpenSSH#1401. --- contrib/win32/win32compat/misc.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/contrib/win32/win32compat/misc.c b/contrib/win32/win32compat/misc.c index fddd3e532..ed94120ae 100644 --- a/contrib/win32/win32compat/misc.c +++ b/contrib/win32/win32compat/misc.c @@ -949,8 +949,10 @@ convertToForwardslash(char *str) * path to produce a canonicalized absolute pathname. */ char * -realpath(const char *inputpath, char resolved[PATH_MAX]) +realpath(const char *inputpath, char * resolved) { + wchar_t* temppath_utf16 = NULL; + wchar_t* resolved_utf16 = NULL; char path[PATH_MAX] = { 0, }, tempPath[PATH_MAX] = { 0, }, *ret = NULL; int is_win_path = 1; @@ -1036,7 +1038,10 @@ realpath(const char *inputpath, char resolved[PATH_MAX]) resolved[3] = '\0'; } - if (_fullpath(tempPath, resolved, PATH_MAX) == NULL) { + /* note: _wfullpath() is required to resolve paths containing unicode characters */ + if ((resolved_utf16 = utf8_to_utf16(resolved)) == NULL || + (temppath_utf16 = _wfullpath(NULL, resolved_utf16, 0)) == NULL || + WideCharToMultiByte(CP_UTF8, 0, temppath_utf16, -1, tempPath, sizeof(tempPath), NULL, NULL) == 0) { errno = EINVAL; goto done; } @@ -1080,6 +1085,10 @@ realpath(const char *inputpath, char resolved[PATH_MAX]) } done: + if (resolved_utf16 != NULL) + free(resolved_utf16); + if (temppath_utf16 != NULL) + free(temppath_utf16); return ret; }