diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2022-01-10 16:10:19 -0800 |
---|---|---|
committer | Marc Mutz <marc.mutz@qt.io> | 2022-01-17 22:17:01 +0000 |
commit | e52d50a03da29e2dddaee551e4409f28c7ed56f2 (patch) | |
tree | 297aaace3a0b17b236e3decd8d8d30e4fc8bd348 /src | |
parent | 238e3beb6f5af61159bf7e0776402f1a4c128484 (diff) |
QLatin1String: perform the comparison to another QL1S using memcmp()
qstrncmp() would stop at the first null character, which isn't correct.
The tests that had been disabled in tst_qstring.cpp (with an inaccurate
comment) were actually passing. I've added one more to ensure that the
terminating null is compared where needed.
[ChangeLog][QtCore][QLatin1String and QUtf8StringView] Fixed a
couple of bugs where two QLatin1Strings or two QUtf8StringViews
would stop their comparisons at the first embedded null
character, instead of comparing the full string. This issue
affected both classes' relational operators (less than, greater
than, etc.) and QUtf8StringView's operator== and operator!=.
Pick-to: 5.15 6.2 6.3
Change-Id: I0e5f6bec596a4a78bd3bfffd16c90ecea71ea68e
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/text/qstring.cpp | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp index 28027b158a..9742f5ad83 100644 --- a/src/corelib/text/qstring.cpp +++ b/src/corelib/text/qstring.cpp @@ -1444,7 +1444,7 @@ bool QtPrivate::equalStrings(QLatin1String lhs, QStringView rhs) noexcept bool QtPrivate::equalStrings(QLatin1String lhs, QLatin1String rhs) noexcept { - return lhs.size() == rhs.size() && (!lhs.size() || qstrncmp(lhs.data(), rhs.data(), lhs.size()) == 0); + return lhs.size() == rhs.size() && (!lhs.size() || memcmp(lhs.data(), rhs.data(), lhs.size()) == 0); } bool QtPrivate::equalStrings(QBasicUtf8StringView<false> lhs, QStringView rhs) noexcept @@ -1470,7 +1470,7 @@ bool QtPrivate::equalStrings(QBasicUtf8StringView<false> lhs, QLatin1String rhs) bool QtPrivate::equalStrings(QBasicUtf8StringView<false> lhs, QBasicUtf8StringView<false> rhs) noexcept { - return lhs.size() == rhs.size() && (!lhs.size() || qstrncmp(lhs.data(), rhs.data(), lhs.size()) == 0); + return lhs.size() == rhs.size() && (!lhs.size() || memcmp(lhs.data(), rhs.data(), lhs.size()) == 0); } bool QAnyStringView::equal(QAnyStringView lhs, QAnyStringView rhs) noexcept @@ -1577,7 +1577,7 @@ int QtPrivate::compareStrings(QLatin1String lhs, QLatin1String rhs, Qt::CaseSens if (cs == Qt::CaseInsensitive) return latin1nicmp(lhs.data(), lhs.size(), rhs.data(), rhs.size()); const auto l = std::min(lhs.size(), rhs.size()); - int r = qstrncmp(lhs.data(), rhs.data(), l); + int r = memcmp(lhs.data(), rhs.data(), l); return r ? r : lencmp(lhs.size(), rhs.size()); } @@ -1629,7 +1629,7 @@ int QtPrivate::compareStrings(QBasicUtf8StringView<false> lhs, QBasicUtf8StringV if (cs == Qt::CaseInsensitive) return compareStrings(lhs.toString(), rhs.toString(), cs); // ### optimize! const auto l = std::min(lhs.size(), rhs.size()); - int r = qstrncmp(lhs.data(), rhs.data(), l); + int r = memcmp(lhs.data(), rhs.data(), l); return r ? r : lencmp(lhs.size(), rhs.size()); } |