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

QBitArray::testBit with unsigned enum fails to build with gcc13

    XMLWordPrintable

Details

    • Linux/Other display system
    • 8739963c8 (dev), f003d43eb (6.6)

    Description

      A compile error in qbitarray.h, could be a compiler bug, or an actual error that isn't detected when the code is unfolded. To reproduce, pass something unsigned to QBitArray::testBit() - could be simpler ways to reproduce also. In our code we declared an enum of type unsigned, and passed one of the values to QBitArray::testBit().

      qt/include/QtCore/qbitarray.h:86:10: error: array subscript 1 is outside array bounds of 'const char [1]' [-Werror=array-bounds=] 86 | return (*(reinterpret_cast<const uchar*>(d.constData())+1+(i>>3)) & (1 << (i & 7))) != 0; }
      

      Code in question:

      inline bool QBitArray::testBit(qsizetype i) const
      { Q_ASSERT(size_t(i) < size_t(size()));
       return (*(reinterpret_cast<const uchar*>(d.constData())+1+(i>>3)) & (1 << (i & 7))) != 0; } 
      

      Unfolding the code removes the compile error, note that this generates the exact same assembly when built:

      inline bool QBitArray::testBit(qsizetype i) const
      {
          Q_ASSERT(size_t(i) < size_t(size()));
          const auto *castedDataPointer = reinterpret_cast<const uchar*>(d.constData());
          castedDataPointer += 1 + (i >> 3);
          const auto dataByte = *castedDataPointer;
          const auto bitLocation = 1 << (i & 7);
          return (dataByte & bitLocation) != 0;
      }
      

      Attachments

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

        Activity

          People

            thiago Thiago Macieira
            andrhans Andreas Aardal Hanssen
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There is 1 open Gerrit change