diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2017-01-28 09:30:14 +0100 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2017-02-15 20:12:36 +0000 |
commit | a5febadac55741b4b48f25463a310835cc8bde19 (patch) | |
tree | e64b22c451c56272381ae3b1423a52dbc3677d6a /src | |
parent | 798819bb87d9a2d1a9e16c4102b25e01814f1b86 (diff) |
QString(Ref): make toLatin1()/toLocal8Bit() null handling consistent
Systematic testing in tst_QStringApiSymmetry revealed a bug in
QStringRef::toLatin1(): a null input did not result in a null output,
but an empty one. This is fixed, for consistency with
QString::toLatin1(), and QString(Ref)::toUtf8(), which behaved
correctly already.
The same bug was found in QString(Ref)::toLocal8Bit(), which is
particularly hideous, as it's documented to fall back to toLatin1(),
which preserves null inputs. Fixed, too.
[ChangeLog][QtCore][QString] toLocal8Bit() now preserves nullness of
the input QString (outputs null QByteArray).
[ChangeLog][QtCore][QStringRef] toLocal8Bit() and toLatin1() now
preserve nullness of the input QStringRef (output null QByteArrays).
Change-Id: I7026211922c287e03d07e89edbad2987aa646e51
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/tools/qstring.cpp | 12 | ||||
-rw-r--r-- | src/corelib/tools/qstring.h | 4 | ||||
-rw-r--r-- | src/corelib/tools/qstring_compat.cpp | 2 |
3 files changed, 12 insertions, 6 deletions
diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index 606893996c..d76eb11073 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -4635,6 +4635,8 @@ QByteArray QString::toLatin1_helper_inplace(QString &s) QByteArray QString::toLocal8Bit_helper(const QChar *data, int size) { + if (!data) + return QByteArray(); #ifndef QT_NO_TEXTCODEC QTextCodec *localeCodec = QTextCodec::codecForLocale(); if (localeCodec) @@ -10376,6 +10378,8 @@ static inline bool qt_ends_with(const QChar *haystack, int haystackLen, */ QByteArray QStringRef::toLatin1() const { + if (isNull()) + return QByteArray(); return QString::toLatin1_helper(unicode(), length()); } @@ -10414,9 +10418,11 @@ QByteArray QStringRef::toLatin1() const QByteArray QStringRef::toLocal8Bit() const { #ifndef QT_NO_TEXTCODEC - QTextCodec *localeCodec = QTextCodec::codecForLocale(); - if (localeCodec) - return localeCodec->fromUnicode(unicode(), length()); + if (!isNull()) { + QTextCodec *localeCodec = QTextCodec::codecForLocale(); + if (localeCodec) + return localeCodec->fromUnicode(unicode(), length()); + } #endif // QT_NO_TEXTCODEC return toLatin1(); } diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index 08242bff11..00ab1d16b0 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -532,9 +532,9 @@ public: QByteArray toUtf8() && Q_REQUIRED_RESULT { return toUtf8_helper(*this); } QByteArray toLocal8Bit() const & Q_REQUIRED_RESULT - { return toLocal8Bit_helper(constData(), size()); } + { return toLocal8Bit_helper(isNull() ? nullptr : constData(), size()); } QByteArray toLocal8Bit() && Q_REQUIRED_RESULT - { return toLocal8Bit_helper(constData(), size()); } + { return toLocal8Bit_helper(isNull() ? nullptr : constData(), size()); } #else QByteArray toLatin1() const Q_REQUIRED_RESULT; QByteArray toUtf8() const Q_REQUIRED_RESULT; diff --git a/src/corelib/tools/qstring_compat.cpp b/src/corelib/tools/qstring_compat.cpp index 300f8467b1..45bb816e4b 100644 --- a/src/corelib/tools/qstring_compat.cpp +++ b/src/corelib/tools/qstring_compat.cpp @@ -80,7 +80,7 @@ QByteArray QString::toLatin1() const QByteArray QString::toLocal8Bit() const { - return toLocal8Bit_helper(constData(), size()); + return toLocal8Bit_helper(isNull() ? nullptr : constData(), size()); } QByteArray QString::toUtf8() const |