Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

else statement is seemingly ignored

void PacketRecord::determineAppProtocol()
{
    if (ipProtocol == IP_PROTO_UDP)
    {
        std::istringstream ss(udpData);

        std::string line;
        if (getline(ss, line) && (line.find("SIP/2.0") != std::string::npos))
        {
            appProtocol = APP_PROTO_SIP;
        }
        else
        {
            appProtocol == APP_PROTO_RTP;
        }
    }
    else
    {
        appProtocol = APP_PROTO_UNKNOWN;
    }
}

If the inner if statement fails to evaluate to true, I would expect the else block to be executed (appProtocol set to APP_PROTO_RTP). However, this does not happen. Instead, it seems the else statement is completely ignored. I can't fathom why this is the case.

As you can see from my gdb session, the first time the if statement works and appProtocol is set to APP_PROTO_SIP (as expected). the second time through, the if fails but instead of going into the else and setting appProtocol to APP_PROTO_RTP, it returns out of the function completely without setting appProtocol. appProtocol remains set to APP_PROTO_INVALID (the value it is initialized with in the PacketRecord ctor).

Breakpoint 1, PacketRecord::determineAppProtocol (this=0x805c6c8) at PacketRecord.cpp:156
156     if (ipProtocol == IP_PROTO_UDP)
(gdb) step
158         std::istringstream ss(udpData);
(gdb) 
159         std::string line;
(gdb) 
160         if (getline(ss, line) && (line.find("SIP/2.0") != std::string::npos))
(gdb) 
162             appProtocol = APP_PROTO_SIP;
(gdb) 
167         }
(gdb) 
173 }
(gdb) continue 
Continuing.

Breakpoint 1, PacketRecord::determineAppProtocol (this=0x8065388) at PacketRecord.cpp:156
156     if (ipProtocol == IP_PROTO_UDP)
(gdb) step
158         std::istringstream ss(udpData);
(gdb) 
159         std::string line;
(gdb) 
160         if (getline(ss, line) && (line.find("SIP/2.0") != std::string::npos))
(gdb) 
167         }
(gdb) 
173 }
(gdb) 
like image 837
bretttolbert Avatar asked Aug 28 '10 15:08

bretttolbert


2 Answers

You should replace

appProtocol == APP_PROTO_RTP;

by

appProtocol = APP_PROTO_RTP;

(no double equal sign)

The else statement is executed. But you are not assigning the value to appProtocol in it.

like image 96
JochenJung Avatar answered Oct 25 '22 10:10

JochenJung


You're not assigning, you're comparing. Use =, not ==

like image 5
Ivo van der Wijk Avatar answered Oct 25 '22 09:10

Ivo van der Wijk