Skip to content

Commit

Permalink
Cleanup qUncompress
Browse files Browse the repository at this point in the history
Cleanup the code in qUncompress and make use of QArrayDataPointer
to keep track of memory.

Change-Id: I2c11f0468813698d2b7c25acd0f8786a289739a0
Reviewed-by: Thiago Macieira <[email protected]>
  • Loading branch information
laknoll committed May 17, 2020
1 parent 88c72d9 commit b800f30
Showing 1 changed file with 8 additions and 21 deletions.
29 changes: 8 additions & 21 deletions src/corelib/text/qbytearray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<char> *d)
{ if (d) QTypedArrayData<char>::deallocate(d); }
};
}

static QByteArray invalidCompressedData()
{
qWarning("qUncompress: Input data is corrupted");
Expand Down Expand Up @@ -733,24 +725,22 @@ QByteArray qUncompress(const uchar* data, int nbytes)
return invalidCompressedData();
}

QPair<QByteArray::Data *, char *> pair = QByteArray::Data::allocate(expectedSize + 1);
QScopedPointer<QByteArray::Data, QByteArrayDataDeleter> 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:
Expand All @@ -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;

Expand Down

0 comments on commit b800f30

Please sign in to comment.