From b98b7de0da395be33e2d1646ac62075422ce4343 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Nordheim?= Date: Tue, 1 Sep 2020 09:07:09 +0200 Subject: [PATCH] QByteArray: Disregard space at front during ::reserve(...) Traditionally when calling reserve it's because you expect to append up to X amount of bytes. We should keep that behavior the same. With another patch still in the works current behavior caused an issue with QStringBuilder in QNAM, as mirrored in the testcase attached. Change-Id: I9792a8f158fc9235e3de48ac8b06ac2c10e7f3dc Reviewed-by: Andrei Golubev Reviewed-by: Lars Knoll --- src/corelib/text/qbytearray.h | 2 +- .../text/qstringbuilder/qstringbuilder1/stringbuilder.cpp | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/corelib/text/qbytearray.h b/src/corelib/text/qbytearray.h index 9374843a5b..61f381409f 100644 --- a/src/corelib/text/qbytearray.h +++ b/src/corelib/text/qbytearray.h @@ -489,7 +489,7 @@ inline qsizetype QByteArray::capacity() const inline void QByteArray::reserve(qsizetype asize) { - if (d->needsDetach() || asize > capacity()) { + if (d->needsDetach() || asize > capacity() - d->freeSpaceAtBegin()) { reallocData(qMax(size_t(size()), size_t(asize)) + 1u, d->detachFlags() | Data::CapacityReserved); } else { d->setFlag(Data::CapacityReserved); diff --git a/tests/auto/corelib/text/qstringbuilder/qstringbuilder1/stringbuilder.cpp b/tests/auto/corelib/text/qstringbuilder/qstringbuilder1/stringbuilder.cpp index b811c63036..e0a261efd0 100644 --- a/tests/auto/corelib/text/qstringbuilder/qstringbuilder1/stringbuilder.cpp +++ b/tests/auto/corelib/text/qstringbuilder/qstringbuilder1/stringbuilder.cpp @@ -344,6 +344,14 @@ void runScenario() QByteArray ba2 = withZero; ba2 += ba2 P withZero; QCOMPARE(ba2, QByteArray(withZero + withZero + withZero)); + + // With space allocated in front, mirroring what happens with QHttpMultiPart in QNAM + QByteArray byteArray; + byteArray.reserve(70); + byteArray.insert(0, "multipart/"); + byteArray.insert(byteArray.size(), "mixed"); + byteArray += "; boundary=\"" P QByteArray(30, 'o') P '"'; + QCOMPARE(byteArray, "multipart/mixed; boundary=\"oooooooooooooooooooooooooooooo\""); } }