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

QByteArray throws bad allocation exception during prepend/append process.

    XMLWordPrintable

Details

    Description

      The code snippets are shown below.

      QByteArray small;
      small.resize(1024);
      small.fill('s', 1024);
      
      QByteArray big;
      big.resize(1024*1024*1024);
      big.fill('b');
      
      //The below lines will not work.
      big.append(small); //big += small;
      small.append(big); //small += big; //big.prepend(small);
      

      A workaround for us is to validate the proper size if the one of the QByteArray already has a size more than half of INT_MAX value, as shown below.

      const int HALF_INT_MAX = INT_MAX / 2;
      int combinedByteArraysSize = big.size() + small.size();
      #if 1
          if(combinedByteArraysSize > HALF_INT_MAX && combinedByteArraysSize < INT_MAX) {
              big.reserve(combinedByteArraysSize);
              big.append(small); //big += small;
          }
      #else //Below lines does not work on Windows system, but work on OS X system.
          if(combinedByteArraysSize > HALF_INT_MAX && combinedByteArraysSize < INT_MAX) {
              small.reserve(combinedByteArraysSize);
              small.append(big); //small += big; //big.prepend(small);
          }
      #endif
      

      Please see bytearraytest_v1.zip for the full test program.

      Attachments

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

        Activity

          People

            thiago Thiago Macieira
            leonlee Leonard Lee
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes