Fail if environment variables define unknown locale under Unix

Don't always "succeed" in wxUILocale::UseDefault() and, consequently, in
wxLocale::Init(wxLANGUAGE_DEFAULT), under Unix systems, even if the
locale couldn't actually be set, as it can happen if the environment
variables contain a locale which is not supported on the current system,
e.g. a "mixed" locale such as "en_FR", or even a completely invalid
string such as "bloordyblop", which still used to succeed.

For now only fix it for reasonably modern systems with locale_t support,
it could be done even for the ancient ones without it later too if
anybody still cares about them.

Closes #23218.
This commit is contained in:
Vadim Zeitlin 2023-02-06 00:07:51 +01:00
parent 856c0371fa
commit 2ca76449bf
2 changed files with 15 additions and 1 deletions

View file

@ -730,7 +730,21 @@ wxUILocaleImpl* wxUILocaleImpl::CreateStdC()
/* static */
wxUILocaleImpl* wxUILocaleImpl::CreateUserDefault()
{
#ifdef HAVE_LOCALE_T
// Setting default locale can fail under Unix if LANG or LC_ALL are set to
// an unsupported value, so check for this here to let the caller know if
// we can't do it.
wxLocaleIdent locDef;
locale_t loc = TryCreateLocaleWithUTF8(locDef);
if ( !loc )
return nullptr;
return new wxUILocaleImplUnix(wxLocaleIdent(), loc);
#else // !HAVE_LOCALE_T
// We could temporarily change the locale here to check if it's supported,
// but for now don't bother and assume it is.
return new wxUILocaleImplUnix(wxLocaleIdent());
#endif // HAVE_LOCALE_T/!HAVE_LOCALE_T
}
/* static */

View file

@ -493,7 +493,7 @@ TEST_CASE("wxUILocale::ShowSystem", "[.]")
CHECK( locDef.Init(wxLANGUAGE_DEFAULT, wxLOCALE_DONT_LOAD_DEFAULT) );
WARN(GetLocaleDesc("After wxLocale::Init(wxLANGUAGE_DEFAULT)"));
REQUIRE( wxUILocale::UseDefault() );
CHECK( wxUILocale::UseDefault() );
WARN(GetLocaleDesc("After wxUILocale::UseDefault()"));
wxString preferredLangsStr;