V739. EOF should not be compared with a value of the 'char' type. Consider using the 'int' type.


The analyzer detected that the EOF constant is compared with a variable of type 'char' or 'unsigned char'. Such comparison implies that some of the characters won't be processed correctly.

Let's see how EOF is defined:

#define EOF (-1)

That is, EOF is actually but the value '-1' of type 'int'. Let's see what complications may occur. The first example:

unsigned char c;
while ((c = getchar()) != EOF)
  { .... }

The unsigned variable 'c' can never refer to the negative value '-1', so the expression ((c = getchar) != EOF) is always true and an infinite loop occurs. An error like that would be noticed and fixed right off in a real program, so there's no need to discuss the 'unsigned char' type further.

Here's a more interesting case:

signed char c;
while ((c = getchar()) != EOF)
  { .... }

The getchar() function returns values of type 'int', namely numbers within the range 0 - 255 or the value -1 (EOF). The read value is assigned to a variable of type 'char'. This operation causes the character with the code 0xFF (255) to turn into -1 and be interpreted just the same way as the end of a file (EOF).

Users who use Extended ASCII Codes sometimes face an issue when one of the characters of their alphabet is incorrectly processed by programs.

For example, the last letter of the Russian alphabet is encoded with that very value 0xFF in the Windows-1251 encoding and is interpreted as EOF by some programs.

The fixed version of the code should look like this:

int c;
while ((c = getchar()) != EOF)

According to Common Weakness Enumeration, potential errors found by using this diagnostic are classified as CWE-20.

You can look at examples of errors detected by the V739 diagnostic.


Bugs Found

Checked Projects
336
Collected Errors
12 743