diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 8512d5c3e76..8067e25be0c 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -3740,11 +3740,13 @@ 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() || needsReallocate(str, newSize)) { + if (str.data_ptr().needsDetach()) { 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)