diff --git a/src/corelib/text/qstringconverter.cpp b/src/corelib/text/qstringconverter.cpp index 1c51cf80c20..b17432c067a 100644 --- a/src/corelib/text/qstringconverter.cpp +++ b/src/corelib/text/qstringconverter.cpp @@ -2710,11 +2710,19 @@ QStringDecoder QStringDecoder::decoderForHtml(QByteArrayView data) #endif // !QT_BOOTSTRAPPED /*! - Returns the canonical name for encoding \a e. + Returns the canonical name for encoding \a e or \nullptr if \a e is an + invalid value. + + \note In Qt versions prior to 6.10, 6.9.1, 6.8.4 or 6.5.9, calling this + function with an invalid argument resulted in undefined behavior. Since the + above-mentioned Qt versions, it returns nullptr instead. */ -const char *QStringConverter::nameForEncoding(QStringConverter::Encoding e) +const char *QStringConverter::nameForEncoding(QStringConverter::Encoding e) noexcept { - return encodingInterfaces[int(e)].name; + auto i = size_t(e); + if (Q_UNLIKELY(i >= std::size(encodingInterfaces))) + return nullptr; + return encodingInterfaces[i].name; } /*! diff --git a/src/corelib/text/qstringconverter_base.h b/src/corelib/text/qstringconverter_base.h index 383eaa8a869..26ea9dfe0d7 100644 --- a/src/corelib/text/qstringconverter_base.h +++ b/src/corelib/text/qstringconverter_base.h @@ -162,7 +162,7 @@ public: Q_CORE_EXPORT static std::optional encodingForName(const char *name) noexcept; #endif Q_CORE_EXPORT static std::optional encodingForName(QAnyStringView name) noexcept; - Q_CORE_EXPORT static const char *nameForEncoding(Encoding e); + Q_DECL_PURE_FUNCTION Q_CORE_EXPORT static const char *nameForEncoding(Encoding e) noexcept; Q_CORE_EXPORT static std::optional encodingForData(QByteArrayView data, char16_t expectedFirstCharacter = 0) noexcept; Q_CORE_EXPORT static std::optional encodingForHtml(QByteArrayView data);