Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-48350

QByteArray comparison operators with QString are broken

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P2: Important
    • 5.6.0 RC
    • 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

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            kandeler Christian Kandeler
            twa009 Stoyan Petkov
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes