diff --git a/src/corelib/tools/qhash.h b/src/corelib/tools/qhash.h index b3c389903a3..4fb68ba0b56 100644 --- a/src/corelib/tools/qhash.h +++ b/src/corelib/tools/qhash.h @@ -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; diff --git a/tests/auto/corelib/tools/qhash/tst_qhash.cpp b/tests/auto/corelib/tools/qhash/tst_qhash.cpp index 014617f8d49..079b9c6ab7e 100644 --- a/tests/auto/corelib/tools/qhash/tst_qhash.cpp +++ b/tests/auto/corelib/tools/qhash/tst_qhash.cpp @@ -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 hash; + QCOMPARE(hash.count(42), 0); + } + + { + QMultiHash hash; + QCOMPARE(hash.count(42), 0); + } +} + QTEST_APPLESS_MAIN(tst_QHash) #include "tst_qhash.moc"