You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 85 Next »

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 an integral part of the function's API.

Subclause 6.8.3 of the C Standard [ISO/IEC 9899:2011] states:

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. 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 ERR33-C. Detect and handle standard library errors.

Noncompliant Code Example

This noncompliant code example calls puts() and fails to check whether a write error occurs:

puts("foo");

However, puts() can fail and return EOF.

Compliant Solution

This compliant solution checks to make sure no output error occurred (see ERR33-C. Detect and handle standard library errors).

if (puts("foo") == EOF) {
  /* Handle error */
}

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. 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.

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 whitelist when automatic checkers are used.

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

Tool

Version

Checker

Description

CodeSonar8.1p0LANG.FUNCS.IRVIgnored return value
Compass/ROSE

 

 

 

Coverity

2017.07

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  2.15leakReturnValNotUsed, 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

ECLAIR

1.2

CC2.EXP12

Fully implemented

Klocwork2024.3

MISRA.FUNC.UNUSEDRET.2012
SV.RVT.RETVAL_NOTTESTED

 

LDRA tool suite9.7.1

382 S

Fully implemented

Parasoft C/C++test9.5CODSTA-122{a,b}Fully implemented
PRQA QA-C
Unable to render {include} The included page could not be found.
3200Fully implemented
Splint3.1.1

 

 

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Related Guidelines

Bibliography

[ISO/IEC 9899:2011]Subclause 6.8.3, "Expression and Null Statements"

 


  • No labels