Data type conversion makes a valid character value same as End-of-File (EOF)
This defect occurs
when you perform a data type conversion that makes a valid character
value indistinguishable from EOF (End-of-File).
Bug Finder flags the defect in one of the following situations:
End-of-File: You perform a data
type conversion such as from int to char that
converts a non-EOF character value into EOF.
char ch = (char)getchar()
if((int)ch == EOF)
Wide End-of-File: You perform a data type conversion that can convert a non-WEOF wide character value into WEOF, and then compare the result with WEOF.
The data type char cannot hold the value EOF that
indicates the end of a file. Functions such as getchar have
return type int to accommodate EOF.
If you convert from int to char,
the values UCHAR_MAX (a valid character value)
and EOF get converted to the same value -1 and
become indistinguishable from each other. When you compare the result
of this conversion with EOF, the comparison can
lead to false detection of EOF. This rationale
also applies to wide character values and WEOF.
Perform the comparison with EOF or WEOF before
conversion.
| Group: Programming |
| Language: C | C++ |
| Default: On for handwritten code, off for generated code |
Command-Line Syntax: CHAR_EOF_CONFUSED |
| Impact: High |
| CWE ID: 704 |
Errno not checked | Invalid use of standard library integer routine | Misuse of sign-extended character value | Returned value of a sensitive function not checked