From ec77d99c406d79b05384989baeabec557717b26b Mon Sep 17 00:00:00 2001 From: Frank Lichtenheld Date: Tue, 21 Jan 2025 17:53:09 +0100 Subject: [PATCH] Replace usages of _snwprintf Avoid warning C4996: '_snwprintf': This function or variable may be unsafe Also makes the code generally simpler. Signed-off-by: Frank Lichtenheld --- access.c | 11 +++-------- openvpn.c | 16 ++++++++-------- plap/plap_common.c | 19 ++++++++++--------- proxy.c | 4 ++-- registry.c | 3 +-- 5 files changed, 24 insertions(+), 29 deletions(-) diff --git a/access.c b/access.c index 103425e..de3e84e 100644 --- a/access.c +++ b/access.c @@ -127,12 +127,8 @@ AddUserToGroup(const WCHAR *group) if (GetSystemDirectory(syspath, size)) { syspath[size - 1] = L'\0'; - size = _countof(cmd); - _snwprintf(cmd, size, L"%ls\\%ls", syspath, L"cmd.exe"); - cmd[size - 1] = L'\0'; - size = _countof(netcmd); - _snwprintf(netcmd, size, L"%ls\\%ls", syspath, L"net.exe"); - netcmd[size - 1] = L'\0'; + _snwprintf_s(cmd, _countof(cmd), _TRUNCATE, L"%ls\\%ls", syspath, L"cmd.exe"); + _snwprintf_s(netcmd, _countof(netcmd), _TRUNCATE, L"%ls\\%ls", syspath, L"net.exe"); } size = (wcslen(fmt) + wcslen(username) + 2 * wcslen(group) + 2 * wcslen(netcmd) + 1); if ((params = malloc(size * sizeof(WCHAR))) == NULL) @@ -140,8 +136,7 @@ AddUserToGroup(const WCHAR *group) return retval; } - _snwprintf(params, size, fmt, netcmd, group, netcmd, group, username); - params[size - 1] = L'\0'; + _snwprintf_s(params, size, _TRUNCATE, fmt, netcmd, group, netcmd, group, username); status = RunAsAdmin(cmd, params); if (status == 0) diff --git a/openvpn.c b/openvpn.c index cec8d3c..1e7a1dd 100644 --- a/openvpn.c +++ b/openvpn.c @@ -1970,8 +1970,8 @@ HandleServiceIO(DWORD err, DWORD bytes, LPOVERLAPPED lpo) } if (err) { - _snwprintf(s->readbuf, len, L"0x%08x\nInteractive Service disconnected\n", err); - s->readbuf[len - 1] = L'\0'; + _snwprintf_s( + s->readbuf, len, _TRUNCATE, L"0x%08x\nInteractive Service disconnected\n", err); SetEvent(s->hEvent); return; } @@ -2133,12 +2133,12 @@ OnProcess(connection_t *c, UNUSED char *msg) return; } - _snwprintf(tmp, - _countof(tmp), - L"OpenVPN terminated with exit code %lu. " - L"See the log file for details", - err); - tmp[_countof(tmp) - 1] = L'\0'; + _snwprintf_s(tmp, + _countof(tmp), + _TRUNCATE, + L"OpenVPN terminated with exit code %lu. " + L"See the log file for details", + err); WriteStatusLog(c, L"OpenVPN GUI> ", tmp, false); OnStop(c, NULL); diff --git a/plap/plap_common.c b/plap/plap_common.c index f42f06d..854ef16 100644 --- a/plap/plap_common.c +++ b/plap/plap_common.c @@ -70,15 +70,16 @@ x_dmsg(const char *file, const char *func, const wchar_t *fmt, ...) wchar_t date[30]; time_t log_time = time(NULL); struct tm *time_struct = localtime(&log_time); - _snwprintf(date, - _countof(date), - L"%d-%.2d-%.2d %.2d:%.2d:%.2d", - time_struct->tm_year + 1900, - time_struct->tm_mon + 1, - time_struct->tm_mday, - time_struct->tm_hour, - time_struct->tm_min, - time_struct->tm_sec); + _snwprintf_s(date, + _countof(date), + _TRUNCATE, + L"%d-%.2d-%.2d %.2d:%.2d:%.2d", + time_struct->tm_year + 1900, + time_struct->tm_mon + 1, + time_struct->tm_mday, + time_struct->tm_hour, + time_struct->tm_min, + time_struct->tm_sec); EnterCriticalSection(&log_write); diff --git a/proxy.c b/proxy.c index 255c5f6..0e56cb7 100644 --- a/proxy.c +++ b/proxy.c @@ -462,11 +462,11 @@ QueryWindowsProxySettings(const url_scheme scheme, LPCSTR host) NULL, WINHTTP_ACCESS_TYPE_NO_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0); if (session) { - int size = _snwprintf(NULL, 0, L"%ls://%hs", UrlSchemeStr(scheme), host) + 1; + int size = _scwprintf(L"%ls://%hs", UrlSchemeStr(scheme), host) + 1; LPWSTR url = malloc(size * sizeof(WCHAR)); if (url) { - _snwprintf(url, size, L"%ls://%hs", UrlSchemeStr(scheme), host); + _snwprintf_s(url, size, _TRUNCATE, L"%ls://%hs", UrlSchemeStr(scheme), host); LPWSTR old_proxy = proxy; WINHTTP_PROXY_INFO proxy_info; diff --git a/registry.c b/registry.c index eccd2c2..d1238ca 100644 --- a/registry.c +++ b/registry.c @@ -556,8 +556,7 @@ OpenConfigRegistryKey(const WCHAR *config_name, HKEY *regkey, BOOL create) return 0; } - _snwprintf(name, count, fmt, config_name); - name[count - 1] = L'\0'; + _snwprintf_s(name, count, _TRUNCATE, fmt, config_name); if (!create) {