summaryrefslogtreecommitdiffstats
path: root/src/corelib
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2014-10-11 21:37:56 +0200
committerMarc Mutz <marc.mutz@kdab.com>2015-04-02 19:28:18 +0000
commit6f145707eb473de1dc4971da0f5b9ebf640a25a2 (patch)
tree3b5f4028a9b56a018c7991f7f5fd286a5faf7b7d /src/corelib
parentffedd0cf640ca75602bdb8151256636d22de413d (diff)
QString::from{Utf8,Latin1,Local8Bit}(QByteArray): preserve nullness of argument
Currently, calling these functions with a null QByteArray will return a non-null QString because QByteArray::data() never returns nullptr. This behavior leads to inconsistencies between QString::append overloads, in particular the QByteArray vs. all others (null + null test always returns a null QString, except for the QByteArray overload before this change). It also is inconsistent with the const char* overloads of these methods, which explicitly preserve nullness (as verified by test cases). Fixed by an explicit check for nullness and return of null QString. Alternative would have been to check for nullness and pass nullptr instead of ba.data() to the _helper() functions, which do the correct thing in that case. But since we'd have the check anyway and with the chosen strategy we can avoid a call to a non-inline method, I opted against that. [ChangeLog][QtCore][QString] fromLatin1(), fromAscii(), fromUtf8() and fromLocal8Bit() now return a null QString when called with a null QByteArray. Change-Id: I5f2c0bad27fb73c7d535085af0271823bf6ed1da Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib')
-rw-r--r--src/corelib/tools/qstring.h6
1 files changed, 3 insertions, 3 deletions
diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h
index bb918f36c8..8ce3d51b2c 100644
--- a/src/corelib/tools/qstring.h
+++ b/src/corelib/tools/qstring.h
@@ -534,11 +534,11 @@ public:
return fromLocal8Bit_helper(str, (str && size == -1) ? int(strlen(str)) : size);
}
static inline QString fromLatin1(const QByteArray &str)
- { return fromLatin1(str.data(), qstrnlen(str.constData(), str.size())); }
+ { return str.isNull() ? QString() : fromLatin1(str.data(), qstrnlen(str.constData(), str.size())); }
static inline QString fromUtf8(const QByteArray &str)
- { return fromUtf8(str.data(), qstrnlen(str.constData(), str.size())); }
+ { return str.isNull() ? QString() : fromUtf8(str.data(), qstrnlen(str.constData(), str.size())); }
static inline QString fromLocal8Bit(const QByteArray &str)
- { return fromLocal8Bit(str.data(), qstrnlen(str.constData(), str.size())); }
+ { return str.isNull() ? QString() : fromLocal8Bit(str.data(), qstrnlen(str.constData(), str.size())); }
static QString fromUtf16(const ushort *, int size = -1);
static QString fromUcs4(const uint *, int size = -1);
static QString fromRawData(const QChar *, int size);