Skip to content

Commit

Permalink
Fix OpenSSLInitialized thread safety (#1739) (#1740)
Browse files Browse the repository at this point in the history
* Fix OpenSSLInitialized thread safety (#1739)

The init/uninit methods can be called from multiple threads, and thus need synchronization with a mutex.

* Renamed mutex variable and use ScopedLock.

* Change reference count variable to be an integer, since it’s protected by a mutex and no longer needs to be atomic.
  • Loading branch information
neotron authored and aleks-f committed Jul 18, 2017
1 parent b59d74c commit 40324cd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Crypto/include/Poco/Crypto/OpenSSLInitializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ class Crypto_API OpenSSLInitializer
static void dynlockDestroy(struct CRYPTO_dynlock_value* lock, const char* file, int line);

private:
static Poco::FastMutex _mutex;
static Poco::FastMutex* _mutexes;
static Poco::AtomicCounter _rc;
static int _rc;
static bool _disableSSLInitialization;
};

Expand Down
6 changes: 4 additions & 2 deletions Crypto/src/OpenSSLInitializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ using Poco::Thread;
namespace Poco {
namespace Crypto {


Poco::FastMutex OpenSSLInitializer::_mutex;
Poco::FastMutex* OpenSSLInitializer::_mutexes(0);
Poco::AtomicCounter OpenSSLInitializer::_rc;
int OpenSSLInitializer::_rc(0);
bool OpenSSLInitializer::_disableSSLInitialization = false;

OpenSSLInitializer::OpenSSLInitializer()
Expand All @@ -59,6 +59,7 @@ OpenSSLInitializer::~OpenSSLInitializer()

void OpenSSLInitializer::initialize()
{
FastMutex::ScopedLock lock(_mutex);
if (++_rc == 1)
{
#if OPENSSL_VERSION_NUMBER >= 0x0907000L
Expand Down Expand Up @@ -104,6 +105,7 @@ void OpenSSLInitializer::initialize()

void OpenSSLInitializer::uninitialize()
{
FastMutex::ScopedLock lock(_mutex);
if (--_rc == 0)
{
if(_mutexes != NULL) {
Expand Down

0 comments on commit 40324cd

Please sign in to comment.