From 7c149dd86944b3669420832b2b14fac00327d4b7 Mon Sep 17 00:00:00 2001 From: Alex Trotsenko Date: Sat, 28 Mar 2015 15:03:18 +0200 Subject: [PATCH] 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 --- src/corelib/io/qiodevice.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/corelib/io/qiodevice.cpp b/src/corelib/io/qiodevice.cpp index 7a87a78c600..43ad8d9316d 100644 --- a/src/corelib/io/qiodevice.cpp +++ b/src/corelib/io/qiodevice.cpp @@ -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);