From 9f5a687ffebbc93ce5f8f25c544aa9129c6366c3 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Fri, 17 Feb 2023 07:35:23 +0100 Subject: [PATCH] qt_inIsoNametoLCID: protect against a nullptr name The only user of the function, QCollatorPrivate::init(), passes QLocalePrivate::bcp47Name().constData(). bcp47Name() may return a default-constructed QByteArray (e.g. for QLocale::AnyLanguage), so constData() may be nullptr (QT5_NULL_STRINGS != 1). Passing nullptr to strncmp() or strncpy() is UB, though. Instead of using the nullptr-hardened q... versions of these functions, check name for nullptr once, at the top of the function, and avoid all the lookup code that follows and is known to fail (because windows_to_iso_list does not contain empty entries). This way, we take advantage of the std functions' UB for performance reasons (fewer repeated nullptr checks), instead of being taken advantage of. Qt 5 is not affected, as constData() never returns nullptr there. Pick-to: 6.5 6.4 6.2 Change-Id: I980dace2bca1e983ac526e89fadeb92239ab5f11 Reviewed-by: Thiago Macieira --- src/corelib/text/qlocale_win.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/corelib/text/qlocale_win.cpp b/src/corelib/text/qlocale_win.cpp index 550f7dd464..43324fbfb6 100644 --- a/src/corelib/text/qlocale_win.cpp +++ b/src/corelib/text/qlocale_win.cpp @@ -1038,6 +1038,8 @@ static const char *winLangCodeToIsoName(int code) LCID qt_inIsoNametoLCID(const char *name) { + if (!name) + return LOCALE_USER_DEFAULT; // handle norwegian manually, the list above will fail if (!strncmp(name, "nb", 2)) return 0x0414;