MISRA C:2012 Rule 22.7
The macro EOF
shall only be compared with the unmodified return
value from any Standard Library function capable of returning
EOF
Description
Rule Definition
The macro EOF
shall only be compared with the
unmodified return value from any Standard Library function capable of returning
EOF
.
This rule comes from MISRA C™: 2012 Amendment 1.
Rationale
The EOF
value returned by a standard library function can become
indistinguishable from a valid character code if the value returned is converted to
another type. In such cases, testing the converted value against
EOF
does not reliably determine whether the end of the file
has been reached or if an error has occurred.
To determine the end of file reliably, use the functions feof()
or ferror()
.
Polyspace Implementation
Polyspace® reports a violation of this rule if these events happen in a sequence:
A standard library function that can return
EOF
is called.The return value of the function is then converted to a different type. This conversion can happen over several steps.
The converted return value is then compared to the macro
EOF
.
Troubleshooting
If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Resources |
Category: Required |
AGC Category: Required |
Version History
Introduced in R2017a