Versions Compared

Key

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

Redundant testing by caller and by callee as a style of defensive programming is largely discredited in the C and C++ communitycommunities, the main problem being performance. The usual discipline in C and C++ is to require validation on only one side of each interface.

...

Code Block
bgColor#ccccff
langc
/* Sets some internal state in the library */
extern errno_t setfile(FILE *file);

/* Performs some action using the file passed earlier */
extern errno_t usefile(void);

static FILE *myFile;

errno_t setfile(FILE *file) {
 if (file && !ferror(file) && !feof(file)) {
    myFile = file;
    return 0;
  }

  /* Error safety: leave myFile unchanged */
  return -1;
}

errno_t usefile(void) {
  if (!myFile) return -1;

    /*
     * Perform other checks if needed,; return 
     * error condition.
     */

    /* Perform some action here */
    return 0;
}

...

Recommendation

Severity

Likelihood

Remediation Cost

Priority

Level

API00-C

mediumMedium

unlikelyUnlikely

highHigh

P2

L3

Automated Detection

Tool

Version

Checker

Description

LDRA tool suite

Include Page
LDRA_V
LDRA_V

 

 

Related Vulnerabilities

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

...