summaryrefslogtreecommitdiffstats
path: root/src/corelib/text/qcollator_posix.cpp
diff options
context:
space:
mode:
authorMikolaj Boc <mikolaj.boc@qt.io>2023-01-09 16:08:52 +0100
committerMikolaj Boc <mikolaj.boc@qt.io>2023-01-12 14:32:28 +0100
commit80686be0862c3cc37e12db679e46640316ac8e78 (patch)
treee48f81ef65ba374e54638b9f3c6e5cf4d7734623 /src/corelib/text/qcollator_posix.cpp
parent696ad54e5d76dd063cbb02d0c288fdece6ee75d0 (diff)
Use the correct size argument for wcsxfrm
The documentation says that the size argument to wcsxfrm should include the null terminating character. Currently it doesn't, which breaks collation, as the last character is omitted. Task-number: QTBUG-109954 Change-Id: Ic0c78a617ed1d50e31e50cae56e21675d2069ead Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/text/qcollator_posix.cpp')
-rw-r--r--src/corelib/text/qcollator_posix.cpp18
1 files changed, 12 insertions, 6 deletions
diff --git a/src/corelib/text/qcollator_posix.cpp b/src/corelib/text/qcollator_posix.cpp
index 54c700015b..5ed80c1b8e 100644
--- a/src/corelib/text/qcollator_posix.cpp
+++ b/src/corelib/text/qcollator_posix.cpp
@@ -69,13 +69,19 @@ QCollatorSortKey QCollator::sortKey(const QString &string) const
if (d->isC()) {
std::copy(original.cbegin(), original.cend(), result.begin());
} else {
- size_t size = std::wcsxfrm(result.data(), original.constData(), string.size());
- if (size > size_t(result.size())) {
- result.resize(size+1);
- size = std::wcsxfrm(result.data(), original.constData(), string.size());
+ auto availableSizeIncludingNullTerminator = result.size();
+ size_t neededSizeExcludingNullTerminator = std::wcsxfrm(
+ result.data(), original.constData(), availableSizeIncludingNullTerminator);
+ if (neededSizeExcludingNullTerminator > size_t(availableSizeIncludingNullTerminator - 1)) {
+ result.resize(neededSizeExcludingNullTerminator + 1);
+ availableSizeIncludingNullTerminator = result.size();
+ neededSizeExcludingNullTerminator = std::wcsxfrm(result.data(), original.constData(),
+ availableSizeIncludingNullTerminator);
+ Q_ASSERT(neededSizeExcludingNullTerminator
+ == size_t(availableSizeIncludingNullTerminator - 1));
}
- result.resize(size+1);
- result[size] = 0;
+ result.resize(neededSizeExcludingNullTerminator + 1);
+ result[neededSizeExcludingNullTerminator] = 0;
}
return QCollatorSortKey(new QCollatorSortKeyPrivate(std::move(result)));
}