Details
-
Bug
-
Resolution: Done
-
P2: Important
-
5.5.0
-
None
-
Qt 5.5.0 MinGW, Windows 7 x64 (but should be reproducible on all platforms)
-
d24366a63281543e6c1a8e6b615ce882ea6259ab
Description
The comparison operators (<, <=, > and >=) when the LHS is QByteArray and the RHS is QString are returning incorrect results. For example:
// All of these should return 0 but the second one returns 1 qDebug("cpu >= model = %d", (int) (QString("cpu") >= QString("model"))); qDebug("cpu >= model = %d", (int) (QByteArray("cpu") >= QString("model"))); qDebug("cpu >= model = %d", (int) (QString("cpu") >= QByteArray("model"))); qDebug("cpu >= model = %d", (int) (QByteArray("cpu") >= QByteArray("model")));
The problem is in lines 1230-1237 of QtCore/qstring.h:
inline bool QByteArray::operator<(const QString &s) const { return QString::compare_helper(s.constData(), s.size(), constData(), qstrnlen(constData(), size())) < 0; } inline bool QByteArray::operator>(const QString &s) const { return QString::compare_helper(s.constData(), s.size(), constData(), qstrnlen(constData(), size())) > 0; } inline bool QByteArray::operator<=(const QString &s) const { return QString::compare_helper(s.constData(), s.size(), constData(), qstrnlen(constData(), size())) <= 0; } inline bool QByteArray::operator>=(const QString &s) const { return QString::compare_helper(s.constData(), s.size(), constData(), qstrnlen(constData(), size())) >= 0; }
The places of the LHS and RHS operands are switched but the operators remain the same. The fix is just to reverse the operators too:
inline bool QByteArray::operator<(const QString &s) const { return QString::compare_helper(s.constData(), s.size(), constData(), qstrnlen(constData(), size())) > 0; } inline bool QByteArray::operator>(const QString &s) const { return QString::compare_helper(s.constData(), s.size(), constData(), qstrnlen(constData(), size())) < 0; } inline bool QByteArray::operator<=(const QString &s) const { return QString::compare_helper(s.constData(), s.size(), constData(), qstrnlen(constData(), size())) >= 0; } inline bool QByteArray::operator>=(const QString &s) const { return QString::compare_helper(s.constData(), s.size(), constData(), qstrnlen(constData(), size())) <= 0; }
Also, it would be a good idea to include an additional automated test because this is something that should have been caught by the automated testing and never appear in an official release.
Attachments
For Gerrit Dashboard: QTBUG-48350 | ||||||
---|---|---|---|---|---|---|
# | Subject | Branch | Project | Status | CR | V |
126484,2 | Fix comparisons between QByteArray and QString. | 5.5 | qt/qtbase | Status: MERGED | +2 | 0 |