Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This recommendation encompasses ERR33-C. Detect and handle standard 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. 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 fputsasprintf() and , but fails to check whether a write error occurs:the string was successfully created.

Code Block
bgColor#ffcccc
langc
FILEvoid func(char* fname) {
 = /char* outputs file stream */
/* ... */
fputs("foo", f);

...

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

Code Block
bgColor#ccccff
langc
FILE* f = /* output file stream */
/* ... */
if (fputs("foo", f) == EOF) {
void func(char* name) {
  char* s = NULL;
  if (asprintf(&s,"Hello, %s!\n", name) < 0) {
    /* Handle error */
  }
  (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. 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, or Exception ERR33-C-EX1 in  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 whitelist when automatic checkers are used.

...

Tool

Version

Checker

Description

Astrée
Include Page
Astrée_V
Astrée_V

error-information-unused
error-information-unused-computed

Fully checked
Axivion Bauhaus Suite

Include Page
Axivion Bauhaus Suite_V
Axivion Bauhaus Suite_V

CertC-EXP12Fully implemented
CodeSonar
Include Page
CodeSonar_V
CodeSonar_V
LANG.FUNCS.IRVIgnored return value
Compass/ROSE




Coverity

Include Page
Coverity_V
Coverity_V

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
 
Include Page
Cppcheck_V
Cppcheck_V
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

ECLAIR

Include Page
ECLAIR_V
ECLAIR_V

CC2.EXP12

Fully implemented

Helix QAC

Include Page
Helix QAC_V
Helix QAC_V

C3200
Klocwork
Include Page
Klocwork_V
Klocwork_V

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


LDRA tool suite
Include Page
LDRA_V
LDRA_V

382 S

Fully implemented

Parasoft C/C++test
Include Page
Parasoft_V
Parasoft_V

CERT_C-EXP12-a
CERT_C-EXP12-b

The value returned by a function having non-void return type shall be used
The value returned by a function having non-void return type shall be used

PC-lint Plus

Include Page
PC-lint Plus_V
PC-lint Plus_V

534

Fully supported

Polyspace Bug Finder

Include Page
Polyspace Bug Finder_V
Polyspace Bug Finder_V

CERT C: Rec. EXP12-CChecks for situations where returned Returned value of a sensitive function not checkedSensitive functions called without checking for unexpected return values and errorsPRQA QA-C
Include Page
PRQA QA-C_vPRQA QA-C_v3200Fully implementedis not checked (rec. fully covered)
PVS-Studio

Include Page
PVS-Studio_V
PVS-Studio_V

V530, V698, V757, V797
RuleChecker
Include Page
RuleChecker_V
RuleChecker_V

error-information-unused

Partially checked
Splint
Include Page
Splint_V
Splint_V



...