Many existing functions that return errno
are declared as returning a value of type int
. It is semantically unclear by looking at the function declaration or prototype if these functions return an error status or a value or worse, some combination of the two. (see See guideline ERR02-C. Avoid in-band error indicators.).
TR 24731-1 introduces the new type errno_t
that is defined to be type int
in errno.h
and elsewhere. Many of the functions defined in TR 24731-1 return values of this type. The errno_t
type should be used as the type of an object that may only contain values that might be found in errno
. For example, a function that returns the value of errno
should be declared as having the return type errno_t
.
...
This noncompliant code example, however, does comply with guideline ERR30-C. Set errno to zero before calling a library function known to set errno, and check errno only after the function returns a value indicating failure.
...
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Other Languages
Related Guidelines
This rule appears in the C++ Secure Coding Standard as : DCL09-CPP. Declare functions that return errno with a return type of errno_t.
...
Wiki Markup |
---|
\[[ISO/IEC 9899:1999|AA. Bibliography#ISO/IEC 9899-1999]\] Section 6.7.5.3, "Function declarators (including prototypes)" \[[ISO/IEC PDTR 24772|AA. Bibliography#ISO/IEC PDTR 24772]\] "NZN Returning error status" \[[ISO/IEC TR 24731-1:2007|AA. Bibliography#ISO/IEC TR 24731-1-2007]\] \[[MISRA 042004|AA. Bibliography#MISRA 04]\] Rule 20.5 \[[Open Group 042004|AA. Bibliography#Open Group 04]\] |
...