diff --git a/src/corelib/tools/qcontainertools_impl.h b/src/corelib/tools/qcontainertools_impl.h index 425ec6ba394..cbb8772e4ba 100644 --- a/src/corelib/tools/qcontainertools_impl.h +++ b/src/corelib/tools/qcontainertools_impl.h @@ -398,12 +398,13 @@ template qsizetype qset_erase_if(QSet &set, Predicate &pred) { qsizetype result = 0; - auto it = set.begin(); - const auto e = set.end(); + auto it = set.cbegin(); + auto e = set.cend(); // stable across detach (QHash::end() is a stateless sentinel)... while (it != e) { if (pred(*it)) { ++result; it = set.erase(it); + e = set.cend(); // ...but re-set nonetheless, in case at some point it won't be } else { ++it; } diff --git a/src/corelib/tools/qhash.h b/src/corelib/tools/qhash.h index 524cdff7efe..e1aaccb6bed 100644 --- a/src/corelib/tools/qhash.h +++ b/src/corelib/tools/qhash.h @@ -991,12 +991,13 @@ private: if (isEmpty()) // prevents detaching shared null return false; auto it = d->findBucket(key); + if (it.isUnused()) + return false; + size_t bucket = it.toBucketIndex(d); detach(); it = typename Data::Bucket(d, bucket); // reattach in case of detach - if (it.isUnused()) - return false; d->erase(it); return true; } diff --git a/tests/auto/corelib/tools/qset/tst_qset.cpp b/tests/auto/corelib/tools/qset/tst_qset.cpp index ba42ccb7d79..cdcb293a7e7 100644 --- a/tests/auto/corelib/tools/qset/tst_qset.cpp +++ b/tests/auto/corelib/tools/qset/tst_qset.cpp @@ -402,17 +402,9 @@ void tst_QSet::removeOnlyDetachesIfSomethingGetsRemoved() QVERIFY(!copy.isDetached()); QVERIFY(!copy.remove(42)); - QEXPECT_FAIL("", "QTBUG-132831", Continue); - QVERIFY(!copy.isDetached()); - - copy = set; QVERIFY(!copy.isDetached()); QCOMPARE(copy.removeIf([] (auto) { return false; }), 0); - QEXPECT_FAIL("", "QTBUG-132831", Continue); - QVERIFY(!copy.isDetached()); - - copy = set; QVERIFY(!copy.isDetached()); QVERIFY(copy.remove(4));