Read or write of errno
ID |
c.maintainability.avoid_errno |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
medium |
Remediation Effort |
medium |
Resource |
Error Handling |
Language |
C / C++ |
Description
Reports any read or write of the errno global, both direct assignment and comparison/read.
Rationale
errno is process-wide (or thread-local, but still ambient) mutable state that any library call along the way can overwrite, so a read of errno is only meaningful immediately after the one call whose failure it is documented to report, and only when nothing else runs in between. Threading it through application logic couples that logic to an implicit global instead of an explicit, testable return value or status type. Safety-critical and embedded coding standards ban errno outright in favor of explicit error propagation.
The following code illustrates the pattern detected by this rule:
void f1(void) {
// FLAGGED: Read or write of errno
errno = 0;
}
Remediation
Report and propagate errors through the function’s own return value or a dedicated status/ result type instead of relying on errno. Where a wrapped library call must inspect errno, confine the read to immediately after that one call and convert it to the application’s own error representation before it is used further.