From b655734965155146290f3f3a9205243af11e42fb Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 13 May 2020 20:11:35 +0200 Subject: QString: fix an aliasing issue in remove(QString) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Even in Qt 5, remove() can be passed an alias to *this. In Qt 6, with the advent of substring sharing, this will become even more pronounced. Use the same fix as was already used in QString::insert(). Pick-to: 5.15 Change-Id: I1a0d3d99fd7dff6e727661646d2cbfdc94df2682 Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Edward Welbourne --- src/corelib/text/qstring.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index fc1a9f2a83..631b279332 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -2883,7 +2883,15 @@ static void removeStringImpl(QString &s, const T &needle, Qt::CaseSensitivity cs */ QString &QString::remove(const QString &str, Qt::CaseSensitivity cs) { - removeStringImpl(*this, str, cs); + const auto s = reinterpret_cast(str.data()); + const std::less less; + if (!less(s, d.data()) && less(s, d.data() + d.size)) { + // Part of me - take a copy + const QVarLengthArray copy(s, s + str.size()); + removeStringImpl(*this, QStringView{copy.data(), copy.size()}, cs); + } else { + removeStringImpl(*this, qToStringViewIgnoringNull(str), cs); + } return *this; } -- cgit v1.2.3