From ed753eb5fbd29f323077ac7475fde66ec6a0a12f Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Wed, 12 Jan 2022 09:53:14 -0800 Subject: [PATCH] QString::compare: fix comparing a 4GB string to 1 QChar If the size() was larger than INT_MAX, the result would be wrong (and UB). Pick-to: 6.2 6.3 Change-Id: I0e5f6bec596a4a78bd3bfffd16c99763e66c2013 Reviewed-by: Edward Welbourne Reviewed-by: Marc Mutz --- src/corelib/text/qstring.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h index f53cb869c5..cdf710e5d9 100644 --- a/src/corelib/text/qstring.h +++ b/src/corelib/text/qstring.h @@ -115,7 +115,7 @@ public: [[nodiscard]] int compare(QLatin1String other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const noexcept { return QtPrivate::compareStrings(*this, other, cs); } [[nodiscard]] constexpr int compare(QChar c) const noexcept - { return isEmpty() || front() == c ? size() - 1 : uchar(m_data[0]) - c.unicode() ; } + { return isEmpty() ? -1 : front() == c ? int(size() > 1) : uchar(m_data[0]) - c.unicode(); } [[nodiscard]] int compare(QChar c, Qt::CaseSensitivity cs) const noexcept { return QtPrivate::compareStrings(*this, QStringView(&c, 1), cs); } [[nodiscard]] int compare(QLatin1Char c, Qt::CaseSensitivity cs) const noexcept