Fix QMultiHash::count(key) crash

As QMultiHash uses a pointer for the data, nullptr dereference is a
thing, so check for valid d before doing anything in count()

Fixes: QTBUG-91704
Pick-to: 6.0 6.1
Change-Id: Ia20440cd7bdc03cb09c77f796fb9c5b52765eac5
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Andrei Golubev 2021-03-11 10:01:04 +01:00
parent ae88dea1be
commit f226854d25
2 changed files with 19 additions and 0 deletions

View File

@ -1806,6 +1806,8 @@ public:
qsizetype count(const Key &key) const noexcept
{
if (!d)
return 0;
auto it = d->find(key);
if (it.isUnused())
return 0;
@ -1821,6 +1823,8 @@ public:
qsizetype count(const Key &key, const T &value) const noexcept
{
if (!d)
return 0;
auto it = d->find(key);
if (it.isUnused())
return 0;

View File

@ -80,6 +80,8 @@ private slots:
void hashOfHash();
void stdHash();
void countInEmptyHash();
};
struct IdentityTracker {
@ -1976,5 +1978,18 @@ void tst_QHash::stdHash()
QVERIFY(!strings.contains("z"));
}
void tst_QHash::countInEmptyHash()
{
{
QHash<int, int> hash;
QCOMPARE(hash.count(42), 0);
}
{
QMultiHash<int, int> hash;
QCOMPARE(hash.count(42), 0);
}
}
QTEST_APPLESS_MAIN(tst_QHash)
#include "tst_qhash.moc"