diff options
Diffstat (limited to 'src/corelib/serialization/qdatastream.cpp')
-rw-r--r-- | src/corelib/serialization/qdatastream.cpp | 22 |
1 files changed, 9 insertions, 13 deletions
diff --git a/src/corelib/serialization/qdatastream.cpp b/src/corelib/serialization/qdatastream.cpp index d35e871de0..329be4a294 100644 --- a/src/corelib/serialization/qdatastream.cpp +++ b/src/corelib/serialization/qdatastream.cpp @@ -1088,26 +1088,22 @@ QDataStream &QDataStream::readBytes(char *&s, qint64 &l) qsizetype step = (dev->bytesAvailable() >= len) ? len : 1024 * 1024; qsizetype allocated = 0; - char *prevBuf = nullptr; - char *curBuf = nullptr; + std::unique_ptr<char[]> curBuf = nullptr; + constexpr qsizetype StepIncreaseThreshold = std::numeric_limits<qsizetype>::max() / 2; do { qsizetype blockSize = qMin(step, len - allocated); - prevBuf = curBuf; - curBuf = new char[allocated + blockSize + 1]; - if (prevBuf) { - memcpy(curBuf, prevBuf, allocated); - delete [] prevBuf; - } - if (readBlock(curBuf + allocated, blockSize) != blockSize) { - delete [] curBuf; + const qsizetype n = allocated + blockSize + 1; + if (const auto prevBuf = std::exchange(curBuf, std::make_unique<char[]>(n))) + memcpy(curBuf.get(), prevBuf.get(), allocated); + if (readBlock(curBuf.get() + allocated, blockSize) != blockSize) return *this; - } allocated += blockSize; - step *= 2; + if (step <= StepIncreaseThreshold) + step *= 2; } while (allocated < len); - s = curBuf; + s = curBuf.release(); s[len] = '\0'; l = len; return *this; |