diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 77ffde84037..e579ffd8f58 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -3755,13 +3755,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)