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() ){
    
        if(port_->waitForReadyRead(2000))
        {
    
            do
            {
                buff = port_->readAll();
                while(port_->waitForReadyRead(100))
                {
                    buff +=port_->readAll();
                }
    
                for (int i=0; i<buff.size(); i++)
                {
                    l->append(buff.at(i));
                }
    
                if(buff.at(0) != 'M' && buff.at(1) != 'P' && buff.at(121) != '$' && buff.at(122) != 'G' && buff.at(123) != 'P' && buff.at(124) != 'R')
                {
                    return err;
                }
    
            }
            while(buff.at(0) != 'M' && buff.at(1) != '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

    Hi,

    One strange thing here:

    buff.at(0) != 'M' && buff.at(1) != 'P' && buff.at(121) != '$' && buff.at(122) != 'G' && buff.at(123) != 'P' && buff.at(124) != '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

    solved.


  • Moderators

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.