Fix the QByteArray overloads to QString::fromXXXX

c951908bc2 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 <lars.knoll@nokia.com>
This commit is contained in:
Thiago Macieira 2012-04-25 15:02:12 +02:00 committed by Qt by Nokia
parent 2af70ba396
commit 19d160b72b

View File

@ -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