Misuse of errno
errno
incorrectly checked for error
conditions
Description
This defect occurs when
you check errno
for error conditions in situations
where checking errno
does not guarantee
the absence of errors. In some cases, checking errno
can
lead to false positives.
For instance, you check errno
following calls
to the functions:
fopen
: If you follow the ISO® Standard, the function might not seterrno
on errors.atof
: If you follow the ISO Standard, the function does not seterrno
.signal
: Theerrno
value indicates an error only if the function returns theSIG_ERR
error indicator.
Risk
The ISO C Standard does not enforce that these functions
set errno
on errors. Whether the functions set errno
or
not is implementation-dependent.
To detect errors, if you check errno
alone,
the validity of this check also becomes implementation-dependent.
In some cases, the errno
value indicates
an error only if the function returns a specific error indicator.
If you check errno
before checking the function
return value, you can see false positives.
Fix
For information on how to detect errors, see the documentation for that specific function.
Typically, the functions return an out-of-band error indicator to indicate errors. For instance:
fopen
returns a null pointer if an error occurs.signal
returns theSIG_ERR
error indicator and setserrno
to a positive value. Checkerrno
only after you have checked the function return value.
Examples
Result Information
Group: Programming |
Language: C | C++ |
Default: On for handwritten code, off for generated code |
Command-Line Syntax: ERRNO_MISUSE |
Impact: High |
Version History
Introduced in R2017a
See Also
Find defects (-checkers)
| Returned value of a sensitive function not checked
| Errno not reset
| Errno not checked
| Unsafe conversion from string to numerical 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)