Fix regression causing QVector::fill w/ same size to not detach

Caused by commit 01301b0b34, which made
vector.resize(vector.size()) not to detach, which was used by fill() and
assumed that detaching happened. The test does not test the resize()
behavior, only that fill() is not broken anymore.

[ChangeLog][QtCore][QVector] Fixed a regression that caused fill() not
to detach, corrupting shared copies.

Fixes: QTBUG-77058
Change-Id: I6aed4df6a12e43c3ac8efffd15b1b527a8007bf3
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
bb10
Thiago Macieira 2019-07-15 15:28:47 -07:00
parent a837ec15fe
commit 8abbbb4c48
2 changed files with 12 additions and 1 deletions

View File

@ -416,7 +416,7 @@ template <typename T>
void QVector<T>::resize(int asize)
{
if (asize == d->size)
return;
return detach();
if (asize > int(d->alloc) || !isDetached()) { // there is not enough space
QArrayData::AllocationOptions opt = asize > int(d->alloc) ? QArrayData::Grow : QArrayData::Default;
realloc(qMax(int(d->alloc), asize), opt);

View File

@ -250,6 +250,7 @@ private slots:
void fillInt() const;
void fillMovable() const;
void fillCustom() const;
void fillDetaches() const;
void first() const;
void fromListInt() const;
void fromListMovable() const;
@ -1272,6 +1273,16 @@ void tst_QVector::fillCustom() const
QCOMPARE(instancesCount, Custom::counter.loadAcquire());
}
void tst_QVector::fillDetaches() const
{
QVector<int> test = { 1, 2, 3 };
QVector<int> copy = test;
copy.fill(42);
QCOMPARE(test, QVector<int>({1, 2, 3}));
QCOMPARE(copy, QVector<int>({42, 42, 42}));
}
void tst_QVector::first() const
{
QVector<int> myvec;