From c46ee7df57c30c94107df8506d30d8872ffa3baa Mon Sep 17 00:00:00 2001 From: Fabian Kosmale Date: Wed, 25 May 2022 09:07:37 +0200 Subject: QStringConverter: Do not crash if invalid Attempting to use an invalid QStringConverter would so far have resulted in a crash, as we would dereference the null iface pointer. Fix this by inserting adequate checks, and ensure that hasError returns true if we attempt to en/decode with an invalid converter. Pick-to: 6.2 6.3 Change-Id: Icf74bb88cd8c95685481cc0bd512da99b62f33e6 Reviewed-by: Lars Knoll --- src/corelib/text/qstringconverter.h | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/corelib/text/qstringconverter.h b/src/corelib/text/qstringconverter.h index 213de9fe55..c1645f0938 100644 --- a/src/corelib/text/qstringconverter.h +++ b/src/corelib/text/qstringconverter.h @@ -62,12 +62,23 @@ public: #endif qsizetype requiredSpace(qsizetype inputLength) const - { return iface->fromUtf16Len(inputLength); } + { return iface ? iface->fromUtf16Len(inputLength) : 0; } char *appendToBuffer(char *out, QStringView in) - { return iface->fromUtf16(out, in, &state); } + { + if (!iface) { + state.invalidChars = 1; + return out; + } + return iface->fromUtf16(out, in, &state); + } private: QByteArray encodeAsByteArray(QStringView in) { + if (!iface) { + // ensure that hasError returns true + state.invalidChars = 1; + return {}; + } QByteArray result(iface->fromUtf16Len(in.size()), Qt::Uninitialized); char *out = result.data(); out = iface->fromUtf16(out, in, &state); @@ -120,12 +131,23 @@ public: #endif qsizetype requiredSpace(qsizetype inputLength) const - { return iface->toUtf16Len(inputLength); } + { return iface ? iface->toUtf16Len(inputLength) : 0; } QChar *appendToBuffer(QChar *out, QByteArrayView ba) - { return iface->toUtf16(out, ba, &state); } + { + if (!iface) { + state.invalidChars = 1; + return out; + } + return iface->toUtf16(out, ba, &state); + } private: QString decodeAsString(QByteArrayView in) { + if (!iface) { + // ensure that hasError returns true + state.invalidChars = 1; + return {}; + } QString result(iface->toUtf16Len(in.size()), Qt::Uninitialized); const QChar *out = iface->toUtf16(result.data(), in, &state); result.truncate(out - result.constData()); -- cgit v1.2.3