qUn/Compress: reject negative lengths

In qCompress, we've been calculating postive len values out of them,
only to fail at random points later, possibly running into UB. Fail
early instead.

In qUncompress, we've been catching negative values, and reported them
indiscriminately as "invalid data". Use a better warning message
instead.

By rights, nbytes ≥ 0 would be a precondition of both functions (which
we would Q_ASSERT() on), but seeing we're picking this back into LTS
branches, I found it prudent to use a non-fatal way to signal the
precondition violation.

If and when we keep these functions for Qt 7, it will be as an
overload that takes QByteArrayView, in which case nbytes ≥ 0 enters as
a hard precondition via the QByteArrayView constructor, so there
appears to be no need to pre-program a Q_ASSERT() for Qt 7.0.

Pick-to: 6.4 6.3 6.2
Task-number: QTBUG-104972
Task-number: QTBUG-106542
Change-Id: I6a1b25fe12d31e3d4c845033cad320832976f83c
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
bb10
Marc Mutz 2022-09-19 17:05:53 +02:00
parent 6472616e6c
commit bbd1f576f7
1 changed files with 12 additions and 0 deletions

View File

@ -558,6 +558,12 @@ static QByteArray dataIsNull(ZLibOp op)
return zlibError(op, "Data is null");
}
Q_DECL_COLD_FUNCTION
static QByteArray lengthIsNegative(ZLibOp op)
{
return zlibError(op, "Input length is negative");
}
Q_DECL_COLD_FUNCTION
static QByteArray tooMuchData(ZLibOp op)
{
@ -579,6 +585,9 @@ QByteArray qCompress(const uchar* data, qsizetype nbytes, int compressionLevel)
if (!data)
return dataIsNull(ZLibOp::Compression);
if (nbytes < 0)
return lengthIsNegative(ZLibOp::Compression);
if (compressionLevel < -1 || compressionLevel > 9)
compressionLevel = -1;
@ -657,6 +666,9 @@ QByteArray qUncompress(const uchar* data, qsizetype nbytes)
if (!data)
return dataIsNull(ZLibOp::Decompression);
if (nbytes < 0)
return lengthIsNegative(ZLibOp::Decompression);
constexpr qsizetype HeaderSize = sizeof(CompressSizeHint_t);
if (nbytes < HeaderSize)
return invalidCompressedData();