Make sure we don't try to ask QByteArray to allocate too much

QFile::readAll could be asked to read a file that is over 1 GB in size
and thus cause an assertion:

ASSERT failure in qAllocMore: "Requested size is too large!", ...

The idea behind the existing code was correct, but the value was wrong.
It prevented overflow of the integer size request, but didn't prevent
overflowing the storage size.

Change-Id: I072e6e419f47b639454f3fd96deb0f88d03e960c
Reviewed-by: Martin Smith <martin.smith@digia.com>
This commit is contained in:
Thiago Macieira 2014-12-16 14:24:55 -08:00
parent 80d0075588
commit c70658d301
3 changed files with 14 additions and 2 deletions

View File

@ -1025,7 +1025,7 @@ QByteArray QIODevice::readAll()
} else {
// Read it all in one go.
// If resize fails, don't read anything.
if (readBytes + theSize - d->pos > INT_MAX)
if (quint64(readBytes + theSize - d->pos) > QByteArray::MaxSize)
return QByteArray();
result.resize(int(readBytes + theSize - d->pos));
readBytes += read(result.data() + readBytes, result.size() - readBytes);

View File

@ -63,7 +63,7 @@ int qFindByteArray(
int qAllocMore(int alloc, int extra) Q_DECL_NOTHROW
{
Q_ASSERT(alloc >= 0 && extra >= 0);
Q_ASSERT_X(alloc < (1 << 30) - extra, "qAllocMore", "Requested size is too large!");
Q_ASSERT_X(uint(alloc) < QByteArray::MaxSize, "qAllocMore", "Requested size is too large!");
unsigned nalloc = qNextPowerOfTwo(alloc + extra);
@ -776,6 +776,15 @@ static inline char qToLower(char c)
\sa QString, QBitArray
*/
/*!
\variable QByteArray::MaxSize
\internal
\since 5.4
The maximum size of a QByteArray, in bytes. Also applies to a the maximum
storage size of QString and QVector, though not the number of elements.
*/
/*!
\enum QByteArray::Base64Option
\since 5.2

View File

@ -173,6 +173,9 @@ private:
typedef QTypedArrayData<char> Data;
public:
// undocumented:
static const quint64 MaxSize = (1 << 30) - sizeof(Data);
enum Base64Option {
Base64Encoding = 0,
Base64UrlEncoding = 1,