From 22416ecaaf58619c716229b71cdca558fda0a861 Mon Sep 17 00:00:00 2001 From: Zhang Yu Date: Wed, 30 Dec 2020 10:57:09 +0800 Subject: [PATCH] Fix QMultiHash::equal_range crashes QMultiHash::equal_range crashes when called in a const member function. The Data `d` is a NULL pointer when calling equal_range() before inserting data into an empty QMultiHash. Then calling`d->find` crashes. Fixes: QTBUG-89687 Pick-to: 6.0 Change-Id: I10c3d196cbc72aed8c8c922ef16534bba51037b7 Reviewed-by: Lars Knoll --- src/corelib/tools/qhash.h | 3 +++ tests/auto/corelib/tools/qhash/tst_qhash.cpp | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/src/corelib/tools/qhash.h b/src/corelib/tools/qhash.h index 4b4235aa99..b3c389903a 100644 --- a/src/corelib/tools/qhash.h +++ b/src/corelib/tools/qhash.h @@ -1901,6 +1901,9 @@ public: QPair equal_range(const Key &key) const noexcept { + if (!d) + return qMakePair(end(), end()); + auto it = d->find(key); if (it.isUnused()) return qMakePair(end(), end()); diff --git a/tests/auto/corelib/tools/qhash/tst_qhash.cpp b/tests/auto/corelib/tools/qhash/tst_qhash.cpp index a922ee55fe..014617f8d4 100644 --- a/tests/auto/corelib/tools/qhash/tst_qhash.cpp +++ b/tests/auto/corelib/tools/qhash/tst_qhash.cpp @@ -1735,6 +1735,13 @@ void tst_QHash::equal_range() QVERIFY(p2.first == cm1.cbegin() || p2.second == cm1.cend()); } + { + const QMultiHash cm2; + auto p1 = cm2.equal_range(0); + QVERIFY(p1.first == cm2.end()); + QVERIFY(p1.second == cm2.end()); + } + QMultiHash h2; for (int i = 0; i < 8; ++i) for (int j = 0; j < 8; ++j)