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 <edward.welbourne@qt.io>
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
This commit is contained in:
Thiago Macieira 2022-01-12 09:53:14 -08:00
parent 0c3a56b621
commit ed753eb5fb

View File

@ -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