From 8320a92141b736683bb3372f754375cb52836f5d Mon Sep 17 00:00:00 2001 From: Jani Heikkinen Date: Tue, 17 Aug 2021 07:51:25 +0300 Subject: Revert "QString::lastIndexOf: fix off-by-one for zero length matches" This reverts commit be83ff65c424cff1036e7da19d6175826d9f7ed9. The revert is needed as commit caused a regression in QString::lastIndexOf and the regression is more critical than the original issue fixed by the reverted commit. Fixes: QTBUG-94215 Pick-to: 6.1 Change-Id: I785c39d4e0e73f38d5447942357eff0eb19e3f96 Reviewed-by: Thiago Macieira --- src/corelib/text/qstring.cpp | 4 ++-- tests/auto/corelib/text/qstring/tst_qstring.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index e56de8fd51..a915a7e3bb 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -4366,13 +4366,13 @@ qsizetype QString::lastIndexOf(const QRegularExpression &re, qsizetype from, QRe return -1; } - qsizetype endpos = (from < 0) ? (size() + from + 1) : (from); + qsizetype endpos = (from < 0) ? (size() + from + 1) : (from + 1); QRegularExpressionMatchIterator iterator = re.globalMatch(*this); qsizetype lastIndex = -1; while (iterator.hasNext()) { QRegularExpressionMatch match = iterator.next(); qsizetype start = match.capturedStart(); - if (start <= endpos) { + if (start < endpos) { lastIndex = start; if (rmatch) *rmatch = std::move(match); diff --git a/tests/auto/corelib/text/qstring/tst_qstring.cpp b/tests/auto/corelib/text/qstring/tst_qstring.cpp index 4f9fa1ad0e..11e3b87abc 100644 --- a/tests/auto/corelib/text/qstring/tst_qstring.cpp +++ b/tests/auto/corelib/text/qstring/tst_qstring.cpp @@ -1669,7 +1669,7 @@ void tst_QString::lastIndexOf() QCOMPARE(haystack.lastIndexOf(needle.toLatin1().data(), from, cs), expected); #if QT_CONFIG(regularexpression) - if (from >= -1 && from < haystack.size() && needle.size() > 0) { + if (from >= -1 && from < haystack.size()) { // unfortunately, QString and QRegularExpression don't have the same out of bound semantics // I think QString is wrong -- See file log for contact information. { -- cgit v1.2.3