From d11941db41f00525f907417bbcd6ac5ee30d8485 Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Wed, 30 Mar 2022 17:09:32 +0200 Subject: Q[Multi]Hash::reserve(): do nothing if desired size is less than current MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Calling Q[Multi]Hash::reserve(n) when n is much smaller than the current amount of elements in the hash, could result in an infinite loop, because at some point the algorithm could not find a free bucket for the element. Fixing it by returning early if the new desired capacity is less than current. Fixes: QTBUG-102067 Pick-to: 6.3 6.2 Change-Id: I38ef0b2168c4e2a317eedf91b2155b1fdffb1c27 Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Thiago Macieira --- tests/auto/corelib/tools/qhash/tst_qhash.cpp | 31 ++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'tests/auto/corelib/tools') diff --git a/tests/auto/corelib/tools/qhash/tst_qhash.cpp b/tests/auto/corelib/tools/qhash/tst_qhash.cpp index fac4ac7007..6f59fe4436 100644 --- a/tests/auto/corelib/tools/qhash/tst_qhash.cpp +++ b/tests/auto/corelib/tools/qhash/tst_qhash.cpp @@ -98,6 +98,7 @@ private slots: void fineTuningInEmptyHash(); void reserveShared(); + void reserveLessThanCurrentAmount(); void QTBUG98265(); @@ -2667,6 +2668,36 @@ void tst_QHash::reserveShared() QCOMPARE(hash.capacity(), oldCap); } +void tst_QHash::reserveLessThanCurrentAmount() +{ + { + QHash hash; + for (int i = 0; i < 1000; ++i) + hash.insert(i, i * 10); + + // This used to hang in an infinite loop: QTBUG-102067 + hash.reserve(1); + + // Make sure that hash still has all elements + for (int i = 0; i < 1000; ++i) + QCOMPARE(hash.value(i), i * 10); + } + { + QMultiHash hash; + for (int i = 0; i < 1000; ++i) { + hash.insert(i, i * 10); + hash.insert(i, i * 10 + 1); + } + + // This used to hang in infinite loop: QTBUG-102067 + hash.reserve(1); + + // Make sure that hash still has all elements + for (int i = 0; i < 1000; ++i) + QCOMPARE(hash.values(i), QList() << i * 10 + 1 << i * 10); + } +} + void tst_QHash::QTBUG98265() { QMultiHash a; -- cgit v1.2.3