From e34ee8e88a825fe305f582be7d78cca0f3af33cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Nordheim?= Date: Mon, 16 Oct 2023 17:05:58 +0200 Subject: [PATCH] QLocal8Bit::convertFromUnicode[win] use local array for buffer To match convertToUnicode, we use a local array as a temporary buffer, then if any growth is needed we work directly with a QBA. As a drive-by: explicitly cast to int where we pass int Pick-to: 6.6 6.5 Task-number: QTBUG-105105 Task-number: QTBUG-118185 Change-Id: I1efff318eea41d87d558599d737b64107af4ae17 Reviewed-by: Thiago Macieira Reviewed-by: Edward Welbourne --- src/corelib/text/qstringconverter.cpp | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/corelib/text/qstringconverter.cpp b/src/corelib/text/qstringconverter.cpp index a75d2562ef..437569ba8e 100644 --- a/src/corelib/text/qstringconverter.cpp +++ b/src/corelib/text/qstringconverter.cpp @@ -1396,14 +1396,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). @@ -1416,7 +1424,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