diff options
author | MÃ¥rten Nordheim <marten.nordheim@qt.io> | 2023-10-30 17:34:30 +0100 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2023-11-09 13:35:17 +0000 |
commit | f9cc1e4aae2c0b417b688454206c59add51ceb28 (patch) | |
tree | 8fb2c4d9b3c75f5fca6c88290eb715e2ede97f2a | |
parent | 8296051ae73cbc35823dced6d628b2ae36ba8825 (diff) |
QLocal8Bit::convert*Unicode[win]: Converge logic
I ended up writing different logic for similar things.
And using points_into_range doesn't work if we, by coincidence,
point at end, though this shouldn't be possible yet, but it may happen
once we support input larger than 2Gi. So, let's instead check if the
destination buffer has been initialized.
Pick-to: 6.5
Task-number: QTBUG-105105
Change-Id: I28c367eb965339ae84355c0cac27c5d0352d9271
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit 6f4823348220780a2e926c7885f2249f89f7e16d)
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r-- | src/corelib/text/qstringconverter.cpp | 34 |
1 files changed, 19 insertions, 15 deletions
diff --git a/src/corelib/text/qstringconverter.cpp b/src/corelib/text/qstringconverter.cpp index 0d0a885d70..d3834d8c94 100644 --- a/src/corelib/text/qstringconverter.cpp +++ b/src/corelib/text/qstringconverter.cpp @@ -1358,22 +1358,24 @@ QString QLocal8Bit::convertToUnicode_sys(QByteArrayView in, quint32 codePage, break; } } - - if (QtPrivate::q_points_into_range(out, buf.data(), buf.data() + buf.size())) { - if (out - buf.data() + len > 0) - sp = QStringView(buf.data(), out + len).toString(); + out += len; + if (len) + mblen = 0; + + if (sp.isEmpty()) { + // We must have only used the stack buffer + if (out != buf.data()) // else: we return null-string + sp = QStringView(buf.data(), out).toString(); } else{ - sp.truncate(out - reinterpret_cast<wchar_t *>(sp.data()) + len); + const auto begin = reinterpret_cast<wchar_t *>(sp.data()); + sp.truncate(std::distance(begin, out)); } if (sp.size() && sp.back().isNull()) sp.chop(1); - if (!state && mblen != length) { // We have trailing characters that should be converted - qsizetype diff = length - mblen; - sp.resize(sp.size() + diff, QChar::ReplacementCharacter); - } - + if (!state && mblen > 0) // We have trailing characters that should be converted + sp.resize(sp.size() + mblen, QChar::ReplacementCharacter); return sp; } @@ -1438,6 +1440,7 @@ QByteArray QLocal8Bit::convertFromUnicode_sys(QStringView in, quint32 codePage, nullptr))) { int r = GetLastError(); if (r == ERROR_INSUFFICIENT_BUFFER) { + Q_ASSERT(mb.isEmpty()); int neededLength = WideCharToMultiByte(codePage, 0, ch, int(uclen), nullptr, 0, nullptr, nullptr); const qsizetype currentLength = out - buf.data(); @@ -1457,12 +1460,13 @@ QByteArray QLocal8Bit::convertFromUnicode_sys(QStringView in, quint32 codePage, break; } } - auto end = out + len; - if (QtPrivate::q_points_into_range(out, buf.data(), buf.data() + buf.size())) { - if (end != buf.data()) // else: we return null-array - mb = QByteArrayView(buf.data(), end).toByteArray(); + out += len; + if (mb.isEmpty()) { + // We must have only used the stack buffer + if (out != buf.data()) // else: we return null-array + mb = QByteArrayView(buf.data(), out).toByteArray(); } else { - mb.truncate(end - mb.data()); + mb.truncate(std::distance(mb.data(), out)); } return mb; } |