Skip to content

Commit

Permalink
QTlsBackendOpenSSL: Early return from ensureCiphersAndCertsLoaded()
Browse files Browse the repository at this point in the history
Add an atomic state variable to perform early return without taking
a recursive lock after ensureCiphersAndCertsLoaded() is complete.

Make related mutex and state variable function-local static because
they are not used anywhere else.

Taks-number: QTBUG-103559
Change-Id: I1e4c9c4f73204885bce82ba7f2b5e64548c3aac3
Reviewed-by: Mårten Nordheim <[email protected]>
  • Loading branch information
eugmes committed Sep 7, 2022
1 parent 42c1e3c commit 8bada69
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
22 changes: 16 additions & 6 deletions src/plugins/tls/openssl/qtlsbackend_openssl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ using namespace Qt::StringLiterals;

Q_LOGGING_CATEGORY(lcTlsBackend, "qt.tlsbackend.ossl");

Q_GLOBAL_STATIC(QRecursiveMutex, qt_opensslInitMutex)

static void q_loadCiphersForConnection(SSL *connection, QList<QSslCipher> &ciphers,
QList<QSslCipher> &defaultCiphers)
{
Expand All @@ -59,7 +57,6 @@ static void q_loadCiphersForConnection(SSL *connection, QList<QSslCipher> &ciphe
}
}

bool QTlsBackendOpenSSL::s_loadedCiphersAndCerts = false;
int QTlsBackendOpenSSL::s_indexForSSLExtraData = -1;

QString QTlsBackendOpenSSL::getErrorsFromOpenSsl()
Expand Down Expand Up @@ -172,11 +169,24 @@ void QTlsBackendOpenSSL::ensureInitialized() const

void QTlsBackendOpenSSL::ensureCiphersAndCertsLoaded() const
{
const QMutexLocker locker(qt_opensslInitMutex());
Q_CONSTINIT static bool initializationStarted = false;
Q_CONSTINIT static QAtomicInt initialized = Q_BASIC_ATOMIC_INITIALIZER(0);
Q_CONSTINIT static QRecursiveMutex initMutex;

if (initialized.loadAcquire())
return;

if (s_loadedCiphersAndCerts)
const QMutexLocker locker(&initMutex);

if (initializationStarted || initialized.loadAcquire())
return;
s_loadedCiphersAndCerts = true;

// Indicate that the initialization has already started in the current
// thread in case of recursive calls. The atomic variable cannot be used
// for this because it is checked without holding the init mutex.
initializationStarted = true;

auto guard = qScopeGuard([] { initialized.storeRelease(1); });

resetDefaultCiphers();
resetDefaultEllipticCurves();
Expand Down
1 change: 0 additions & 1 deletion src/plugins/tls/openssl/qtlsbackend_openssl_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class QTlsBackendOpenSSL final : public QTlsBackend
static void clearErrorQueue();

// Index used in SSL_get_ex_data to get the matching TlsCryptographerOpenSSL:
static bool s_loadedCiphersAndCerts;
static int s_indexForSSLExtraData;

static QString msgErrorsDuringHandshake();
Expand Down

0 comments on commit 8bada69

Please sign in to comment.