]> git.saurik.com Git - wxWidgets.git/commitdiff
Fix incorrect use of setlocale() in wxLocale::IsAvailable().
authorVadim Zeitlin <vadim@wxwidgets.org>
Wed, 6 Apr 2011 14:37:27 +0000 (14:37 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Wed, 6 Apr 2011 14:37:27 +0000 (14:37 +0000)
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

src/common/intl.cpp
tests/intl/intltest.cpp

index 575900d041359e90d91902d1f48b5a20627af2e4..9b38ffbf14e52ff2f7e5ce8e5896267e18988700 100644 (file)
@@ -1045,16 +1045,21 @@ bool wxLocale::IsAvailable(int lang)
 #elif defined(__UNIX__)
 
     // Test if setting the locale works, then set it back.
-    const char *oldLocale = wxSetlocaleTryUTF8(LC_ALL, info->CanonicalName);
-    if ( !oldLocale )
-    {
-        // Some C libraries don't like xx_YY form and require xx only
-        oldLocale = wxSetlocaleTryUTF8(LC_ALL, ExtractLang(info->CanonicalName));
-        if ( !oldLocale )
-            return false;
-    }
+    char * const oldLocale = wxStrdupA(setlocale(LC_ALL, NULL));
+
+    // Some platforms don't like xx_YY form and require xx only so test for
+    // it too.
+    const bool
+        available = wxSetlocaleTryUTF8(LC_ALL, info->CanonicalName) ||
+                    wxSetlocaleTryUTF8(LC_ALL, ExtractLang(info->CanonicalName));
+
     // restore the original locale
     wxSetlocale(LC_ALL, oldLocale);
+
+    free(oldLocale);
+
+    if ( !available )
+        return false;
 #endif
 
     return true;
index 02b98472ed849721237d4817077c564ba089bce9..3308e4e2100d5fe6bf4b5805e1c279e87f71519b 100644 (file)
@@ -43,12 +43,14 @@ private:
         CPPUNIT_TEST( Headers );
         CPPUNIT_TEST( DateTimeFmtFrench );
         CPPUNIT_TEST( DateTimeFmtC );
+        CPPUNIT_TEST( IsAvailable );
     CPPUNIT_TEST_SUITE_END();
 
     void Domain();
     void Headers();
     void DateTimeFmtFrench();
     void DateTimeFmtC();
+    void IsAvailable();
 
     wxLocale *m_locale;
 
@@ -204,4 +206,14 @@ void IntlTestCase::DateTimeFmtC()
                     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