From ea0d24809d24445f27b156b357b4b60be699b363 Mon Sep 17 00:00:00 2001 From: Vincent Richard Date: Thu, 6 Sep 2018 20:09:54 +0200 Subject: [PATCH] Useless mutex does not make nl_langinfo() thread-safe. --- src/vmime/platforms/posix/posixHandler.cpp | 53 ++++------------------ 1 file changed, 9 insertions(+), 44 deletions(-) diff --git a/src/vmime/platforms/posix/posixHandler.cpp b/src/vmime/platforms/posix/posixHandler.cpp index 20c0f342..f8861315 100644 --- a/src/vmime/platforms/posix/posixHandler.cpp +++ b/src/vmime/platforms/posix/posixHandler.cpp @@ -55,9 +55,6 @@ #include #include -#if VMIME_HAVE_PTHREAD -# include -#endif // VMIME_HAVE_PTHREAD /* #ifdef _POSIX_PRIORITY_SCHEDULING @@ -66,45 +63,6 @@ */ -#if VMIME_HAVE_PTHREAD - -namespace { - - // This construction ensures mutex will be initialized in compile-time - // and will be available any time in the runtime. - pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER; - - // Helper lock, to be exception safe all the time. - class PLockHelper { - - public: - - PLockHelper() { - - if (pthread_mutex_lock(&g_mutex) != 0) { - assert(!"unable to lock mutex - thread safety's void"); - } - } - - ~PLockHelper() { - - if (pthread_mutex_unlock(&g_mutex) != 0) { - assert(!"unable to unlock mutex - application's dead..."); - } - } - - private: - - // Object cannot be copied - PLockHelper(const PLockHelper&); - const PLockHelper& operator=(const PLockHelper&); - }; - -} // unnamed namespace - -#endif // VMIME_HAVE_PTHREAD - - namespace vmime { namespace platforms { namespace posix { @@ -173,9 +131,16 @@ const vmime::datetime posixHandler::getCurrentLocalTime() const { const vmime::charset posixHandler::getLocalCharset() const { - const PLockHelper lock; + // Note: nl_langinfo() might be affected by calls to setlocale() + // in a multithread environment. There is not MT-safe alternative + // to nl_langinfo(). + auto codeset = ::nl_langinfo(CODESET); - return vmime::charset(::nl_langinfo(CODESET)); + if (codeset) { + return vmime::charset(codeset); + } + + return vmime::charset(); }