QByteArray: don't recurse when needle or replacement alias *this

While the idea to delay the construction of the QVLAs until we know we
need them is elegant, we're doing a lot of checks twice or thrice this
way, so merely re-set the a, b pointers and otherwise continue
executing.

This requires us to default-construct the QVLAs before potentially
assign()ing to them, but that ought to be cheap, given the compiler
can see everything that's going on in QVLA.

Pick-to: 6.5
Change-Id: I4e42d5ab48711af91cc1785171524be5fb45ae2f
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Ahmad Samir <a.samirh78@gmail.com>
(cherry picked from commit 4308ab70cafa4398aa72dfe63bc580a6ecff9e52)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit 646bea85a5f82b73755f6f1d743b65f90a224d66)
This commit is contained in:
Marc Mutz 2025-02-19 13:48:37 +01:00 committed by Qt Cherry-pick Bot
parent 65f7bdd3ab
commit bcd456154c

View File

@ -2512,13 +2512,14 @@ QByteArray &QByteArray::replace(QByteArrayView before, QByteArrayView after)
return *this;
// protect against before or after being part of this
QVarLengthArray<char> pinnedNeedle, pinnedReplacement;
if (QtPrivate::q_points_into_range(a, d)) {
QVarLengthArray copy(a, a + asize);
return replace(before, QByteArrayView{copy});
pinnedReplacement.assign(a, a + asize);
a = pinnedReplacement.data();
}
if (QtPrivate::q_points_into_range(b, d)) {
QVarLengthArray copy(b, b + bsize);
return replace(QByteArrayView{copy}, after);
pinnedNeedle.assign(b, b + bsize);
b = pinnedNeedle.data();
}
QByteArrayMatcher matcher(b, bsize);