Fix incorrect use of setlocale() in wxLocale::IsAvailable().

The return value of setlocale() was used incorrectly in this code: it
represents the newly set locale and not the previously active one so we didn't
actually restore the original locale before.

Fix the code and check that we do actually restore the locale in a new unit
test for it.

See #13117.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@67405 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2011-04-06 14:37:27 +00:00
parent 3668a4f69e
commit fd1c361c33
2 changed files with 25 additions and 8 deletions

View File

@@ -1045,16 +1045,21 @@ bool wxLocale::IsAvailable(int lang)
#elif defined(__UNIX__) #elif defined(__UNIX__)
// Test if setting the locale works, then set it back. // Test if setting the locale works, then set it back.
const char *oldLocale = wxSetlocaleTryUTF8(LC_ALL, info->CanonicalName); char * const oldLocale = wxStrdupA(setlocale(LC_ALL, NULL));
if ( !oldLocale )
{ // Some platforms don't like xx_YY form and require xx only so test for
// Some C libraries don't like xx_YY form and require xx only // it too.
oldLocale = wxSetlocaleTryUTF8(LC_ALL, ExtractLang(info->CanonicalName)); const bool
if ( !oldLocale ) available = wxSetlocaleTryUTF8(LC_ALL, info->CanonicalName) ||
return false; wxSetlocaleTryUTF8(LC_ALL, ExtractLang(info->CanonicalName));
}
// restore the original locale // restore the original locale
wxSetlocale(LC_ALL, oldLocale); wxSetlocale(LC_ALL, oldLocale);
free(oldLocale);
if ( !available )
return false;
#endif #endif
return true; return true;

View File

@@ -43,12 +43,14 @@ private:
CPPUNIT_TEST( Headers ); CPPUNIT_TEST( Headers );
CPPUNIT_TEST( DateTimeFmtFrench ); CPPUNIT_TEST( DateTimeFmtFrench );
CPPUNIT_TEST( DateTimeFmtC ); CPPUNIT_TEST( DateTimeFmtC );
CPPUNIT_TEST( IsAvailable );
CPPUNIT_TEST_SUITE_END(); CPPUNIT_TEST_SUITE_END();
void Domain(); void Domain();
void Headers(); void Headers();
void DateTimeFmtFrench(); void DateTimeFmtFrench();
void DateTimeFmtC(); void DateTimeFmtC();
void IsAvailable();
wxLocale *m_locale; wxLocale *m_locale;
@@ -204,4 +206,14 @@ void IntlTestCase::DateTimeFmtC()
m_locale->GetInfo(wxLOCALE_TIME_FMT) ); m_locale->GetInfo(wxLOCALE_TIME_FMT) );
} }
void IntlTestCase::IsAvailable()
{
const wxString origLocale(setlocale(LC_ALL, NULL));
// Calling IsAvailable() shouldn't change the locale.
wxLocale::IsAvailable(wxLANGUAGE_ENGLISH);
CPPUNIT_ASSERT_EQUAL( origLocale, setlocale(LC_ALL, NULL) );
}
#endif // wxUSE_INTL #endif // wxUSE_INTL