From f1540a2966ce911e9a5d5754e53f6026e3c26d22 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Mon, 24 Mar 2014 12:53:27 -0700 Subject: Fix capacity reservation for shared QByteArray MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We can squeeze, but not by discarding elements. Make sure the size of the object stays intact after changing the reserved capacity. I've also added unit tests for other containers, just to be sure. Task-number: QTBUG-37750 Change-Id: I5135b095943b7589423c51cebcb52af792468e61 Reviewed-by: Marc Mutz Reviewed-by: Jędrzej Nowacki --- src/corelib/tools/qbytearray.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/corelib/tools/qbytearray.h b/src/corelib/tools/qbytearray.h index b0a6971964..0a2f7a9e53 100644 --- a/src/corelib/tools/qbytearray.h +++ b/src/corelib/tools/qbytearray.h @@ -462,7 +462,7 @@ inline int QByteArray::capacity() const inline void QByteArray::reserve(int asize) { if (d->ref.isShared() || uint(asize) + 1u > d->alloc) { - reallocData(uint(asize) + 1u, d->detachFlags() | Data::CapacityReserved); + reallocData(qMax(uint(size()), uint(asize)) + 1u, d->detachFlags() | Data::CapacityReserved); } else { // cannot set unconditionally, since d could be the shared_null or // otherwise static -- cgit v1.2.3