Search code examples
c++while-loopgdbqt5program-flow

During debugging my program jumps to nearest while statement without break or continue [C++]


When running my C++/Qt5 program under gdb I experienced what seems like an impossibility:

while(totalAvailable > (sizeof(quint32)+sizeof(quint16))){  
    if(nullptr!=c){
        // POINT-A
        qDebug()<<rct<<"Courier message with ID "<<octomy_message_type_int<<" was received with name "<<c->getName()<<" and "<<bytesAvailable<<" bytes available";
        const quint16 bytesSpent=c->dataReceived(*ds, bytesAvailable);
        const int left=bytesAvailable-bytesSpent;
        totalAvailable-=bytesSpent;
        if(left>=0){
            if(left>0){
                ds->skipRawData(left);
                totalAvailable-=left;
            }
            else{
                // POINT-B
                qDebug()<<rct<<"LOLBOB";
            }
        }
        else{
            qWarning()<<"BAR";
            return;
        }
    }
    else{
        qWarning()<<"FOO";
        return;
    }
}

In short, when I step from //POINT-A to //POINT-B everything is as expected, but as soon as I step past //POINT-B debugger jumps up to the first line of the program (while statement). But there are no break or continue or other flow-altering statements in the code. How is this possible?

I have tried rebuilding my code from scratch to eliminate a bogus linkage or similar problems, but the bug is reproducible still.

Any input is welcomed.


Solution

  • At POINT-B, you are inside an else, which is inside an if, which is inside an if. Once this else is done, there is nothing more to do in the whole tree.

    Where would you expect the pointer to jump to??

    Technically, it would go to the closing bracket two lines behind POINT-B, then to the closing bracket three lines behind POINT-B, and then to closing bracket eight lines behind POINT-B, and then to the closing bracket at the very end. All those do nothing, so they are skipped.