QBuffer: optimize seek()-past-end-of-buffer

Use new QByteArray::resize(n, ch) method to resize the buffer
directly, instead of first allocating a QByteArray full of NULs and
then using public write() API to append it.

Change-Id: Ibdb082b970de0ba24534a570ecb23304c5f1470c
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@gmx.de>
This commit is contained in:
Marc Mutz 2022-04-04 16:49:03 +02:00
parent a00a1d8806
commit 787d178b19

View File

@ -363,16 +363,15 @@ bool QBuffer::seek(qint64 pos)
const auto oldBufSize = d->buf->size();
constexpr qint64 MaxSeekPos = (std::numeric_limits<decltype(oldBufSize)>::max)();
if (pos <= MaxSeekPos && pos > oldBufSize && isWritable()) {
if (seek(d->buf->size())) {
const qint64 gapSize = pos - d->buf->size();
if (write(QByteArray(gapSize, 0)) != gapSize) {
qWarning("QBuffer::seek: Unable to fill gap");
return false;
}
} else {
QT_TRY {
d->buf->resize(qsizetype(pos), '\0');
} QT_CATCH(const std::bad_alloc &) {} // swallow, failure case is handled below
if (d->buf->size() != pos) {
qWarning("QBuffer::seek: Unable to fill gap");
return false;
}
} else if (pos > d->buf->size() || pos < 0) {
}
if (pos > d->buf->size() || pos < 0) {
qWarning("QBuffer::seek: Invalid pos: %lld", pos);
return false;
}