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 <giuseppe.dangelo@kdab.com>
This commit is contained in:
Marc Mutz 2025-02-26 09:57:48 +00:00
parent fcada7c5c3
commit d1539331dd

View File

@ -3753,13 +3753,11 @@ static void replace_helper(QString &str, QSpan<size_t> indices, qsizetype blen,
const qsizetype oldSize = str.data_ptr().size; const qsizetype oldSize = str.data_ptr().size;
const qsizetype adjust = indices.size() * (after.size() - blen); const qsizetype adjust = indices.size() * (after.size() - blen);
const qsizetype newSize = oldSize + adjust; 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); replace_with_copy(str, indices, blen, after);
return; return;
} }
str.reserve(newSize);
if (QtPrivate::q_points_into_range(after.begin(), str)) if (QtPrivate::q_points_into_range(after.begin(), str))
// Copy after if it lies inside our own d.b area (which we could // Copy after if it lies inside our own d.b area (which we could
// possibly invalidate via a realloc or modify by replacement) // possibly invalidate via a realloc or modify by replacement)