diff options
author | Fabian Kosmale <fabian.kosmale@qt.io> | 2022-05-25 09:07:37 +0200 |
---|---|---|
committer | Fabian Kosmale <fabian.kosmale@qt.io> | 2022-05-25 16:38:35 +0200 |
commit | c46ee7df57c30c94107df8506d30d8872ffa3baa (patch) | |
tree | 74e2f1c109210ec38f634699ce879bf081a2cd02 /src | |
parent | d83441340c210056d25777ed59ec6dab6351a55f (diff) |
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 <lars.knoll@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/text/qstringconverter.h | 30 |
1 files changed, 26 insertions, 4 deletions
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()); |