From b800f3039a754f67466df5e195e70ea2821f9404 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Mon, 18 Nov 2019 08:44:40 +0100 Subject: Cleanup qUncompress Cleanup the code in qUncompress and make use of QArrayDataPointer to keep track of memory. Change-Id: I2c11f0468813698d2b7c25acd0f8786a289739a0 Reviewed-by: Thiago Macieira --- src/corelib/text/qbytearray.cpp | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index 4d86205488..f8c2759507 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -692,14 +692,6 @@ QByteArray qCompress(const uchar* data, int nbytes, int compressionLevel) */ #ifndef QT_NO_COMPRESS -namespace { -struct QByteArrayDataDeleter -{ - static inline void cleanup(QTypedArrayData *d) - { if (d) QTypedArrayData::deallocate(d); } -}; -} - static QByteArray invalidCompressedData() { qWarning("qUncompress: Input data is corrupted"); @@ -733,24 +725,22 @@ QByteArray qUncompress(const uchar* data, int nbytes) return invalidCompressedData(); } - QPair pair = QByteArray::Data::allocate(expectedSize + 1); - QScopedPointer d(pair.first); + QByteArray::DataPointer d(QByteArray::Data::allocate(expectedSize + 1)); if (Q_UNLIKELY(d.data() == nullptr)) return invalidCompressedData(); forever { ulong alloc = len; - - int res = ::uncompress((uchar*)pair.second, &len, + int res = ::uncompress((uchar*)d.data(), &len, data+4, nbytes-4); switch (res) { case Z_OK: { Q_ASSERT(len <= alloc); - Q_UNUSED(alloc); - QByteArray::DataPointer dataPtr = { d.take(), pair.second, uint(len) }; - pair.second[len] = '\0'; - return QByteArray(dataPtr); + Q_UNUSED(alloc) + d.data()[len] = '\0'; + d.size = len; + return QByteArray(d); } case Z_MEM_ERROR: @@ -764,12 +754,9 @@ QByteArray qUncompress(const uchar* data, int nbytes) return invalidCompressedData(); } else { // grow the block - pair = QByteArray::Data::reallocateUnaligned(d.data(), pair.second, len + 1); - Q_CHECK_PTR(pair.first); - if (Q_UNLIKELY(pair.first == nullptr)) + d->reallocate(d->allocatedCapacity()*2, QByteArray::Data::GrowsForward); + if (Q_UNLIKELY(d.data() == nullptr)) return invalidCompressedData(); - d.take(); // don't free - d.reset(pair.first); } continue; -- cgit v1.2.3