You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@stdcxx.apache.org by Travis Vitek <tv...@quovadx.com> on 2007/09/29 01:26:52 UTC

[PATCH] __rw_setlocale not threadsafe

The __rw_setlocale destructor currently restores the previous locale when it isn't holding the lock. This can cause problems if another thread takes the lock and calls setlocale before the original thread restores the locale.

I believe this problem to be the cause of the following failure that I frequently get when running the 22.locale.numpunct.mt test.

    terminate called after throwing an instance of 'std::runtime_error'
      what():  /amd/devco/vitek/stdcxx-trunk/src/setlocale.cpp:128: __rw::__rw_setlocale::__rw_setlocale(const char *, int, int): bad locale name: "À¡G.utf8"
    Aborted (core dumped)

Note: The only time that the destructor needs to restore the locale is when the locale was actually changed. That case is indicated when _C_guard is set to some non-zero value.

Travis



2007-09-28  Travis Vitek  <vi...@roguewave.com>

	* setlocale.cpp (~__rw_setlocale): Restore the previous
	locale in a threadsafe manner. Simplified.

Index: setlocale.cpp
===================================================================
--- setlocale.cpp	(revision 578875)
+++ setlocale.cpp	(working copy)
@@ -135,18 +135,18 @@
 // was in effect when the object was constructed
 __rw_setlocale::~__rw_setlocale ()
 {
-    // release the lock
-    if (_C_guard)
-        __rw_setlocale_mutex._C_release ();
+    // if guard is set, constructor changed the locale
+    if (_C_guard) {
 
-    if (_C_name) {
-
-        // and restore the locale
+        // restore the locale
         ::setlocale (_C_cat, _C_name);
 
-        if (_C_name != _C_namebuf)
-            delete [] _C_name;
+        // release the lock
+        __rw_setlocale_mutex._C_release ();
     }
+
+    if (_C_name != _C_namebuf)
+        delete [] _C_name;
 }
 
 

RE: [PATCH] __rw_setlocale not threadsafe

Posted by Farid Zaripov <Fa...@epam.com>.
> -----Original Message-----
> From: Travis Vitek [mailto:tvitek@quovadx.com] 
> Sent: Saturday, September 29, 2007 2:27 AM
> To: stdcxx-dev@incubator.apache.org
> Subject: [PATCH] __rw_setlocale not threadsafe
> 
> 
> The __rw_setlocale destructor currently restores the previous 
> locale when it isn't holding the lock. This can cause 
> problems if another thread takes the lock and calls setlocale 
> before the original thread restores the locale.
> 
> I believe this problem to be the cause of the following 
> failure that I frequently get when running the 
> 22.locale.numpunct.mt test.
> 
>     terminate called after throwing an instance of 
> 'std::runtime_error'
>       what():  
> /amd/devco/vitek/stdcxx-trunk/src/setlocale.cpp:128: 
> __rw::__rw_setlocale::__rw_setlocale(const char *, int, int): 
> bad locale name: "À¡G.utf8"
>     Aborted (core dumped)
> 
> Note: The only time that the destructor needs to restore the 
> locale is when the locale was actually changed. That case is 
> indicated when _C_guard is set to some non-zero value.

  Commited thus: http://svn.apache.org/viewvc?rev=580961&view=rev

Farid.