Index: src/plugin/PluginSystem.cpp |
=================================================================== |
--- a/src/plugin/PluginSystem.cpp |
+++ b/src/plugin/PluginSystem.cpp |
@@ -1,35 +1,40 @@ |
#include "PluginStdAfx.h" |
- |
#include "PluginSystem.h" |
#include "PluginClient.h" |
+#include <array> |
std::wstring GetBrowserLanguage() |
{ |
LANGID lcid = GetUserDefaultLangID(); |
- wchar_t language[128]; |
- memset(language, 0, sizeof(language)); |
- wchar_t country[128]; |
- memset(language, 0, sizeof(country)); |
- |
std::wstring lang; |
- int res = GetLocaleInfoW(lcid, LOCALE_SISO639LANGNAME, language, 127); |
- if (res == 0) |
+ // we use `size() - 1` here to avoid additional calls to get the required size or to get the |
+ // number of retrieved characters. |
{ |
- DEBUG_ERROR_LOG(::GetLastError(), PLUGIN_ERROR_SYSINFO, PLUGIN_ERROR_SYSINFO_BROWSER_LANGUAGE, "System::GetBrowserLang - Failed"); |
- } |
- else |
- { |
- lang += language; |
+ std::array<wchar_t, 128> localeLanguage; |
Eric
2014/09/26 18:07:23
The actual buffer size we need, both here for lang
|
+ localeLanguage.fill(L'\0'); |
+ int res = GetLocaleInfoW(lcid, LOCALE_SISO639LANGNAME, localeLanguage.data(), localeLanguage.size() - 1); |
+ if (res == 0) |
+ { |
+ DEBUG_ERROR_LOG(::GetLastError(), PLUGIN_ERROR_SYSINFO, PLUGIN_ERROR_SYSINFO_BROWSER_LANGUAGE, "System::GetBrowserLang - Failed"); |
+ } |
+ else |
+ { |
+ lang += localeLanguage.data(); |
+ } |
} |
lang += L"-"; |
- res = GetLocaleInfoW(lcid, LOCALE_SISO3166CTRYNAME, country, 127); |
- if (res == 0) |
{ |
- DEBUG_ERROR_LOG(::GetLastError(), PLUGIN_ERROR_SYSINFO, PLUGIN_ERROR_SYSINFO_BROWSER_LANGUAGE, "System::GetBrowserLang - failed to retrieve country"); |
- } |
- else |
- { |
- lang += country; |
+ std::array<wchar_t, 128> localeCountry; |
+ localeCountry.fill(L'\0'); |
+ int res = GetLocaleInfoW(lcid, LOCALE_SISO3166CTRYNAME, localeCountry.data(), localeCountry.size() - 1); |
+ if (res == 0) |
+ { |
+ DEBUG_ERROR_LOG(::GetLastError(), PLUGIN_ERROR_SYSINFO, PLUGIN_ERROR_SYSINFO_BROWSER_LANGUAGE, "System::GetBrowserLang - failed to retrieve country"); |
+ } |
+ else |
+ { |
+ lang += localeCountry.data(); |
+ } |
} |
return lang; |
} |