Skip to content

Commit a2cd0b0

Browse files
committed
Merge #10058: No need to use OpenSSL malloc/free
6d5dd60 No need to use OpenSSL malloc/free (Thomas Snider) Tree-SHA512: 29f790067ffd5a10a8e1a621318a0ba445691f57c804aa3b7c8ca372c8408d8c7fe703c42b48018e400fc32e3feff5ab401d97433910ce2c50e69da0b8a6662e
2 parents e6033e0 + 6d5dd60 commit a2cd0b0

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

src/util.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,26 +123,24 @@ CTranslationInterface translationInterface;
123123
std::atomic<uint32_t> logCategories(0);
124124

125125
/** Init OpenSSL library multithreading support */
126-
static CCriticalSection** ppmutexOpenSSL;
126+
static std::unique_ptr<CCriticalSection[]> ppmutexOpenSSL;
127127
void locking_callback(int mode, int i, const char* file, int line) NO_THREAD_SAFETY_ANALYSIS
128128
{
129129
if (mode & CRYPTO_LOCK) {
130-
ENTER_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
130+
ENTER_CRITICAL_SECTION(ppmutexOpenSSL[i]);
131131
} else {
132-
LEAVE_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
132+
LEAVE_CRITICAL_SECTION(ppmutexOpenSSL[i]);
133133
}
134134
}
135135

136-
// Init
136+
// Singleton for wrapping OpenSSL setup/teardown.
137137
class CInit
138138
{
139139
public:
140140
CInit()
141141
{
142142
// Init OpenSSL library multithreading support
143-
ppmutexOpenSSL = (CCriticalSection**)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(CCriticalSection*));
144-
for (int i = 0; i < CRYPTO_num_locks(); i++)
145-
ppmutexOpenSSL[i] = new CCriticalSection();
143+
ppmutexOpenSSL.reset(new CCriticalSection[CRYPTO_num_locks()]);
146144
CRYPTO_set_locking_callback(locking_callback);
147145

148146
// OpenSSL can optionally load a config file which lists optional loadable modules and engines.
@@ -166,9 +164,8 @@ class CInit
166164
RAND_cleanup();
167165
// Shutdown OpenSSL library multithreading support
168166
CRYPTO_set_locking_callback(NULL);
169-
for (int i = 0; i < CRYPTO_num_locks(); i++)
170-
delete ppmutexOpenSSL[i];
171-
OPENSSL_free(ppmutexOpenSSL);
167+
// Clear the set of locks now to maintain symmetry with the constructor.
168+
ppmutexOpenSSL.reset();
172169
}
173170
}
174171
instance_of_cinit;

0 commit comments

Comments
 (0)