Character value absorbed into EOF
Data type conversion makes a valid character value same as End-of-File (EOF)
Description
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
tochar
that converts a non-EOF character value intoEOF
.You then compare the result with EOF.char ch = (char)getchar()
The conversion can be explicit or implicit.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.
Risk
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
.
Fix
Perform the comparison with EOF
or WEOF
before
conversion.
Examples
Result Information
Group: Programming |
Language: C | C++ |
Default: On for handwritten code, off for generated code |
Command-Line Syntax: CHAR_EOF_CONFUSED |
Impact: High |
Version History
Introduced in R2017a
See Also
Find defects (-checkers)
| Invalid use of standard library integer routine
| Returned value of a sensitive function not checked
| Errno not checked
| Misuse of sign-extended character value
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)