Brackets that didn’t help

A guy sent an email to our support saying that the analyzer was producing four false positives at once on one line of his code.

V560 warnings here are all false. Running with most recent version of PVS-Studio for personal use. Basically, “IF” statement is correct. Outer one is done for speed – inner ones are still needed and non are always true or false.

if (ch >= 0x0FF00)
{
	if (!((ch >= 0x0FF10) && (ch <= 0x0FF19)) || ((ch >= 0x0FF21) && (ch <= 0x0FF3A)) || ((ch >= 0x0FF41) && (ch <= 0x0FF5A)))
	{
		if (j == 0)
			continue;
		ch = chx;
	}
}

Now let me explain it to those who were too lazy to bother trying. Look at the condition once again:

if (!((ch >= 0x0FF10) && (ch <= 0x0FF19)) ||
     ((ch >= 0x0FF21) && (ch <= 0x0FF3A)) ||
     ((ch >= 0x0FF41) && (ch <= 0x0FF5A)))

The programmer intended to check that the character didn’t fall into any of the three ranges.

The error here is that the logical NOT (!) operator is applied only to the first subexpression.

If this condition is true:

!((ch >= 0x0FF10) && (ch <= 0x0FF19))

then further evaluation of the expression is aborted, just as prescribed by the short-circuit evaluation semantics. If the condition is false, then the value of the ch variable lies in the range [0xFF10..0xFF19] and the next four comparisons make no sense since they will all be either true or false.

Please click here to see more bugs from this project.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.