From 1560e0161af70b5cf88a70e55c0b502612d433cd Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Tue, 13 Feb 2024 14:58:18 -0800 Subject: QtPrivate::equalStrings(): enforce matching size where applicable Commit 8a7b74f6e71d6b8264f365148bf5a04db4038617 added these functions and already placed the checking for the size in the inline code. Aside from the operators==, the only other place that calls equalStrings() directly is QAnyStringView::equal() and that also checks the size for the non-UTF-8 string views do match. Unfortunately, the size checking for UTF-8 was only added in 52e0a91fbc187ca09d3ea6279e8bdce982edc586. This reverts commit e0eb93d9a2b104ac8d7596c419d1f13dc9db9b1b ("QLatin1StringView: delegate operator== to QByteArrayView") because that would compare the sizes again. Change-Id: I83dda2d36c904517b3c0fffd17b38e422b5e2c25 Reviewed-by: Ivan Solovev --- src/corelib/text/qstring.cpp | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'src/corelib/text') diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index b3fd7be4f0..38df9b6f4d 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -1348,10 +1348,8 @@ static int ucstrncmp(const char16_t *a, const char *b, size_t l) // Unicode case-sensitive equality template -static bool ucstreq(const char16_t *a, size_t alen, const Char2 *b, size_t blen) +static bool ucstreq(const char16_t *a, size_t alen, const Char2 *b) { - if (alen != blen) - return false; if constexpr (std::is_same_v) { if (a == b) return true; @@ -1394,12 +1392,14 @@ static int latin1nicmp(const char *lhsChar, qsizetype lSize, const char *rhsChar bool QtPrivate::equalStrings(QStringView lhs, QStringView rhs) noexcept { - return ucstreq(lhs.utf16(), lhs.size(), rhs.utf16(), rhs.size()); + Q_ASSERT(lhs.size() == rhs.size()); + return ucstreq(lhs.utf16(), lhs.size(), rhs.utf16()); } bool QtPrivate::equalStrings(QStringView lhs, QLatin1StringView rhs) noexcept { - return ucstreq(lhs.utf16(), lhs.size(), rhs.latin1(), rhs.size()); + Q_ASSERT(lhs.size() == rhs.size()); + return ucstreq(lhs.utf16(), lhs.size(), rhs.latin1()); } bool QtPrivate::equalStrings(QLatin1StringView lhs, QStringView rhs) noexcept @@ -1409,7 +1409,8 @@ bool QtPrivate::equalStrings(QLatin1StringView lhs, QStringView rhs) noexcept bool QtPrivate::equalStrings(QLatin1StringView lhs, QLatin1StringView rhs) noexcept { - return QByteArrayView(lhs) == QByteArrayView(rhs); + Q_ASSERT(lhs.size() == rhs.size()); + return (!lhs.size() || memcmp(lhs.data(), rhs.data(), lhs.size()) == 0); } bool QtPrivate::equalStrings(QBasicUtf8StringView lhs, QStringView rhs) noexcept @@ -1434,7 +1435,14 @@ bool QtPrivate::equalStrings(QBasicUtf8StringView lhs, QLatin1StringView bool QtPrivate::equalStrings(QBasicUtf8StringView lhs, QBasicUtf8StringView rhs) noexcept { - return lhs.size() == rhs.size() && (!lhs.size() || memcmp(lhs.data(), rhs.data(), lhs.size()) == 0); +#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) || defined(QT_BOOTSTRAPPED) || defined(QT_STATIC) + Q_ASSERT(lhs.size() == rhs.size()); +#else + // operator== didn't enforce size prior to Qt 6.2 + if (lhs.size() != rhs.size()) + return false; +#endif + return (!lhs.size() || memcmp(lhs.data(), rhs.data(), lhs.size()) == 0); } bool QAnyStringView::equal(QAnyStringView lhs, QAnyStringView rhs) noexcept -- cgit v1.2.3