diff --git a/contrib/win32/win32compat/console.c b/contrib/win32/win32compat/console.c index bb574ae54..49d295776 100644 --- a/contrib/win32/win32compat/console.c +++ b/contrib/win32/win32compat/console.c @@ -514,6 +514,9 @@ ConWriteString(char* pszString, int cbString) int cnt = 0; wchar_t* utf16 = NULL; + if (pszString == NULL) + return 0; + if ((needed = MultiByteToWideChar(CP_UTF8, 0, pszString, cbString, NULL, 0)) == 0 || (utf16 = malloc(needed * sizeof(wchar_t))) == NULL || (cnt = MultiByteToWideChar(CP_UTF8, 0, pszString, cbString, utf16, needed)) == 0) { @@ -536,6 +539,9 @@ ConTranslateAndWriteString(char* pszString, int cbString) { DWORD Result = 0; + if (pszString == NULL) + return 0; + if (hOutputConsole) WriteConsole(hOutputConsole, pszString, cbString, &Result, 0); else diff --git a/contrib/win32/win32compat/fileio.c b/contrib/win32/win32compat/fileio.c index 8dc238167..71e0efb77 100644 --- a/contrib/win32/win32compat/fileio.c +++ b/contrib/win32/win32compat/fileio.c @@ -347,7 +347,11 @@ createFile_flags_setup(int flags, mode_t mode, struct createFile_flags* cf_flags // If the mode is USHRT_MAX then we will inherit the permissions from the parent folder. if (mode != USHRT_MAX) { /*validate mode*/ - if (mode & ~(S_IRWXU | S_IRWXG | S_IRWXO)) { + /* + * __S_IFDIR __S_IFREG are added for compat + * TODO- open(__S_IFDIR) on a file and vice versa should fail + */ + if (mode & ~(S_IRWXU | S_IRWXG | S_IRWXO | __S_IFDIR | __S_IFREG)) { debug3("open - ERROR: unsupported mode: %d", mode); errno = ENOTSUP; return -1; diff --git a/contrib/win32/win32compat/inc/fcntl.h b/contrib/win32/win32compat/inc/fcntl.h index d22f9c782..6b3730c51 100644 --- a/contrib/win32/win32compat/inc/fcntl.h +++ b/contrib/win32/win32compat/inc/fcntl.h @@ -58,4 +58,11 @@ int w32_allocate_fd_for_handle(HANDLE, BOOL); # define S_IROTH 0000004 /* read permission, other */ # define S_IRWXU 0000700 /* read, write, execute */ # define S_IRWXG 0000070 /* read, write, execute */ -# define S_IRWXO 0000007 /* read, write, execute */ \ No newline at end of file +# define S_IRWXO 0000007 /* read, write, execute */ + +/* + * File types. Note that the values are different from similar variants + * defined in stat.h. These are based on similar definition values on Linux + */ +#define __S_IFDIR 0040000 /* Directory. */ +#define __S_IFREG 0100000 /* Regular file. */ \ No newline at end of file diff --git a/contrib/win32/win32compat/pwd.c b/contrib/win32/win32compat/pwd.c index 1647e5a62..93af2223e 100644 --- a/contrib/win32/win32compat/pwd.c +++ b/contrib/win32/win32compat/pwd.c @@ -106,7 +106,7 @@ get_passwd(const char *user_utf8, LPWSTR user_sid) char *uname_utf8 = NULL, *uname_upn = NULL, *udom_utf8 = NULL, *pw_home_utf8 = NULL, *user_sid_utf8 = NULL; LPBYTE user_info = NULL; LPWSTR user_sid_local = NULL; - wchar_t reg_path[PATH_MAX], profile_home[PATH_MAX]; + wchar_t reg_path[PATH_MAX], profile_home[PATH_MAX], profile_home_exp[PATH_MAX]; HKEY reg_key = 0; int tmp_len = PATH_MAX; PDOMAIN_CONTROLLER_INFOW pdc = NULL; @@ -165,8 +165,10 @@ get_passwd(const char *user_utf8, LPWSTR user_sid) /* if one of below fails, set profile path to Windows directory */ if (swprintf_s(reg_path, PATH_MAX, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList\\%ls", user_sid) == -1 || RegOpenKeyExW(HKEY_LOCAL_MACHINE, reg_path, 0, STANDARD_RIGHTS_READ | KEY_QUERY_VALUE | KEY_WOW64_64KEY, ®_key) != 0 || - RegQueryValueExW(reg_key, L"ProfileImagePath", 0, NULL, (LPBYTE)profile_home, &tmp_len) != 0) - if (GetWindowsDirectoryW(profile_home, PATH_MAX) == 0) { + RegQueryValueExW(reg_key, L"ProfileImagePath", 0, NULL, (LPBYTE)profile_home, &tmp_len) != 0 || + ExpandEnvironmentStringsW(profile_home, NULL, 0) > PATH_MAX || + ExpandEnvironmentStringsW(profile_home, profile_home_exp, PATH_MAX) == 0) + if (GetWindowsDirectoryW(profile_home_exp, PATH_MAX) == 0) { debug3("GetWindowsDirectoryW failed with %d", GetLastError()); errno = EOTHER; goto done; @@ -174,7 +176,7 @@ get_passwd(const char *user_utf8, LPWSTR user_sid) if ((uname_utf8 = utf16_to_utf8(uname_utf16)) == NULL || (udom_utf16 && (udom_utf8 = utf16_to_utf8(udom_utf16)) == NULL) || - (pw_home_utf8 = utf16_to_utf8(profile_home)) == NULL || + (pw_home_utf8 = utf16_to_utf8(profile_home_exp)) == NULL || (user_sid_utf8 = utf16_to_utf8(user_sid)) == NULL) { errno = ENOMEM; goto done;