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.6 6.5
Task-number: QTBUG-105105
Change-Id: I28c367eb965339ae84355c0cac27c5d0352d9271
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Mårten Nordheim 2023-10-30 17:34:30 +01:00
parent 10f5e4f809
commit 6f48233482
1 changed files with 18 additions and 14 deletions

View File

@ -1359,22 +1359,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;
}
@ -1439,6 +1441,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();
@ -1458,12 +1461,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;
}