QHstsCache - do not mix const/non-const iterators

Found by clazy (clazy-strict-iterators). QMap::find returns
'iterator' and this is what we need, since we need to modify
the value referenced by this iterator. But QMap::insert only
accepts 'const_iterator' and clazy warns about mixed const/non-const
iterators (though actually QHstsCache does not have the original
problem this check it trying to find). Since we do not share
cache and do not want to try detaching on non-const access, we
can use std::map which conveniently has 'insert' taking 'iterator'
as its first parameter. As a bonus this will also fix another
warning from clazy-range-loop check (when iterating over such
a non-const map).

Change-Id: Id82991cefce33723d177ed04058d15295e9b71d7
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
This commit is contained in:
Timur Pocheptsov 2017-10-23 13:58:59 +02:00
parent fb1cf63d77
commit 5b9f4a7130
2 changed files with 11 additions and 10 deletions

View File

@ -136,7 +136,7 @@ void QHstsCache::updateKnownHost(const QString &host, const QDateTime &expires,
return; return;
} }
knownHosts.insert(pos, hostName, newPolicy); knownHosts.insert(pos, {hostName, newPolicy});
if (hstsStore) if (hstsStore)
hstsStore->addToObserved(newPolicy); hstsStore->addToObserved(newPolicy);
return; return;
@ -144,8 +144,8 @@ void QHstsCache::updateKnownHost(const QString &host, const QDateTime &expires,
if (newPolicy.isExpired()) if (newPolicy.isExpired())
knownHosts.erase(pos); knownHosts.erase(pos);
else if (*pos != newPolicy) else if (pos->second != newPolicy)
*pos = std::move(newPolicy); pos->second = std::move(newPolicy);
else else
return; return;
@ -185,13 +185,13 @@ bool QHstsCache::isKnownHost(const QUrl &url) const
while (nameToTest.fragment.size()) { while (nameToTest.fragment.size()) {
auto const pos = knownHosts.find(nameToTest); auto const pos = knownHosts.find(nameToTest);
if (pos != knownHosts.end()) { if (pos != knownHosts.end()) {
if (pos.value().isExpired()) { if (pos->second.isExpired()) {
knownHosts.erase(pos); knownHosts.erase(pos);
if (hstsStore) { if (hstsStore) {
// Inform our store that this policy has expired. // Inform our store that this policy has expired.
hstsStore->addToObserved(pos.value()); hstsStore->addToObserved(pos->second);
} }
} else if (!superDomainMatch || pos.value().includesSubDomains()) { } else if (!superDomainMatch || pos->second.includesSubDomains()) {
return true; return true;
} }
} }
@ -215,9 +215,9 @@ void QHstsCache::clear()
QVector<QHstsPolicy> QHstsCache::policies() const QVector<QHstsPolicy> QHstsCache::policies() const
{ {
QVector<QHstsPolicy> values; QVector<QHstsPolicy> values;
values.reserve(knownHosts.size()); values.reserve(int(knownHosts.size()));
for (const auto &host : knownHosts) for (const auto &host : knownHosts)
values << host; values << host.second;
return values; return values;
} }

View File

@ -61,7 +61,8 @@
#include <QtCore/qglobal.h> #include <QtCore/qglobal.h>
#include <QtCore/qpair.h> #include <QtCore/qpair.h>
#include <QtCore/qurl.h> #include <QtCore/qurl.h>
#include <QtCore/qmap.h>
#include <map>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@ -117,7 +118,7 @@ private:
QStringRef fragment; QStringRef fragment;
}; };
mutable QMap<HostName, QHstsPolicy> knownHosts; mutable std::map<HostName, QHstsPolicy> knownHosts;
QHstsStore *hstsStore = nullptr; QHstsStore *hstsStore = nullptr;
}; };