Prevent memory overgrowth while reading from a sequential device

After flushing the internal buffer, QIODevice::readAll() attempts
to read the device incrementally. On each iteration, the result buffer
size is increased by a constant value independently from the number of
read bytes. This lead to unreasonable growth of the buffer if these
additional conditions were met:

 - readData() requests new data from the device on every call;
 - highly loaded device provides at least one byte on each request.

Instead of constant resizing, keep the size of free block to avoid a
possible memory exhaustion.

Task-number: QTBUG-44286
Change-Id: I637e2d0e05bd900a1bb9517af2fe7d8038c75a35
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@theqtcompany.com>
This commit is contained in:
Alex Trotsenko 2015-03-28 15:03:18 +02:00
parent db52d55fba
commit 7c149dd869

View File

@ -989,8 +989,8 @@ QByteArray QIODevice::readAll()
// Size is unknown, read incrementally.
qint64 readResult;
do {
result.resize(result.size() + QIODEVICE_BUFFERSIZE);
readResult = read(result.data() + readBytes, result.size() - readBytes);
result.resize(readBytes + QIODEVICE_BUFFERSIZE);
readResult = read(result.data() + readBytes, QIODEVICE_BUFFERSIZE);
if (readResult > 0 || readBytes == 0)
readBytes += readResult;
} while (readResult > 0);