diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 3078bb85179..ad529447fc3 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -3771,11 +3771,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)