summaryrefslogtreecommitdiffstats
path: root/src/plugins/tls
diff options
context:
space:
mode:
authorIevgenii Meshcheriakov <ievgenii.meshcheriakov@qt.io>2022-08-31 10:25:23 +0200
committerIevgenii Meshcheriakov <ievgenii.meshcheriakov@qt.io>2022-09-07 12:39:25 +0000
commit8bada697d6b8d7202e7975d73a5845c999fac8b2 (patch)
tree62df020e15081848c9a9de0f54f3134ef4041d84 /src/plugins/tls
parent42c1e3c3347555898d85d17552444e885c8c31ec (diff)
QTlsBackendOpenSSL: Early return from ensureCiphersAndCertsLoaded()
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 <marten.nordheim@qt.io>
Diffstat (limited to 'src/plugins/tls')
-rw-r--r--src/plugins/tls/openssl/qtlsbackend_openssl.cpp22
-rw-r--r--src/plugins/tls/openssl/qtlsbackend_openssl_p.h1
2 files changed, 16 insertions, 7 deletions
diff --git a/src/plugins/tls/openssl/qtlsbackend_openssl.cpp b/src/plugins/tls/openssl/qtlsbackend_openssl.cpp
index b454a20f2a..dfd48e01af 100644
--- a/src/plugins/tls/openssl/qtlsbackend_openssl.cpp
+++ b/src/plugins/tls/openssl/qtlsbackend_openssl.cpp
@@ -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)
{
@@ -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()
@@ -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();
diff --git a/src/plugins/tls/openssl/qtlsbackend_openssl_p.h b/src/plugins/tls/openssl/qtlsbackend_openssl_p.h
index 6ff1052957..b9f1f95df0 100644
--- a/src/plugins/tls/openssl/qtlsbackend_openssl_p.h
+++ b/src/plugins/tls/openssl/qtlsbackend_openssl_p.h
@@ -42,7 +42,6 @@ public:
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();