diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2017-02-17 00:54:04 +0100 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2017-02-21 18:46:35 +0000 |
commit | cbdf40c4292aa4fc4038d4c12880b16c60b58d9e (patch) | |
tree | 331514d4be3eb8a593c02a235cadaeb88c864439 /src | |
parent | 152e927d0883df98eef622b9631a26cdec4fdf7e (diff) |
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>
(cherry picked from commit 0e3d6fe4f69955bf98e23a382caf5251e2b47ea0)
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/tools/qvarlengtharray.h | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/src/corelib/tools/qvarlengtharray.h b/src/corelib/tools/qvarlengtharray.h index d54121f452..1457b90af0 100644 --- a/src/corelib/tools/qvarlengtharray.h +++ b/src/corelib/tools/qvarlengtharray.h @@ -140,15 +140,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(qMove(copy)); + } else { + ptr[idx] = qMove(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; } |