From 5c206063974c8f81e1715e4a387e27778afbda0c Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sat, 22 Jan 2011 04:33:21 +0000 Subject: [PATCH] fixed race condition in iconv string converter --- ChangeLog | 1 + src/escape_string.cpp | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 6733673e4..dfe738f43 100644 --- a/ChangeLog +++ b/ChangeLog @@ -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 diff --git a/src/escape_string.cpp b/src/escape_string.cpp index 8559d80b3..2ce238723 100644 --- a/src/escape_string.cpp +++ b/src/escape_string.cpp @@ -55,6 +55,7 @@ POSSIBILITY OF SUCH DAMAGE. #endif #include "libtorrent/utf8.hpp" +#include "libtorrent/thread.hpp" #if TORRENT_USE_ICONV #include @@ -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;