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 <frank@lichtenheld.com>
master
Frank Lichtenheld 2025-01-21 17:53:09 +01:00 committed by Gert Doering
parent 913641ec41
commit ec77d99c40
5 changed files with 24 additions and 29 deletions

View File

@ -127,12 +127,8 @@ AddUserToGroup(const WCHAR *group)
if (GetSystemDirectory(syspath, size)) if (GetSystemDirectory(syspath, size))
{ {
syspath[size - 1] = L'\0'; syspath[size - 1] = L'\0';
size = _countof(cmd); _snwprintf_s(cmd, _countof(cmd), _TRUNCATE, L"%ls\\%ls", syspath, L"cmd.exe");
_snwprintf(cmd, size, L"%ls\\%ls", syspath, L"cmd.exe"); _snwprintf_s(netcmd, _countof(netcmd), _TRUNCATE, L"%ls\\%ls", syspath, L"net.exe");
cmd[size - 1] = L'\0';
size = _countof(netcmd);
_snwprintf(netcmd, size, L"%ls\\%ls", syspath, L"net.exe");
netcmd[size - 1] = L'\0';
} }
size = (wcslen(fmt) + wcslen(username) + 2 * wcslen(group) + 2 * wcslen(netcmd) + 1); size = (wcslen(fmt) + wcslen(username) + 2 * wcslen(group) + 2 * wcslen(netcmd) + 1);
if ((params = malloc(size * sizeof(WCHAR))) == NULL) if ((params = malloc(size * sizeof(WCHAR))) == NULL)
@ -140,8 +136,7 @@ AddUserToGroup(const WCHAR *group)
return retval; return retval;
} }
_snwprintf(params, size, fmt, netcmd, group, netcmd, group, username); _snwprintf_s(params, size, _TRUNCATE, fmt, netcmd, group, netcmd, group, username);
params[size - 1] = L'\0';
status = RunAsAdmin(cmd, params); status = RunAsAdmin(cmd, params);
if (status == 0) if (status == 0)

View File

@ -1970,8 +1970,8 @@ HandleServiceIO(DWORD err, DWORD bytes, LPOVERLAPPED lpo)
} }
if (err) if (err)
{ {
_snwprintf(s->readbuf, len, L"0x%08x\nInteractive Service disconnected\n", err); _snwprintf_s(
s->readbuf[len - 1] = L'\0'; s->readbuf, len, _TRUNCATE, L"0x%08x\nInteractive Service disconnected\n", err);
SetEvent(s->hEvent); SetEvent(s->hEvent);
return; return;
} }
@ -2133,12 +2133,12 @@ OnProcess(connection_t *c, UNUSED char *msg)
return; return;
} }
_snwprintf(tmp, _snwprintf_s(tmp,
_countof(tmp), _countof(tmp),
L"OpenVPN terminated with exit code %lu. " _TRUNCATE,
L"See the log file for details", L"OpenVPN terminated with exit code %lu. "
err); L"See the log file for details",
tmp[_countof(tmp) - 1] = L'\0'; err);
WriteStatusLog(c, L"OpenVPN GUI> ", tmp, false); WriteStatusLog(c, L"OpenVPN GUI> ", tmp, false);
OnStop(c, NULL); OnStop(c, NULL);

View File

@ -70,15 +70,16 @@ x_dmsg(const char *file, const char *func, const wchar_t *fmt, ...)
wchar_t date[30]; wchar_t date[30];
time_t log_time = time(NULL); time_t log_time = time(NULL);
struct tm *time_struct = localtime(&log_time); struct tm *time_struct = localtime(&log_time);
_snwprintf(date, _snwprintf_s(date,
_countof(date), _countof(date),
L"%d-%.2d-%.2d %.2d:%.2d:%.2d", _TRUNCATE,
time_struct->tm_year + 1900, L"%d-%.2d-%.2d %.2d:%.2d:%.2d",
time_struct->tm_mon + 1, time_struct->tm_year + 1900,
time_struct->tm_mday, time_struct->tm_mon + 1,
time_struct->tm_hour, time_struct->tm_mday,
time_struct->tm_min, time_struct->tm_hour,
time_struct->tm_sec); time_struct->tm_min,
time_struct->tm_sec);
EnterCriticalSection(&log_write); EnterCriticalSection(&log_write);

View File

@ -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); NULL, WINHTTP_ACCESS_TYPE_NO_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
if (session) 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)); LPWSTR url = malloc(size * sizeof(WCHAR));
if (url) 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; LPWSTR old_proxy = proxy;
WINHTTP_PROXY_INFO proxy_info; WINHTTP_PROXY_INFO proxy_info;

View File

@ -556,8 +556,7 @@ OpenConfigRegistryKey(const WCHAR *config_name, HKEY *regkey, BOOL create)
return 0; return 0;
} }
_snwprintf(name, count, fmt, config_name); _snwprintf_s(name, count, _TRUNCATE, fmt, config_name);
name[count - 1] = L'\0';
if (!create) if (!create)
{ {