kamailio / kamailio-wiki

Kamailio Wiki with content in markdown format
GNU General Public License v2.0
36 stars 21 forks source link

Using `break` outside of `case` #27

Open dilyanpalauzov opened 1 year ago

dilyanpalauzov commented 1 year ago

https://www.kamailio.org/wikidocs/cookbooks/devel/core/ says about break:

'break' statement can be used to end a 'case' block in a 'switch' statement or exit from a 'while' statement.

IMPORTANT NOTE: break can be used only to mark the end of a case branch (as it is in shell scripts). If you are trying to use break outside a case block the script will return error -- you must use return there.

https://www.kamailio.org/docs/modules/devel/modules/tm.html contains examples with break, like:

request_route {
  seturi("sip:a@example.com");
  append_branch("sip:b@example.com");
  append_branch("sip:c@example.com");
  append_branch("sip:d@example.com");

  t_relay();
  break;
}

Either the definition for break is wrong, or the four tm-examples are incorrect.

The cited documentation is contradicting itself on whether break can be used in while.

dilyanpalauzov commented 1 year ago

https://github.com/kamailio/kamailio/blob/master/src/modules/rtpproxy/examples/alg.cfg contains more examples with break outside of case and switch.

miconda commented 1 year ago

The break was the first execution control statement, before exit/drop as well as before while/switch. The examples using break where it should be exit are very old, they must be updated.

Now whether break can still be used there or not: probably it should be tested, in 2008 we merged source code of Kamailio and SER projects, and it could have some functionality inherited from SER code base, which Kamailio devs might not have used or be familiar with. So it could be like a backward-compatibility functionality.

But, over all, the recommandation is to use break on in while/switch, in other places there are more suggesting options like exit or drop.

henningw commented 1 year ago

Just to add, did a quick test, it parses and starts. But we certainly should remove it from the module examples.

henning@app01:~/repositories/kamailio$ cat etc/test.cfg
loadmodule "tm.so"

request_route {
  seturi("sip:a@example.com");

  t_relay();
  break;
}

henning@app01:~/repositories/kamailio$ ./src/kamailio -f etc/test.cfg -E -L src/modules/ -Y /tmp/
Listening on
             udp: 127.0.0.1:5060
[..]