From d1539331dd813c83916a323f6cdc7fd6aacb0043 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 26 Feb 2025 09:57:48 +0000 Subject: [PATCH] Revert "QString::replace: fix a performance regression" This reverts commit 315210de916d060c044c01e53ff249d676122b1b. The change was not correct. If the newly-inserted reserve() call actually grows the string, and `after` aliased a part of the old string, it will now reference deleted data, as the q_points_into_range check now comes too late. Change-Id: I2e016b8b90f74126dfc7126800b7b7fde96a091e Pick-to: 6.9 6.8 6.7 Task-number: QTBUG-127549 Reviewed-by: Giuseppe D'Angelo --- src/corelib/text/qstring.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index de9560f121e..a160b81c006 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -3753,13 +3753,11 @@ static void replace_helper(QString &str, QSpan indices, qsizetype blen, const qsizetype oldSize = str.data_ptr().size; const qsizetype adjust = indices.size() * (after.size() - blen); const qsizetype newSize = oldSize + adjust; - if (str.data_ptr().needsDetach()) { + if (str.data_ptr().needsDetach() || needsReallocate(str, newSize)) { replace_with_copy(str, indices, blen, after); return; } - str.reserve(newSize); - if (QtPrivate::q_points_into_range(after.begin(), str)) // Copy after if it lies inside our own d.b area (which we could // possibly invalidate via a realloc or modify by replacement)