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

Incorrect initialization of QBitArray from dense byte array

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P2: Important
    • 5.12.5, 5.14.0 Alpha
    • 5.12.4, 5.13.0
    • Core: Other
    • None
    • All
    • 6ce9404a6e7ad6ba3ff37f6890fe400c643c3d52 (qt/qtbase/5.12)

    Description

      When initializing a QBitArray using function:

      static QBitArray fromBits(const char *data, qsizetype len);
      

      The resulting bit array isn't always properly initialized.

      I wrote an example where I have one byte but want to use only 5 bits.

      // Example code
      #include <QCoreApplication>
      #include <QBitArray>
      #include <stdio.h>
      
      int main(int argc, char* argv[])
      {
          QCoreApplication a(argc, argv);
          char c = 0xff;
          const int nbits = 5;
          const QBitArray ba = QBitArray::fromBits(&c, nbits);
      
          for(int b=0 ; b<nbits ; ++b)
              printf("%d ", ba.testBit(b) ? 1 : 0);
      
          fflush(stdout);
      
          return a.exec();
      }
      

      The above code produces the following result:

      1 1 1 0 0
      

      When in fact it should produce:

      1 1 1 1 1
      

      This happens always in the last byte of the array, I was able to reproduce this in arrays of several sizes, this example has one byte only for simplicity.

       

      Attachments

        For Gerrit Dashboard: QTBUG-77285
        # Subject Branch Project Status CR V

        Activity

          People

            thiago Thiago Macieira
            ruisilva Rui Silva
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes