From 8c86f30a0ff5ad4d192bfdd30013948126ba8bf5 Mon Sep 17 00:00:00 2001 From: Bryan Berns Date: Tue, 24 Apr 2018 04:47:09 -0400 Subject: [PATCH] Reworked Path Resolution Function - Review Changes - Changes based on review comments. --- contrib/win32/win32compat/fileio.c | 2 +- contrib/win32/win32compat/misc.c | 14 ++++++++------ contrib/win32/win32compat/w32log.c | 8 ++++---- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/contrib/win32/win32compat/fileio.c b/contrib/win32/win32compat/fileio.c index 5e49bda5a..7a5121b8d 100644 --- a/contrib/win32/win32compat/fileio.c +++ b/contrib/win32/win32compat/fileio.c @@ -439,7 +439,7 @@ fileio_open(const char *path_utf8, int flags, mode_t mode) } /* if opening null device, point to Windows equivalent */ - if (strcmp(path_utf8, NULL_DEVICE) == 0) + if (strncmp(path_utf8, NULL_DEVICE, sizeof(NULL_DEVICE)) == 0) path_utf8 = NULL_DEVICE_WIN; if ((path_utf16 = resolved_path_utf16(path_utf8)) == NULL) { diff --git a/contrib/win32/win32compat/misc.c b/contrib/win32/win32compat/misc.c index 7878b9604..ea916f0a1 100644 --- a/contrib/win32/win32compat/misc.c +++ b/contrib/win32/win32compat/misc.c @@ -53,6 +53,7 @@ #include "w32fd.h" #include "inc\string.h" #include "inc\grp.h" +#include static char* s_programdir = NULL; @@ -248,7 +249,7 @@ w32_fopen_utf8(const char *input_path, const char *mode) } /* if opening null device, point to Windows equivalent */ - if (strcmp(input_path, NULL_DEVICE) == 0) + if (strncmp(input_path, NULL_DEVICE, sizeof(NULL_DEVICE)) == 0) input_path = NULL_DEVICE_WIN; wpath = resolved_path_utf16(input_path); @@ -260,7 +261,7 @@ w32_fopen_utf8(const char *input_path, const char *mode) } if ((_wfopen_s(&f, wpath, wmode) != 0) || (f == NULL)) { - debug3("Failed to open file:%S error:%d", wpath, errno); + debug3("Failed to open file:%s error:%d", input_path, errno); goto cleanup; } @@ -931,6 +932,7 @@ resolved_path_utf16(const char *input_path) wchar_t * resolved_path_new = realloc(resolved_path, (resolved_len + changed_req + 1) * sizeof(wchar_t)); if (resolved_path == NULL) { + debug3("%s: memory allocation failed.", __FUNCTION__); free(resolved_path); return NULL; } @@ -938,16 +940,16 @@ resolved_path_utf16(const char *input_path) } /* shift memory contents over based on side of the new string */ - memmove(resolved_path + variable_len + changed_req, resolved_path + variable_len, - (resolved_len - variable_len + 1) * sizeof(wchar_t)); - memcpy(resolved_path, program_data, programdata_len * sizeof(wchar_t)); + wmemmove_s(&resolved_path[variable_len + changed_req], resolved_len - variable_len + 1, + &resolved_path[variable_len], resolved_len - variable_len + 1); resolved_len += changed_req; + wmemcpy_s(resolved_path, resolved_len + 1, program_data, programdata_len); } if (resolved_path[0] == L'/' && iswalpha(resolved_path[1]) && resolved_path[2] == L':') { /* shift memory to remove forward slash including null terminator */ - memmove(resolved_path, resolved_path + 1, (resolved_len + 1 - 1) * sizeof(wchar_t)); + wmemmove_s(resolved_path, resolved_len + 1, resolved_path + 1, (resolved_len + 1 - 1)); /* if just a drive letter path, make x: into x:\ */ if (resolved_path[2] == L'\0') { diff --git a/contrib/win32/win32compat/w32log.c b/contrib/win32/win32compat/w32log.c index a0cb6f271..a45aad5f9 100644 --- a/contrib/win32/win32compat/w32log.c +++ b/contrib/win32/win32compat/w32log.c @@ -110,11 +110,11 @@ openlog_file() while (tail > module_path && *tail != L'\\' && *tail != L'/') tail--; - wchar_t ssh_root_path[PATH_MAX] = {0 ,}; - wcscat_s(ssh_root_path, _countof(ssh_root_path), get_program_data_path()); /* "%programData%" */ - wcscat_s(ssh_root_path, _countof(ssh_root_path), L"\\ssh"); /* "%programData%\\ssh" */ + wchar_t ssh_cfg_path[PATH_MAX] = {0 ,}; + wcscat_s(ssh_cfg_path, _countof(ssh_cfg_path), get_program_data_path()); /* "%programData%" */ + wcscat_s(ssh_cfg_path, _countof(ssh_cfg_path), L"\\ssh"); /* "%programData%\\ssh" */ - if ((wcsncat_s(log_file, PATH_MAX + 12, ssh_root_path, wcslen(ssh_root_path)) != 0) || + if ((wcsncat_s(log_file, PATH_MAX + 12, ssh_cfg_path, wcslen(ssh_cfg_path)) != 0) || (wcsncat_s(log_file, PATH_MAX + 12, logs_dir, 6) != 0) || (wcsncat_s(log_file, PATH_MAX + 12, tail + 1, wcslen(tail + 1) - 3) != 0 ) || (wcsncat_s(log_file, PATH_MAX + 12, L"log", 3) != 0))