
The EOF
macro represents a negative value that is used to indicate that the file is exhausted and no data remains when reading data from a file. EOF
is an example of an in-band error indicator. In-band error indicators are problematic to work with, and the creation of new in-band-error indicators is discouraged by ERR02-C. Avoid in-band error indicators.
...
The C Standard feof()
and ferror()
functions are not subject to the problems associated with character and integer sizes and should be used to verify end-of-file and file errors for susceptible implementations [Kettlewell 2002]. Calling both functions on each iteration of a loop adds significant overhead, so a good strategy is to temporarily trust EOF
and WEOF
within the loop but verify them with feof()
and ferror()
following the loop.
...
Automated Detection
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
CodeSonar |
| LANG.CAST.COERCE | Coercion alters value | ||||||
|
|
| |||||||
6.5 | CHAR_IO | Identifies defects when the return value of | |||||||
1.2 | CC2.FIO34 | Partially implemented | |||||||
5.0 |
| Can detect violations of this rule with CERT C Rule Pack | |||||||
Parasoft C/C++test | 9.5 | MISRA2004-10_1_d | Partially implemented | ||||||
3.1.1 |
|
|
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Related Guidelines
Bibliography
[Kettlewell 2002] | Section 1.2, "<stdio.h > and Character Types" |
[NIST 2006] | SAMATE Reference Dataset Test Case ID 000-000-088 |
[Summit 2005] | Question 12.2 |