Nominate our 2022 Qt Champions!

ASSERT: "uint(i) < uint(size())" qbytearray error

  • Hello,

    I think there's something wrong in my code because sometimes it throw and ASSERT error :

    QList<int> QlChannelSerial::readBytes() {

    QList<int> *l = new QList<int>();
    QList<int> err({1,1});
    QByteArray buff = QByteArrayLiteral("0000");
    if (port_->isOpen() ){
                buff = port_->readAll();
                    buff +=port_->readAll();
                for (int i=0; i<buff.size(); i++)
                if( != 'M' && != 'P' && != '$' && != 'G' && != 'P' && != 'R')
                    return err;
            while( != 'M' && != 'P' && buff.size() < 5);
         return *l;

    What i'm trying here is to read a Serial Buffer but since it's UART, so not synchrone i want to copy it from zero at exact beginning.

    i'm receiving those data in qml and show them in differents textbox...

    the "err" variable is here to repeat the readBytes() if i didn't receive the correct UART string in order (which is very dirty way i think, but i don't see how to fill my buffer correctly with so randoms reading from Serial)

    Thank you for helping

  • Lifetime Qt Champion


    One strange thing here: != 'M' && != 'P' && != '$' && != 'G' && != 'P' && != 'R'

    You don't even check that your buffer is large enough to access these positions.

  • It's not just "strange", it's the reason for the ASSERT. I can guess from the text that Qt is checking each QByteArray access to ensure the index (i) is less than than the current content size (size()). If you check buff.size() you'll find it's less than 124.

  • thank you, i sometimes got random values but now it doesn't crash anymore


  • Lifetime Qt Champion

    @rzizanie But you still should check the range - always!

Log in to reply