In the beginning of this year, I used the PVS-Studio analyzer to scan some projects by large IT companies, which, following the modern trend, make their projects' sources publicly available under free licenses. I started to notice that almost every project has errors in conditional expressions that deal with incorrect use of conditional operators. Expressions themselves are quite simple and consist of just three operators:
• != || !=
• == || !=
• == && ==
• == && !=
In total, you can write 6 conditional expressions using these operators, but 4 of them are incorrect: two are always true or false; in two others, the result of the whole expression does not depend on the result of one of its subexpressions.
For example, the following is a synthetic example where the conditional expression will always evaluate to true:
if ( err != code1 || err != code2)
{ …. }
Mistakes described in my article are the ones made by the developers of such well-known projects as FreeBSD, Microsoft ChakraCore, Mozilla Thunderbird, LibreOffice, and many others.