From ee39a898641a1ef55a3a683271d768c17dbfcdd6 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Thu, 1 May 2025 01:21:18 +0300 Subject: [PATCH] qtlsbackend_openssl: optimize QDirListing usage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Internally QDirListing uses the name filters to create QRegularExpression objects which are then used to do the matching. Here we are looking for files that have ".pem" or ".crt" extensions, so basic string matching should work the same and is inherently faster. Change-Id: Ib19b1eb8717b21c3b96a52e7036665c40fb24caf Reviewed-by: MÃ¥rten Nordheim (cherry picked from commit 6dcf1483940cb180cbe59e0ea8781d722ae6cddd) Reviewed-by: Qt Cherry-pick Bot --- src/plugins/tls/openssl/qtlsbackend_openssl.cpp | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/plugins/tls/openssl/qtlsbackend_openssl.cpp b/src/plugins/tls/openssl/qtlsbackend_openssl.cpp index 15bf384ae86..6a55f275943 100644 --- a/src/plugins/tls/openssl/qtlsbackend_openssl.cpp +++ b/src/plugins/tls/openssl/qtlsbackend_openssl.cpp @@ -391,13 +391,22 @@ QList systemCaCertificates() QStringLiteral("/etc/pki/tls/certs/ca-bundle.crt"), // Fedora, Mandriva QStringLiteral("/usr/local/share/certs/ca-root-nss.crt") // FreeBSD's ca_root_nss }; - static const QStringList nameFilters = {u"*.pem"_s, u"*.crt"_s}; + + static const size_t extLen = strlen(".pem"); // or strlen(".crt") + auto hasMatchingExtension = [](const QString &fileName) { + if (size_t(fileName.size()) < extLen + 1) + return false; + auto ext = QStringView{fileName}.last(extLen); + return ext == ".pem"_L1 || ext == ".crt"_L1; + }; + using F = QDirListing::IteratorFlag; constexpr auto flags = F::FilesOnly | F::ResolveSymlinks; // Files and symlinks to files for (const QByteArray &directory : directories) { - for (const auto &dirEntry : QDirListing(QFile::decodeName(directory), nameFilters, flags)) { + for (const auto &dirEntry : QDirListing(QFile::decodeName(directory), flags)) { // use canonical path here to not load the same certificate twice if symlinked - certFiles.insert(dirEntry.canonicalFilePath()); + if (hasMatchingExtension(dirEntry.fileName())) + certFiles.insert(dirEntry.canonicalFilePath()); } } for (const QString& file : std::as_const(certFiles))