fixed race condition in iconv string converter

This commit is contained in:
Arvid Norberg 2011-01-22 04:33:21 +00:00
parent 6db9c5be31
commit 5c20606397
2 changed files with 11 additions and 1 deletions

View File

@ -72,6 +72,7 @@
incoming connection
* added more detailed instrumentation of the disk I/O thread
* fixed race condition in iconv string converter
* fixed error handling in torrent_info constructor
* fixed bug in torrent_info::remap_files
* fix python binding for wait_for_alert

View File

@ -55,6 +55,7 @@ POSSIBILITY OF SUCH DAMAGE.
#endif
#include "libtorrent/utf8.hpp"
#include "libtorrent/thread.hpp"
#if TORRENT_USE_ICONV
#include <iconv.h>
@ -646,7 +647,7 @@ namespace libtorrent
char const* in = s.c_str();
char* out = &ret[0];
#ifdef TORRENT_LINUX
// linux seems to have a weird iconv signature
// linux (and posix) seems to have a weird iconv signature
#define ICONV_IN_CAST (char**)
#else
#define ICONV_IN_CAST
@ -666,6 +667,10 @@ namespace libtorrent
std::string convert_to_native(std::string const& s)
{
static mutex iconv_mutex;
// only one thread can use this handle at a time
mutex::scoped_lock l(iconv_mutex);
// the empty string represents the local dependent encoding
static iconv_t iconv_handle = iconv_open("", "UTF-8");
if (iconv_handle == iconv_t(-1)) return s;
@ -674,6 +679,10 @@ namespace libtorrent
std::string convert_from_native(std::string const& s)
{
static mutex iconv_mutex;
// only one thread can use this handle at a time
mutex::scoped_lock l(iconv_mutex);
// the empty string represents the local dependent encoding
static iconv_t iconv_handle = iconv_open("UTF-8", "");
if (iconv_handle == iconv_t(-1)) return s;