From 80686be0862c3cc37e12db679e46640316ac8e78 Mon Sep 17 00:00:00 2001 From: Mikolaj Boc Date: Mon, 9 Jan 2023 16:08:52 +0100 Subject: [PATCH] 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 --- src/corelib/text/qcollator_posix.cpp | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/corelib/text/qcollator_posix.cpp b/src/corelib/text/qcollator_posix.cpp index 54c700015b7..5ed80c1b8ea 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))); }