From 6512a7fc642c65455db770385c67cfa6d71c294c Mon Sep 17 00:00:00 2001 From: Andrei Golubev Date: Tue, 9 Feb 2021 14:03:04 +0100 Subject: Restore pre-Qt6 QList::fill() behavior Somehow QList::fill(t, newSize) introduced a regression in Qt6: when newSize < QList::size() we should resize to the newSize. This is aligned with QVector::fill() in 5.15 and std::vector::assign() While 6.0 is already out, picking it to 6.0.x could save someone who haven't migrated yet as well as fix some accidental bugs in Qt's code [ChangeLog][QtCore][QList] Fixed QList::fill() regression introduced in 6.0: calling fill() with size < current list size wouldn't truncate the list Fixes: QTBUG-91042 Pick-to: 6.0 6.1 Change-Id: Ic166e2c5e42390b61df1030f7c705e344433f7f2 Reviewed-by: Lars Knoll --- src/corelib/tools/qlist.h | 5 +- tests/auto/corelib/tools/qlist/tst_qlist.cpp | 70 +++++++++++++++++++++++++--- 2 files changed, 67 insertions(+), 8 deletions(-) diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index 662fede6f7..b50cd9090f 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -834,8 +834,11 @@ inline QList &QList::fill(parameter_type t, qsizetype newSize) // we're detached const T copy(t); d->assign(d.begin(), d.begin() + qMin(size(), newSize), t); - if (newSize > size()) + if (newSize > size()) { d->copyAppend(newSize - size(), copy); + } else if (newSize < size()) { + d->truncate(newSize); + } } return *this; } diff --git a/tests/auto/corelib/tools/qlist/tst_qlist.cpp b/tests/auto/corelib/tools/qlist/tst_qlist.cpp index 64123c61a7..80adb0f6a1 100644 --- a/tests/auto/corelib/tools/qlist/tst_qlist.cpp +++ b/tests/auto/corelib/tools/qlist/tst_qlist.cpp @@ -256,7 +256,9 @@ private slots: void fillInt() const; void fillMovable() const; void fillCustom() const; - void fillDetaches() const; + void fillDetachInt() const; + void fillDetachMovable() const; + void fillDetachCustom() const; void first() const; void fromListInt() const; void fromListMovable() const; @@ -363,6 +365,7 @@ private: template void erase(bool shared) const; template void eraseReserved() const; template void fill() const; + template void fillDetach() const; template void fromList() const; template void insert() const; template void qhash() const; @@ -1443,6 +1446,10 @@ void tst_QList::fill() const << SimpleValue::at(2) << SimpleValue::at(2) << SimpleValue::at(2) << SimpleValue::at(2) << SimpleValue::at(2) << SimpleValue::at(2)); + + // make sure it can resize to smaller size as well + myvec.fill(SimpleValue::at(3), 2); + QCOMPARE(myvec, QList() << SimpleValue::at(3) << SimpleValue::at(3)); } void tst_QList::fillInt() const @@ -1464,14 +1471,63 @@ void tst_QList::fillCustom() const QCOMPARE(instancesCount, Custom::counter.loadAcquire()); } -void tst_QList::fillDetaches() const +template +void tst_QList::fillDetach() const { - QList test = { 1, 2, 3 }; - QList copy = test; - copy.fill(42); + // detaches to the same size + { + QList original = { SimpleValue::at(1), SimpleValue::at(1), SimpleValue::at(1) }; + QList copy = original; + copy.fill(SimpleValue::at(2)); + + QCOMPARE(original, + QList({ SimpleValue::at(1), SimpleValue::at(1), SimpleValue::at(1) })); + QCOMPARE(copy, + QList({ SimpleValue::at(2), SimpleValue::at(2), SimpleValue::at(2) })); + } + + // detaches and grows in size + { + QList original = { SimpleValue::at(1), SimpleValue::at(1), SimpleValue::at(1) }; + QList copy = original; + copy.fill(SimpleValue::at(2), 5); - QCOMPARE(test, QList({1, 2, 3})); - QCOMPARE(copy, QList({42, 42, 42})); + QCOMPARE(original, + QList({ SimpleValue::at(1), SimpleValue::at(1), SimpleValue::at(1) })); + QCOMPARE(copy, + QList({ SimpleValue::at(2), SimpleValue::at(2), SimpleValue::at(2), + SimpleValue::at(2), SimpleValue::at(2) })); + } + + // detaches and shrinks in size + { + QList original = { SimpleValue::at(1), SimpleValue::at(1), SimpleValue::at(1) }; + QList copy = original; + copy.fill(SimpleValue::at(2), 1); + + QCOMPARE(original, + QList({ SimpleValue::at(1), SimpleValue::at(1), SimpleValue::at(1) })); + QCOMPARE(copy, QList({ SimpleValue::at(2) })); + } +} + +void tst_QList::fillDetachInt() const +{ + fillDetach(); +} + +void tst_QList::fillDetachMovable() const +{ + const int instancesCount = Movable::counter.loadAcquire(); + fillDetach(); + QCOMPARE(instancesCount, Movable::counter.loadAcquire()); +} + +void tst_QList::fillDetachCustom() const +{ + const int instancesCount = Custom::counter.loadAcquire(); + fillDetach(); + QCOMPARE(instancesCount, Custom::counter.loadAcquire()); } void tst_QList::first() const -- cgit v1.2.3