Many functions return useful values whether or not the function has side effects. In most cases, this value is used to signify whether the function successfully completed its task or if some error occurred (see ERR02-C. Avoid in-band error indicators). Other times, the value is the result of some computation and is a necessary output.an integral part of the function's API.
Subclause Section 6.8.3 of C99 \[[of the C Standard [ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\] states that2011] states: Wiki Markup
The expression in an expression statement is evaluated as a void expression for its side effects.
All expression statements, such as function calls with an ignored value, are implicitly cast to void
. Since Because a return value often contains important information about possible errors, it should always be checked; otherwise, the cast should be made explicit to signify programmer intent. If a function returns no meaningful value, it should be declared with return type void
.
This recommendation encompasses MEM32ERR33-C. Detect and handle memory allocation errors, FIO04standard library errors. Unlike this recommendation, that rule is restricted to functions from the Standard C library.
Compliance with this recommendation is required in order to comply with ERR00-C. Detect and handle input and output errors, and FIO34-C. Use int to capture the return value of character IO functions.
Noncompliant Code Example
Adopt and implement a consistent and comprehensive error-handling policy
Noncompliant Code Example
The asprintf()
function has been provided by the GNU C library. It works like sprintf()
, but if given a null pointer as the destination string, it will create a buffer sufficient to hold the resulting string. It relies on malloc()
to allocate the buffer. If malloc()
fails, then asprintf()
returns a negative number.
This noncompliant code example calls putsasprintf()
and ,
but fails to check whether a write error occursthe string was successfully created.
Code Block | ||||
---|---|---|---|---|
| ||||
puts("foo");
|
...
void func(char* name) {
char* s = NULL;
asprintf(&s,"Hello, %s!\n", name);
(void) puts(s);
free(s);
} |
Compliant Solution
This compliant solution checks to make sure no output error occurred (see FIO04-C. Detect and handle input and output errors).
Code Block | ||||
---|---|---|---|---|
| ||||
void func(char* name) { char* s = NULL; if (putsasprintf("foo") == EOF&s,"Hello, %s!\n", name) < 0) { /* Handle Errorerror */ } (void) puts(s); free(s); } |
Exceptions
EXP12-C-EX1: If the return value is inconsequential or if any errors can be safely ignored, such as for functions called because of their side effects, the function should be explicitly cast to void
to signify programmer intent. See the compliant solution for removing an existing destination file For an example of this exception, see "Compliant Solution (Remove Existing Destination File)" under the section "Portable Behavior" in FIO10-C. Take care when using the rename() function for an example of this exception, or Exception ERR33-C-EX1 in ERR33-C. Detect and handle standard library errors.
EXP12-C-EX2: If a function cannot fail or if the return value cannot signify an error condition, the return value may be ignored. Such functions should be added to a white list whitelist when automatic checkers are used.
Code Block | ||||
---|---|---|---|---|
| ||||
strcpy(dst, src);
|
Risk Assessment
Failure to handle error codes or other values returned by functions can lead to incorrect program flow and violations of data integrity.
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
EXP12-C |
Medium |
Unlikely |
Medium | P4 | L3 |
Automated Detection
Splint Version 3.1.1 can detect violations of this recommendation.
Compass/ROSE can detect violations of this recommendation.
...
Tool | Version | Checker | Description | ||||||
---|---|---|---|---|---|---|---|---|---|
Astrée |
| error-information-unused | Fully checked | ||||||
Axivion Bauhaus Suite |
| CertC-EXP12 | Fully implemented | ||||||
CodeSonar |
| LANG.FUNCS.IRV | Ignored return value | ||||||
Compass/ROSE | |||||||||
| CHECKED_RETURN | Finds inconsistencies in how function call return values are handled. Coverity Prevent cannot discover all violations of this recommendation, so further verification is necessary | |||||||
Cppcheck |
| leakReturnValNotUsed, ignoredReturnValue | Return value of memory allocation function is not used. Ignored return value from function when configuration says it must be used. See the chapter "Library configuration" in the cppcheck manual | ||||||
| CC2.EXP12 | Fully implemented | |||||||
Helix QAC |
| C3200 | |||||||
Klocwork |
| MISRA.FUNC.UNUSEDRET.2012 | |||||||
LDRA tool suite |
| 382 S | Fully implemented | ||||||
Parasoft C/C++test |
| CERT_C-EXP12-a | The value returned by a function having non-void return type shall be used | ||||||
PC-lint Plus |
| 534 | Fully supported | ||||||
| CERT C: Rec. EXP12-C | Checks for situations where returned value of a sensitive function is not checked (rec. fully covered) | |||||||
PVS-Studio |
| V530, V698, V757, V797 | |||||||
RuleChecker |
| error-information-unused | Partially checked | ||||||
Splint |
|
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
Other Languages
...
Related Guidelines
...
Other Languages
...
...
...
...
References
...
methods | |
ISO/IEC TR 24772:2013 | Passing Parameters and Return Values [CSJ] |
MITRE CWE | CWE-754, Improper check for unusual or exceptional conditions |
Bibliography
...
...
2011] | Subclause 6.8.3, |
...
"Expression and Null Statements" |
...
and null statements" \[[ISO/IEC PDTR 24772|AA. C References#ISO/IEC PDTR 24772]\] "CSJ Passing Parameters and Return Values"EXP11-C. Do not apply operators expecting one type to data of an incompatible type 03. Expressions (EXP) EXP30-C. Do not depend on order of evaluation between sequence points