diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index f93b60d4bbd..80a8d1eec9f 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -521,7 +521,9 @@ void QList::reserve(qsizetype asize) template inline void QList::squeeze() { - if (d->needsDetach() || size() != capacity()) { + if (!d.isMutable()) + return; + if (d->needsDetach() || size() < capacity()) { // must allocate memory DataPointer detached(Data::allocate(size(), d->detachFlags() & ~Data::CapacityReserved)); if (size()) { diff --git a/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp b/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp index 27d4fd87d6c..288bc6e120a 100644 --- a/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp +++ b/tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp @@ -1870,6 +1870,8 @@ void tst_QArrayData::literals() QCOMPARE(l.capacity(), 0); for (int i = 0; i < 3; ++i) QCOMPARE(l.at(i).value, i); + l.squeeze(); // shouldn't detach + QCOMPARE(l.capacity(), 0); (void)l.begin(); // "detach" diff --git a/tests/auto/corelib/tools/qlist/tst_qlist.cpp b/tests/auto/corelib/tools/qlist/tst_qlist.cpp index 8a7100d14c1..d1e321f4dbe 100644 --- a/tests/auto/corelib/tools/qlist/tst_qlist.cpp +++ b/tests/auto/corelib/tools/qlist/tst_qlist.cpp @@ -3076,6 +3076,9 @@ void tst_QList::fromReadOnlyData() const { { QVector d = QVector::fromReadOnlyData("ABCDEFGHIJ"); + QCOMPARE(d.capacity(), 0); + d.squeeze(); + QCOMPARE(d.capacity(), 0); QCOMPARE(d.size(), 10u + 1u); for (int i = 0; i < 10; ++i) QCOMPARE(d.data()[i], char('A' + i));