From 8a549506464d1356fe9ff63ac0c56ea85cd38e8b Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 4 Feb 2016 10:38:50 +0100 Subject: [PATCH] Optimize QSslSocketPrivate::isMatchingHostname() - Lowercase 'peerName' only once instead of each time though the loop - Use C++11 range-for instead of Q_FOREACH - Use QMap::equal_range instead of values(Key), saving the creation of a QList. Change-Id: I1229f62d706d1478960b08bb63ee9fb894364f87 Reviewed-by: Lars Knoll Reviewed-by: Richard J. Moore Reviewed-by: Thiago Macieira --- src/network/ssl/qsslsocket.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/network/ssl/qsslsocket.cpp b/src/network/ssl/qsslsocket.cpp index 7ff70abac3..ae695a07e9 100644 --- a/src/network/ssl/qsslsocket.cpp +++ b/src/network/ssl/qsslsocket.cpp @@ -2621,18 +2621,19 @@ QSharedPointer QSslSocketPrivate::sslContext(QSslSocket *socket) bool QSslSocketPrivate::isMatchingHostname(const QSslCertificate &cert, const QString &peerName) { - QStringList commonNameList = cert.subjectInfo(QSslCertificate::CommonName); + const QString lowerPeerName = peerName.toLower(); + const QStringList commonNames = cert.subjectInfo(QSslCertificate::CommonName); - foreach (const QString &commonName, commonNameList) { - if (isMatchingHostname(commonName.toLower(), peerName.toLower())) { + for (const QString &commonName : commonNames) { + if (isMatchingHostname(commonName.toLower(), lowerPeerName)) return true; - } } - foreach (const QString &altName, cert.subjectAlternativeNames().values(QSsl::DnsEntry)) { - if (isMatchingHostname(altName.toLower(), peerName.toLower())) { + const auto subjectAlternativeNames = cert.subjectAlternativeNames(); + const auto altNames = subjectAlternativeNames.equal_range(QSsl::DnsEntry); + for (auto it = altNames.first; it != altNames.second; ++it) { + if (isMatchingHostname(it->toLower(), lowerPeerName)) return true; - } } return false;