[NeoStats-Devel] Potential problems - Yahtzeeserv

"M" <[email protected]>
Newsgroups gmane.comp.neostats.devel
Message-ID <[email protected]>
It is not a good idea to rely on C compiler precedence rules to determine
the priority of tests since they vary across compilers and compiler versions
so brackets should be used to ensure the precedence will be as intended. As
an example, in YahtzeeServ we have the following code in the reroll
function:

(!strchr(buf, (i + 49)) == NULL && rolltype)

It is not clear what the intention is here but I assume it is:

( !( strchr(buf, (i + 49)) == NULL ) && rolltype )

However, the compiler might choose to make this test:

( ( (!strchr(buf, (i + 49))) == NULL ) && rolltype )

Which would provide the complete opposite effect.

I have already changed a similar construct in SeenServ but wanted to raise
this issue before it propogates throughout other areas of code.

There are various ways to fix it. strchr will return NULL or a pointer so:

1) strchr(buf, char ) for TRUE.
   !strchr(buf, char ) for FALSE.

2) strchr(buf, char ) != NULL for TRUE.
   strchr(buf, char ) == NULL for FALSE.

Please try not to mix logical tests since it makes the code difficult to
read and as mentioned above, different compilers may result in the opposite
of the intented result occuring. If you do choose to mix logical tests, make
sure that sufficient brackets are in place so that the compiler cannot make
unexpected decisions.

Mark.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.