QVarLengthArray: fix appending an already-contained item
Like the lvalue QVector::append() overload, when we reallocate, we need to take a copy of the function's argument because the reference will get stale upon reallocation. Add a test. [ChangeLog][QtCore][QVarLengthArray] Fixed a bug involving appending an item already in the container to the container again. Change-Id: I06eeed6cb383dd5924e47a302bb3d1666d04c8e8 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>bb10
parent
d9c3d2301d
commit
0e3d6fe4f6
|
|
@ -146,15 +146,25 @@ public:
|
|||
T value(int i, const T &defaultValue) const;
|
||||
|
||||
inline void append(const T &t) {
|
||||
if (s == a) // i.e. s != 0
|
||||
if (s == a) { // i.e. s != 0
|
||||
T copy(t);
|
||||
realloc(s, s<<1);
|
||||
const int idx = s++;
|
||||
if (QTypeInfo<T>::isComplex) {
|
||||
new (ptr + idx) T(t);
|
||||
const int idx = s++;
|
||||
if (QTypeInfo<T>::isComplex) {
|
||||
new (ptr + idx) T(std::move(copy));
|
||||
} else {
|
||||
ptr[idx] = std::move(copy);
|
||||
}
|
||||
} else {
|
||||
ptr[idx] = t;
|
||||
const int idx = s++;
|
||||
if (QTypeInfo<T>::isComplex) {
|
||||
new (ptr + idx) T(t);
|
||||
} else {
|
||||
ptr[idx] = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void append(const T *buf, int size);
|
||||
inline QVarLengthArray<T, Prealloc> &operator<<(const T &t)
|
||||
{ append(t); return *this; }
|
||||
|
|
|
|||
|
|
@ -77,8 +77,21 @@ struct Foo
|
|||
|
||||
void tst_QVarLengthArray::append()
|
||||
{
|
||||
QVarLengthArray<QString> v;
|
||||
v.append(QString("hello"));
|
||||
QVarLengthArray<QString, 2> v;
|
||||
v.append(QString("1"));
|
||||
v.append(v.front());
|
||||
QCOMPARE(v.capacity(), 2);
|
||||
// transition from prealloc to heap:
|
||||
v.append(v.front());
|
||||
QVERIFY(v.capacity() > 2);
|
||||
QCOMPARE(v.front(), v.back());
|
||||
while (v.size() < v.capacity())
|
||||
v.push_back(v[0]);
|
||||
QCOMPARE(v.back(), v.front());
|
||||
QCOMPARE(v.size(), v.capacity());
|
||||
// transition from heap to larger heap:
|
||||
v.push_back(v.front());
|
||||
QCOMPARE(v.back(), v.front());
|
||||
|
||||
QVarLengthArray<int> v2; // rocket!
|
||||
v2.append(5);
|
||||
|
|
|
|||
Loading…
Reference in New Issue