diff --git a/src/corelib/text/qstringconverter.cpp b/src/corelib/text/qstringconverter.cpp index 137db8662c5..8000a699ce9 100644 --- a/src/corelib/text/qstringconverter.cpp +++ b/src/corelib/text/qstringconverter.cpp @@ -1395,14 +1395,22 @@ QByteArray QLocal8Bit::convertFromUnicode_sys(QStringView in, quint32 codePage, return QByteArray(); if (uclen == 0) return QByteArray(""); - QByteArray mb(4096, 0); + + std::array buf; + char *out = buf.data(); + qsizetype outlen = buf.size(); + QByteArray mb; + int len; - while (!(len = WideCharToMultiByte(codePage, 0, ch, uclen, mb.data(), mb.size() - 1, nullptr, + while (!(len = WideCharToMultiByte(codePage, 0, ch, int(uclen), out, int(outlen), nullptr, nullptr))) { int r = GetLastError(); if (r == ERROR_INSUFFICIENT_BUFFER) { - mb.resize(1 - + WideCharToMultiByte(codePage, 0, ch, uclen, nullptr, 0, nullptr, nullptr)); + int neededLength = WideCharToMultiByte(codePage, 0, ch, int(uclen), nullptr, 0, nullptr, + nullptr); + mb.resize(neededLength); + out = mb.data(); + outlen = neededLength; // and try again... } else { // Fail. Probably can't happen in fact (dwFlags is 0). @@ -1415,7 +1423,12 @@ QByteArray QLocal8Bit::convertFromUnicode_sys(QStringView in, quint32 codePage, break; } } - mb.resize(len); + if (!len) + return QByteArray(); + if (out == buf.data()) + mb = QByteArray(buf.data(), len); + else + mb.resize(len); return mb; } #endif