From 77037744377c7e4b02c640df4264843e73003099 Mon Sep 17 00:00:00 2001 From: muemart Date: Thu, 27 Jun 2019 19:46:03 +0200 Subject: [PATCH] Correctly determine size of wchar arrays (#9490) --- src/openrct2/platform/Crash.cpp | 13 +++++++------ src/openrct2/platform/Platform.Win32.cpp | 4 ++-- src/openrct2/platform/Windows.cpp | 12 ++++++------ 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/openrct2/platform/Crash.cpp b/src/openrct2/platform/Crash.cpp index 0ae6f68ee1..45b0dd0525 100644 --- a/src/openrct2/platform/Crash.cpp +++ b/src/openrct2/platform/Crash.cpp @@ -10,6 +10,7 @@ #include "Crash.h" #ifdef USE_BREAKPAD +# include # include # include # include @@ -99,18 +100,18 @@ static bool OnCrash( wchar_t saveFilePath[MAX_PATH]; wchar_t configFilePath[MAX_PATH]; wchar_t saveFilePathGZIP[MAX_PATH]; - swprintf_s(dumpFilePath, sizeof(dumpFilePath), L"%s\\%s.dmp", dumpPath, miniDumpId); - swprintf_s(saveFilePath, sizeof(saveFilePath), L"%s\\%s.sv6", dumpPath, miniDumpId); - swprintf_s(configFilePath, sizeof(configFilePath), L"%s\\%s.ini", dumpPath, miniDumpId); - swprintf_s(saveFilePathGZIP, sizeof(saveFilePathGZIP), L"%s\\%s.sv6.gz", dumpPath, miniDumpId); + swprintf_s(dumpFilePath, std::size(dumpFilePath), L"%s\\%s.dmp", dumpPath, miniDumpId); + swprintf_s(saveFilePath, std::size(saveFilePath), L"%s\\%s.sv6", dumpPath, miniDumpId); + swprintf_s(configFilePath, std::size(configFilePath), L"%s\\%s.ini", dumpPath, miniDumpId); + swprintf_s(saveFilePathGZIP, std::size(saveFilePathGZIP), L"%s\\%s.sv6.gz", dumpPath, miniDumpId); wchar_t dumpFilePathNew[MAX_PATH]; swprintf_s( - dumpFilePathNew, sizeof(dumpFilePathNew), L"%s\\%s(%s_%s).dmp", dumpPath, miniDumpId, _wszCommitSha1Short, + dumpFilePathNew, std::size(dumpFilePathNew), L"%s\\%s(%s_%s).dmp", dumpPath, miniDumpId, _wszCommitSha1Short, _wszArchitecture); wchar_t dumpFilePathGZIP[MAX_PATH]; - swprintf_s(dumpFilePathGZIP, sizeof(dumpFilePathGZIP), L"%s.gz", dumpFilePathNew); + swprintf_s(dumpFilePathGZIP, std::size(dumpFilePathGZIP), L"%s.gz", dumpFilePathNew); // Compress the dump { diff --git a/src/openrct2/platform/Platform.Win32.cpp b/src/openrct2/platform/Platform.Win32.cpp index 448e149d7e..62ecff739c 100644 --- a/src/openrct2/platform/Platform.Win32.cpp +++ b/src/openrct2/platform/Platform.Win32.cpp @@ -179,7 +179,7 @@ namespace Platform # ifdef __USE_GETDATEFORMATEX__ wchar_t date[20]; - GetDateFormatEx(LOCALE_NAME_USER_DEFAULT, DATE_SHORTDATE, &st, nullptr, date, sizeof(date), nullptr); + GetDateFormatEx(LOCALE_NAME_USER_DEFAULT, DATE_SHORTDATE, &st, nullptr, date, (int)std::size(date), nullptr); std::string result = String::ToUtf8(std::wstring(date)); # else char date[20]; @@ -196,7 +196,7 @@ namespace Platform # ifdef __USE_GETDATEFORMATEX__ wchar_t time[20]; - GetTimeFormatEx(LOCALE_NAME_USER_DEFAULT, 0, &st, nullptr, time, sizeof(time)); + GetTimeFormatEx(LOCALE_NAME_USER_DEFAULT, 0, &st, nullptr, time, (int)std::size(time)); std::string result = String::ToUtf8(std::wstring(time)); # else char time[20]; diff --git a/src/openrct2/platform/Windows.cpp b/src/openrct2/platform/Windows.cpp index e3a0d6f2bc..78535d3bcf 100644 --- a/src/openrct2/platform/Windows.cpp +++ b/src/openrct2/platform/Windows.cpp @@ -413,7 +413,7 @@ uint8_t platform_get_locale_date_format() { // Retrieve short date format, eg "MM/dd/yyyy" wchar_t dateFormat[20]; - if (GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_SSHORTDATE, dateFormat, sizeof(dateFormat)) == 0) + if (GetLocaleInfoEx(LOCALE_NAME_USER_DEFAULT, LOCALE_SSHORTDATE, dateFormat, (int)std::size(dateFormat)) == 0) { return DATE_FORMAT_DAY_MONTH_YEAR; } @@ -584,8 +584,8 @@ static bool windows_setup_file_association( [[maybe_unused]] int32_t printResult; - GetModuleFileNameW(nullptr, exePathW, sizeof(exePathW)); - GetModuleFileNameW(plaform_get_dll_module(), dllPathW, sizeof(dllPathW)); + GetModuleFileNameW(nullptr, exePathW, (DWORD)std::size(exePathW)); + GetModuleFileNameW(plaform_get_dll_module(), dllPathW, (DWORD)std::size(dllPathW)); wchar_t* extensionW = utf8_to_widechar(extension); wchar_t* fileTypeTextW = utf8_to_widechar(fileTypeText); @@ -737,7 +737,7 @@ bool platform_setup_uri_protocol() GetModuleFileNameW(nullptr, exePath, MAX_PATH); wchar_t buffer[512]; - swprintf_s(buffer, sizeof(buffer), L"\"%s\" handle-uri \"%%1\"", exePath); + swprintf_s(buffer, std::size(buffer), L"\"%s\" handle-uri \"%%1\"", exePath); if (RegSetValueW(hClassKey, L"shell\\open\\command", REG_SZ, buffer, 0) == ERROR_SUCCESS) { // Not compulsory, but gives the application a nicer name @@ -745,11 +745,11 @@ bool platform_setup_uri_protocol() HKEY hMuiCacheKey; if (RegCreateKeyW(hRootKey, MUI_CACHE, &hMuiCacheKey) == ERROR_SUCCESS) { - swprintf_s(buffer, sizeof(buffer), L"%s.FriendlyAppName", exePath); + swprintf_s(buffer, std::size(buffer), L"%s.FriendlyAppName", exePath); // mingw-w64 used to define RegSetKeyValueW's signature incorrectly // You need at least mingw-w64 5.0 including this commit: // https://sourceforge.net/p/mingw-w64/mingw-w64/ci/da9341980a4b70be3563ac09b5927539e7da21f7/ - RegSetKeyValueW(hMuiCacheKey, nullptr, buffer, REG_SZ, L"OpenRCT2", sizeof(L"OpenRCT2") + 1); + RegSetKeyValueW(hMuiCacheKey, nullptr, buffer, REG_SZ, L"OpenRCT2", sizeof(L"OpenRCT2")); } log_verbose("URI protocol setup successful");