From 19d160b72ba8e1f52eab63e73c85f0e60f630cc6 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Wed, 25 Apr 2012 15:02:12 +0200 Subject: Fix the QByteArray overloads to QString::fromXXXX c951908bc201afa59402967d50fa926212845fae added these overloads, but did not properly use qstrnlen to get the size. This means we get subtle errors because other methods do have them. For example: QByteArray ba("abc\0def", 7); // ba embedding a NUL QString s1(ba); QString s2 = QString::fromAscii(ba); s1 == s2; // FAILS QString s3; s3.append(ba); s3 == s2; // FAILS Tested in an upcoming commit. Change-Id: I22864521a42da789d522d7b75790696928d9ec32 Reviewed-by: Lars Knoll --- src/corelib/tools/qstring.h | 53 ++++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index edb140b682..5b6869d111 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -456,10 +456,14 @@ public: { return fromLocal8Bit_helper(str, (str && size == -1) ? int(strlen(str)) : size); } - static inline QString fromAscii(const QByteArray &str) { return fromAscii(str.data(), str.size()); } - static inline QString fromLatin1(const QByteArray &str) { return fromLatin1(str.data(), str.size()); } - static inline QString fromUtf8(const QByteArray &str) { return fromUtf8(str.data(), str.size()); } - static inline QString fromLocal8Bit(const QByteArray &str) { return fromLocal8Bit(str.data(), str.size()); } + static inline QString fromAscii(const QByteArray &str) + { return fromAscii(str.data(), qstrnlen(str.constData(), str.size())); } + static inline QString fromLatin1(const QByteArray &str) + { return fromLatin1(str.data(), qstrnlen(str.constData(), str.size())); } + static inline QString fromUtf8(const QByteArray &str) + { return fromUtf8(str.data(), qstrnlen(str.constData(), str.size())); } + static inline QString fromLocal8Bit(const QByteArray &str) + { return 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); @@ -580,13 +584,13 @@ public: inline QT_ASCII_CAST_WARN bool operator==(const QByteArray &s) const; inline QT_ASCII_CAST_WARN bool operator!=(const QByteArray &s) const; inline QT_ASCII_CAST_WARN bool operator<(const QByteArray &s) const - { return *this < QString::fromAscii(s.constData(), s.size()); } + { return *this < QString::fromAscii(s); } inline QT_ASCII_CAST_WARN bool operator>(const QByteArray &s) const - { return *this > QString::fromAscii(s.constData(), s.size()); } + { return *this > QString::fromAscii(s); } inline QT_ASCII_CAST_WARN bool operator<=(const QByteArray &s) const - { return *this <= QString::fromAscii(s.constData(), s.size()); } + { return *this <= QString::fromAscii(s); } inline QT_ASCII_CAST_WARN bool operator>=(const QByteArray &s) const - { return *this >= QString::fromAscii(s.constData(), s.size()); } + { return *this >= QString::fromAscii(s); } #endif typedef QChar *iterator; @@ -700,17 +704,30 @@ public: { return s >= *this; } inline QT_ASCII_CAST_WARN bool operator==(const char *s) const - { return QString::fromAscii(s, s ? int(strlen(s)) : -1) == *this; } + { return QString::fromAscii(s) == *this; } inline QT_ASCII_CAST_WARN bool operator!=(const char *s) const - { return QString::fromAscii(s, s ? int(strlen(s)) : -1) != *this; } + { return QString::fromAscii(s) != *this; } inline QT_ASCII_CAST_WARN bool operator<(const char *s) const - { return QString::fromAscii(s, s ? int(strlen(s)) : -1) > *this; } + { return QString::fromAscii(s) > *this; } inline QT_ASCII_CAST_WARN bool operator>(const char *s) const - { return QString::fromAscii(s, s ? int(strlen(s)) : -1) < *this; } + { return QString::fromAscii(s) < *this; } inline QT_ASCII_CAST_WARN bool operator<=(const char *s) const - { return QString::fromAscii(s, s ? int(strlen(s)) : -1) >= *this; } + { return QString::fromAscii(s) >= *this; } inline QT_ASCII_CAST_WARN bool operator>=(const char *s) const - { return QString::fromAscii(s, s ? int(strlen(s)) : -1) <= *this; } + { return QString::fromAscii(s) <= *this; } + + inline QT_ASCII_CAST_WARN bool operator==(const QByteArray &s) const + { return QString::fromAscii(s) == *this; } + inline QT_ASCII_CAST_WARN bool operator!=(const QByteArray &s) const + { return QString::fromAscii(s) != *this; } + inline QT_ASCII_CAST_WARN bool operator<(const QByteArray &s) const + { return QString::fromAscii(s) > *this; } + inline QT_ASCII_CAST_WARN bool operator>(const QByteArray &s) const + { return QString::fromAscii(s) < *this; } + inline QT_ASCII_CAST_WARN bool operator<=(const QByteArray &s) const + { return QString::fromAscii(s) >= *this; } + inline QT_ASCII_CAST_WARN bool operator>=(const QByteArray &s) const + { return QString::fromAscii(s) <= *this; } private: int m_size; const char *m_data; @@ -1027,14 +1044,14 @@ inline bool operator>=(const QLatin1String &s1, const QLatin1String &s2) inline bool QString::operator==(const QByteArray &s) const -{ return qStringComparisonHelper(*this, s.constData()); } +{ return qStringComparisonHelper(*this, s); } inline bool QString::operator!=(const QByteArray &s) const -{ return !qStringComparisonHelper(*this, s.constData()); } +{ return !qStringComparisonHelper(*this, s); } inline bool QByteArray::operator==(const QString &s) const -{ return qStringComparisonHelper(s, constData()); } +{ return qStringComparisonHelper(s, *this); } inline bool QByteArray::operator!=(const QString &s) const -{ return !qStringComparisonHelper(s, constData()); } +{ return !qStringComparisonHelper(s, *this); } inline bool QByteArray::operator<(const QString &s) const { return QString::fromAscii(constData(), size()) < s; } inline bool QByteArray::operator>(const QString &s) const -- cgit v1.2.3