summaryrefslogtreecommitdiffstats
path: root/src/corelib/text/qstringconverter.cpp
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@qt.io>2023-10-17 22:43:08 +0200
committerMarc Mutz <marc.mutz@qt.io>2023-11-10 19:24:33 +0200
commitb113b01a711752d3add6c9df12d84438607de5b9 (patch)
tree49c663a87454138c666328455dab1c79b2c963bd /src/corelib/text/qstringconverter.cpp
parent8753bb3045d020bcae33eed67a2770a4709ac7b8 (diff)
QStringConverter: harden encodingForName() against nullptr
The nameMatch() function has an implicit precondition that neither argument is nullptr: it immediately dereferences both arguments. Prevent the crash by checking for name == nullptr early, before passing to nameMatch(). Add tests for null and empty. As a drive-by, make variables in the test const (needed for the QByteArray to avoid detaching, peer pressure for the others). Amends a639bcda1e42f48fa32885ede77f9fd320ce731c. Pick-to: 6.6 6.5 6.2 Change-Id: I4a30f6c130310eb701ba7c7251168294489c34db Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Diffstat (limited to 'src/corelib/text/qstringconverter.cpp')
-rw-r--r--src/corelib/text/qstringconverter.cpp2
1 files changed, 2 insertions, 0 deletions
diff --git a/src/corelib/text/qstringconverter.cpp b/src/corelib/text/qstringconverter.cpp
index 9ac290b0c1..f27283a06d 100644
--- a/src/corelib/text/qstringconverter.cpp
+++ b/src/corelib/text/qstringconverter.cpp
@@ -2096,6 +2096,8 @@ const char *QStringConverter::name() const noexcept
*/
std::optional<QStringConverter::Encoding> QStringConverter::encodingForName(const char *name) noexcept
{
+ if (!name)
+ return std::nullopt;
for (qsizetype i = 0; i < LastEncoding + 1; ++i) {
if (nameMatch(encodingInterfaces[i].name, name))
return QStringConverter::Encoding(i);