<html>
<head>
<meta content="text/html; charset=windows-1252"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">Hello,<br>
<br>
Alex is right, but the new code example doesn't do exactly like
the old one ...<br>
<br>
On 04/05/16 06:19, Alex Balashov wrote:<br>
</div>
<blockquote cite="mid:5729783C.5040405@evaristesys.com" type="cite">Nathan,
<br>
<br>
I think this is actually deliberate. The return value of an
assignment operation is always going to be true, so it's just an
"overly clever" way of compactly setting the value of 'ret' to -2
within the same conditional evaluation.
<br>
<br>
The aim there is compactness, though it does obscure readability,
and should probably be refactored into:
<br>
<br>
if(ptr && !(VALID_CONTACT(ptr,act_time)
<br>
&& allowed_method(_m,ptr))) {
<br>
ret = -2;
<br>
goto done;
<br>
}
<br>
<br>
Sometimes programmers like to do things like this because variety
is the spice of life, but certainly, they are likely to elicit
bewilderment in others. :-)
<br>
<br>
</blockquote>
<p>Indeed the condition does what is expected -- just to clarify
better for those that want to understand more, it can be also
written like:</p>
<p> if( (ptr) && ( !VALID_CONTACT(ptr,act_time)<br>
|| !(ret=-2) || !allowed_method(_m,ptr)))</p>
<p>It is also important to know that in C the evaluation of an
expression stops when its final result cannot change anymore.<br>
</p>
<p>The evaluation is:</p>
<p>- if not a valid contact then the ret=-2 and allow_method() are
not executed anymore, because the condition is already true. The
ret was initialized to -1, so the function returns -1<br>
</p>
<p>- if valid contact, ret=-2 is always executed, but now with !
(negation) is always false, so allow_method() is also executed and
goto done happens if the method is not accepted by the target, in
this case function returns -2</p>
<p>- if the method is allowed, then the ret is -2, but few lines
later is set to 1.</p>
<p>An alternative of the IF block will be:</p>
<p> if(!(VALID_CONTACT(ptr,act_time))<br>
goto done;<br>
if(!allowed_method(_m,ptr))) {
<br>
ret = -2;
<br>
goto done;
<br>
}
</p>
<p>Anyhow, looking at the code I spotted a minor issue -- ptr should
not be checked anymore, because the line before the IF is
accessing it. So if it is NULL, then it will crash at the line
before. However, the function setting the ptr was successful, so
ptr should not be null there - the all code:<br>
</p>
<p> res = ul.get_urecord_by_ruid(_d, ahash, &inst,
&r, &ptr);<br>
if(res<0) {<br>
LM_DBG("temp gruu '%.*s' not found in usrloc\n",
aor.len, ZSW(aor.s));<br>
return -1;<br>
}<br>
</p>
<p> aor = *ptr->aor;<br>
/* test if un-expired and suported contact */<br>
if( (ptr) && !(VALID_CONTACT(ptr,act_time)<br>
&& (ret=-2) &&
allowed_method(_m,ptr)))</p>
<p>This is the part for temporary gruu lookup, my guess it's not
much used out there. I will refactor to handle better the ptr and
show more clear the conditions.</p>
<p>Cheers,<br>
Daniel<br>
</p>
<br>
<pre class="moz-signature" cols="72">--
Daniel-Constantin Mierla
<a class="moz-txt-link-freetext" href="http://www.asipto.com">http://www.asipto.com</a>
<a class="moz-txt-link-freetext" href="http://twitter.com/#!/miconda">http://twitter.com/#!/miconda</a> - <a class="moz-txt-link-freetext" href="http://www.linkedin.com/in/miconda">http://www.linkedin.com/in/miconda</a>
Kamailio World Conference, Berlin, May 18-20, 2016 - <a class="moz-txt-link-freetext" href="http://www.kamailioworld.com">http://www.kamailioworld.com</a></pre>
</body>
</html>