From bcd456154cd99ec606f9572479c55f34137cbba0 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 19 Feb 2025 13:48:37 +0100 Subject: [PATCH] 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 Reviewed-by: Ahmad Samir (cherry picked from commit 4308ab70cafa4398aa72dfe63bc580a6ecff9e52) Reviewed-by: Qt Cherry-pick Bot (cherry picked from commit 646bea85a5f82b73755f6f1d743b65f90a224d66) --- src/corelib/text/qbytearray.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index a6c3acc01ff..950c1622064 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -2512,13 +2512,14 @@ QByteArray &QByteArray::replace(QByteArrayView before, QByteArrayView after) return *this; // protect against before or after being part of this + QVarLengthArray 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);