From a7c87be8883458d151e3fe7fa1a8511212af41d4 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Mon, 22 May 2023 09:10:58 +0200 Subject: [PATCH] QCoreApplicationPrivate::initLocale(): report correct encoding We override the old encoding because it wasn't UTF-8, then we use a fresh call to nl_langinfo(CODESET) when reporting the encoding that "is not UTF-8", except that we've just fixed that so it is now. Store the old encoding in a std::string before we change it, so that we can report what it was rather than what we changed it to. Amends commit 3690c202f959a505e0f0bcd4a7b19f235b04d015 Pick-to: 6.5 6.6 Task-number: QTBUG-113371 Change-Id: I5f7c3648890cb0abf1d4769af24715686762c176 Reviewed-by: Thiago Macieira --- src/corelib/kernel/qcoreapplication.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp index 36892b6f6c..52d0d2ac04 100644 --- a/src/corelib/kernel/qcoreapplication.cpp +++ b/src/corelib/kernel/qcoreapplication.cpp @@ -104,6 +104,7 @@ #include #include +#include QT_BEGIN_NAMESPACE @@ -622,8 +623,10 @@ void QCoreApplicationPrivate::initLocale() // Android 6 still lacks nl_langinfo(), so we can't check. // FIXME: Shouldn't we still setlocale("UTF-8")? # else - const char *charEncoding = nl_langinfo(CODESET); - if (Q_UNLIKELY(qstricmp(charEncoding, "UTF-8") != 0 && qstricmp(charEncoding, "utf8") != 0)) { + // std::string's SSO usually saves this the need to allocate: + const std::string oldEncoding = nl_langinfo(CODESET); + if (!Q_LIKELY(qstricmp(oldEncoding.data(), "UTF-8") == 0 + || qstricmp(oldEncoding.data(), "utf8") == 0)) { const QByteArray oldLocale = setlocale(LC_ALL, nullptr); QByteArray newLocale; bool warnOnOverride = true; @@ -658,14 +661,14 @@ void QCoreApplicationPrivate::initLocale() qWarning("Detected locale \"%s\" with character encoding \"%s\", which is not UTF-8.\n" "Qt depends on a UTF-8 locale, but has failed to switch to one.\n" "If this causes problems, reconfigure your locale. See the locale(1) manual\n" - "for more information.", oldLocale.constData(), nl_langinfo(CODESET)); + "for more information.", oldLocale.constData(), oldEncoding.data()); } else if (warnOnOverride) { // Let the user know we over-rode their configuration. qWarning("Detected locale \"%s\" with character encoding \"%s\", which is not UTF-8.\n" "Qt depends on a UTF-8 locale, and has switched to \"%s\" instead.\n" "If this causes problems, reconfigure your locale. See the locale(1) manual\n" "for more information.", - oldLocale.constData(), nl_langinfo(CODESET), newLocale.constData()); + oldLocale.constData(), oldEncoding.data(), newLocale.constData()); } } # endif // Platform choice