qUncompress: use existing MaxByteArraySize

... instead of rolling your own.

Since MaxByteArraySize already adjusts for the trailing NUL byte in
QByteArray, the odd 'len >= max' now become a more natural 'len >
max'.

Rename the limit variable to MaxDecompressedSize, which is more
specific, and remove comments that now look out of place.

As a drive-by, re-arrange an else branch that, had it stayed, would
have required an adjusted if branch to require braces around a
single-line statement.

Task-number: QTBUG-104972
Change-Id: I6805dab8391b7e51db30d99b1b8968434062d12d
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
(cherry picked from commit b84f4be6b78feec617b8d97b00a8231c15d83f67)
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Marc Mutz 2022-09-07 16:47:49 +02:00
parent 4242f57900
commit 1ace2d21f9

View File

@ -632,11 +632,9 @@ QByteArray qUncompress(const uchar* data, qsizetype nbytes)
size_t expectedSize = size_t((data[0] << 24) | (data[1] << 16) | size_t expectedSize = size_t((data[0] << 24) | (data[1] << 16) |
(data[2] << 8) | (data[3] )); (data[2] << 8) | (data[3] ));
size_t len = qMax(expectedSize, 1ul); size_t len = qMax(expectedSize, 1ul);
constexpr size_t maxPossibleSize = MaxAllocSize - sizeof(QByteArray::Data); constexpr size_t MaxDecompressedSize = size_t(MaxByteArraySize);
if (len >= maxPossibleSize) { if (len > MaxDecompressedSize)
// QByteArray does not support that huge size anyway.
return invalidCompressedData(); return invalidCompressedData();
}
Q_ASSERT(len <= size_t((std::numeric_limits<qsizetype>::max)())); Q_ASSERT(len <= size_t((std::numeric_limits<qsizetype>::max)()));
QByteArray::DataPointer d(QByteArray::Data::allocate(qsizetype(len))); QByteArray::DataPointer d(QByteArray::Data::allocate(qsizetype(len)));
@ -662,18 +660,16 @@ QByteArray qUncompress(const uchar* data, qsizetype nbytes)
return QByteArray(); return QByteArray();
case Z_BUF_ERROR: case Z_BUF_ERROR:
static_assert(maxPossibleSize <= (std::numeric_limits<decltype(len)>::max)() / 2, static_assert(MaxDecompressedSize <= (std::numeric_limits<decltype(len)>::max)() / 2,
"oops, next line may overflow"); "oops, next line may overflow");
len *= 2; len *= 2;
if (len >= maxPossibleSize) { if (len > MaxDecompressedSize)
// QByteArray does not support that huge size anyway.
return invalidCompressedData(); return invalidCompressedData();
} else {
// grow the block d->reallocate(d->allocatedCapacity() * 2, QArrayData::Grow);
d->reallocate(d->allocatedCapacity()*2, QArrayData::Grow); if (d.data() == nullptr) // reallocation failed
if (d.data() == nullptr) // reallocation failed return invalidCompressedData();
return invalidCompressedData();
}
continue; continue;
case Z_DATA_ERROR: case Z_DATA_ERROR: