summaryrefslogtreecommitdiffstats
path: root/src/corelib/text
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2024-02-13 14:58:18 -0800
committerThiago Macieira <thiago.macieira@intel.com>2024-03-06 02:09:16 -0800
commit1560e0161af70b5cf88a70e55c0b502612d433cd (patch)
treeecd52a0553168617664859e596bbb9708b437c6f /src/corelib/text
parenta6a56814702612d8981f594a6158d70a7928cb99 (diff)
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 <ivan.solovev@qt.io>
Diffstat (limited to 'src/corelib/text')
-rw-r--r--src/corelib/text/qstring.cpp22
1 files changed, 15 insertions, 7 deletions
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 <typename Char2>
-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<decltype(a), decltype(b)>) {
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<false> lhs, QStringView rhs) noexcept
@@ -1434,7 +1435,14 @@ bool QtPrivate::equalStrings(QBasicUtf8StringView<false> lhs, QLatin1StringView
bool QtPrivate::equalStrings(QBasicUtf8StringView<false> lhs, QBasicUtf8StringView<false> 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