diff --git a/src/corelib/tools/qarraydatapointer.h b/src/corelib/tools/qarraydatapointer.h index 258e7b7446b..a77db21c99b 100644 --- a/src/corelib/tools/qarraydatapointer.h +++ b/src/corelib/tools/qarraydatapointer.h @@ -327,12 +327,10 @@ public: const qsizetype n = std::distance(first, last); if (needsDetach() || n > constAllocatedCapacity()) { QArrayDataPointer allocated(Data::allocate(detachCapacity(n))); - Q_CHECK_PTR(allocated.data()); swap(allocated); } } else if (needsDetach()) { QArrayDataPointer allocated(Data::allocate(allocatedCapacity())); - Q_CHECK_PTR(allocated.data()); swap(allocated); // We don't want to copy data that we know we'll overwrite } diff --git a/tests/auto/corelib/tools/qlist/tst_qlist.cpp b/tests/auto/corelib/tools/qlist/tst_qlist.cpp index afced64b42c..2b910e2a8ee 100644 --- a/tests/auto/corelib/tools/qlist/tst_qlist.cpp +++ b/tests/auto/corelib/tools/qlist/tst_qlist.cpp @@ -231,6 +231,7 @@ private slots: void appendCustom() const { append(); } void appendRvalue() const; void appendList() const; + void assignEmpty() const; void assignInt() const { assign(); } void assignMovable() const { assign(); } void assignCustom() const { assign(); } @@ -762,6 +763,25 @@ void tst_QList::append() const } } +void tst_QList::assignEmpty() const +{ + // Test that the realloc branch in assign(it, it) doesn't crash. + using T = int; + QList list; + QList ref1 = list; + QVERIFY(list.d.needsDetach()); + list.assign(list.begin(), list.begin()); + +#if !defined Q_OS_QNX // QNX has problems with the empty istream_iterator + auto empty = std::istream_iterator{}; + list.squeeze(); + QCOMPARE_EQ(list.capacity(), 0); + ref1 = list; + QVERIFY(list.d.needsDetach()); + list.assign(empty, empty); +#endif +} + template void tst_QList::assign() const {