Skip to content

Commit 6d5dd60

Browse files
committed
No need to use OpenSSL malloc/free
1 parent 02d64bd commit 6d5dd60

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
@@ -117,26 +117,24 @@ std::atomic<bool> fReopenDebugLog(false);
117117
CTranslationInterface translationInterface;
118118

119119
/** Init OpenSSL library multithreading support */
120-
static CCriticalSection** ppmutexOpenSSL;
120+
static std::unique_ptr<CCriticalSection[]> ppmutexOpenSSL;
121121
void locking_callback(int mode, int i, const char* file, int line) NO_THREAD_SAFETY_ANALYSIS
122122
{
123123
if (mode & CRYPTO_LOCK) {
124-
ENTER_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
124+
ENTER_CRITICAL_SECTION(ppmutexOpenSSL[i]);
125125
} else {
126-
LEAVE_CRITICAL_SECTION(*ppmutexOpenSSL[i]);
126+
LEAVE_CRITICAL_SECTION(ppmutexOpenSSL[i]);
127127
}
128128
}
129129

130-
// Init
130+
// Singleton for wrapping OpenSSL setup/teardown.
131131
class CInit
132132
{
133133
public:
134134
CInit()
135135
{
136136
// Init OpenSSL library multithreading support
137-
ppmutexOpenSSL = (CCriticalSection**)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(CCriticalSection*));
138-
for (int i = 0; i < CRYPTO_num_locks(); i++)
139-
ppmutexOpenSSL[i] = new CCriticalSection();
137+
ppmutexOpenSSL.reset(new CCriticalSection[CRYPTO_num_locks()]);
140138
CRYPTO_set_locking_callback(locking_callback);
141139

142140
// OpenSSL can optionally load a config file which lists optional loadable modules and engines.
@@ -160,9 +158,8 @@ class CInit
160158
RAND_cleanup();
161159
// Shutdown OpenSSL library multithreading support
162160
CRYPTO_set_locking_callback(NULL);
163-
for (int i = 0; i < CRYPTO_num_locks(); i++)
164-
delete ppmutexOpenSSL[i];
165-
OPENSSL_free(ppmutexOpenSSL);
161+
// Clear the set of locks now to maintain symmetry with the constructor.
162+
ppmutexOpenSSL.reset();
166163
}
167164
}
168165
instance_of_cinit;

0 commit comments

Comments
 (0)