Skip to content

Commit 48959f7

Browse files
committed
QStringConverter: widen nameForEncoding()'s contract
Coverity apparently mixes bootstrap and non-bootstrap builds: it complains that Encoding::Latin1 indexes encodingInterfaces[] out-of-range (apparently taking the size of the latter from a bootstrapped and the value of the former from a non-bootstrap build). That should somehow be fixed in the Coverity configuration, but it highlighted the fact that we have a narrow-contract function in this security-critical class that can trivially have a wide contract, so widen the contract by returning nullptr for invalid Encoding values. Consequently, mark the function as noexcept. As a drive-by, mark it also as Q_DECL_PURE_FUNCTION. [ChangeLog][QtCore][QStringConverter] The nameForEncoding() function now returns nullptr for an invalid Encoding value. Before, such a call resulted in undefined behavior. Pick-to: 6.9 6.8 6.5 Coverity-Id: 480251 Change-Id: Ie1c5c9df6881147a1ff44fe549e50748b2f5b7da Reviewed-by: Thiago Macieira <[email protected]>
1 parent 0725530 commit 48959f7

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/corelib/text/qstringconverter.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2710,11 +2710,19 @@ QStringDecoder QStringDecoder::decoderForHtml(QByteArrayView data)
27102710
#endif // !QT_BOOTSTRAPPED
27112711

27122712
/*!
2713-
Returns the canonical name for encoding \a e.
2713+
Returns the canonical name for encoding \a e or \nullptr if \a e is an
2714+
invalid value.
2715+
2716+
\note In Qt versions prior to 6.10, 6.9.1, 6.8.4 or 6.5.9, calling this
2717+
function with an invalid argument resulted in undefined behavior. Since the
2718+
above-mentioned Qt versions, it returns nullptr instead.
27142719
*/
2715-
const char *QStringConverter::nameForEncoding(QStringConverter::Encoding e)
2720+
const char *QStringConverter::nameForEncoding(QStringConverter::Encoding e) noexcept
27162721
{
2717-
return encodingInterfaces[int(e)].name;
2722+
auto i = size_t(e);
2723+
if (Q_UNLIKELY(i >= std::size(encodingInterfaces)))
2724+
return nullptr;
2725+
return encodingInterfaces[i].name;
27182726
}
27192727

27202728
/*!

src/corelib/text/qstringconverter_base.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ class QStringConverter : public QStringConverterBase
162162
Q_CORE_EXPORT static std::optional<Encoding> encodingForName(const char *name) noexcept;
163163
#endif
164164
Q_CORE_EXPORT static std::optional<Encoding> encodingForName(QAnyStringView name) noexcept;
165-
Q_CORE_EXPORT static const char *nameForEncoding(Encoding e);
165+
Q_DECL_PURE_FUNCTION Q_CORE_EXPORT static const char *nameForEncoding(Encoding e) noexcept;
166166
Q_CORE_EXPORT static std::optional<Encoding>
167167
encodingForData(QByteArrayView data, char16_t expectedFirstCharacter = 0) noexcept;
168168
Q_CORE_EXPORT static std::optional<Encoding> encodingForHtml(QByteArrayView data);

0 commit comments

Comments
 (0)