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>
This commit is contained in:
Mårten Nordheim 2023-10-30 17:34:30 +01:00 committed by Qt Cherry-pick Bot
parent 8296051ae7
commit f9cc1e4aae

View File

@ -1358,22 +1358,24 @@ QString QLocal8Bit::convertToUnicode_sys(QByteArrayView in, quint32 codePage,
break;
}
}
out += len;
if (len)
mblen = 0;
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();
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;
}