QLogging: fix potential missing NUL-terminator when calling OutputDebugString

The string returned from QStringView::utf16() is, in general, not
NUL-terminated, as OutputDebugString() requires. Though we only ever
call win_outputDebugString_helper() with an actual QString, it's not
100% clear said QString doesn't originate from, say, a QStringLiteral,
in which case QString::utf16() would have to (an does) detach() to
ensure the NUL-termination.

So, take by const QString&, but use QStringView::mid() to avoid
copying the substring content twice.

Amends a049325cc7.

Pick-to: 6.3 6.2 5.15
Change-Id: Ie42a6000c75c6a55d629621d89e0cf498b174d29
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
bb10
Marc Mutz 2021-12-15 09:07:17 +01:00
parent 2a013fec1c
commit 7ef6140170
1 changed files with 2 additions and 2 deletions

View File

@ -1671,7 +1671,7 @@ static bool android_default_message_handler(QtMsgType type,
#endif //Q_OS_ANDROID
#ifdef Q_OS_WIN
static void win_outputDebugString_helper(QStringView message)
static void win_outputDebugString_helper(const QString &message)
{
const qsizetype maxOutputStringLength = 32766;
static QBasicMutex m;
@ -1683,7 +1683,7 @@ static void win_outputDebugString_helper(QStringView message)
wchar_t *messagePart = new wchar_t[maxOutputStringLength + 1];
for (qsizetype i = 0; i < message.length(); i += maxOutputStringLength) {
const qsizetype length = qMin(message.length() - i, maxOutputStringLength);
const qsizetype len = message.mid(i, length).toWCharArray(messagePart);
const qsizetype len = QStringView{message}.mid(i, length).toWCharArray(messagePart);
Q_ASSERT(len == length);
messagePart[len] = 0;
OutputDebugString(messagePart);