Versions Compared

Key

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

...

Code Block
bgColor#FFcccc
langc
char *secret;
const int SECRET_MAX = /* ... */
/* Initialize secret to a null-terminated byte string, 
   of less than SECRETSIZE_MAX chars */

size_t size = strlen(secret);
if (size >= SECRET_MAX) {

  /* Handle error */
}

char *new_secret;
new_secret = (char *)malloc(size+1);
if (!new_secret) {
  /* Handle error */
}
strcpy(new_secret, secret);

/* Process new_secret... */

free(new_secret);
new_secret = NULL;

...

Code Block
bgColor#ccccff
langc
harchar *secret;

const int SECRET_MAX = /* ... */
/* Initialize secret to a null-terminated byte string, 
   of less than SECRETSIZE_MAX chars */

size_t size = strlen(secret);
if (size >= SECRET_MAX) {
  /* Handle error */
}

char *new_secret;
/* Use calloc() to zero-out allocated space */
new_secret = (char *)calloc(size+1, sizeof(char));
if (!new_secret) {
  /* Handle error */
}
strcpy(new_secret, secret);

/* Process new_secret... */

/* Sanitize memory  */
memset_s(new_secret, '\0', size);
free(new_secret);
new_secret = NULL;

...

Tool

Version

Checker

Description

CodeSonar
Include Page
CodeSonar_V
CodeSonar_V
(customization)Users can add a custom check for use of realloc().
Compass/ROSE



Could detect possible violations of this rule by first flagging any usage of realloc(). Also, it could flag any usage of free that is not preceded by code to clear out the preceding memory, using memset. This heuristic is imperfect because it flags all possible data leaks, not just leaks of "sensitive" data, because ROSE cannot tell which data is sensitive

Helix QAC

Include Page
Helix QAC_V
Helix QAC_V

C5010
LDRA tool suite
Include Page
LDRA_V
LDRA_V
44 SEnhanced Enforcement
Parasoft C/C++test

Include Page
Parasoft_V
Parasoft_V

CERT_C-MEM03-aSensitive data should be cleared before being deallocated
Polyspace Bug Finder

Include Page
Polyspace Bug Finder_V
Polyspace Bug Finder_V

CERT C: Rec. MEM03-C


Checks for:

R2016a

  • Sensitive heap memory not cleared before release
  • Uncleared sensitive data in stack

Sensitive data not cleared or released by memory routine

Variable in stack is not cleared and contains sensitive data

Rec. partially covered.

PVS-Studio

Include Page
PVS-Studio_V
PVS-Studio_V

V1072PRQA QA-C
Include Page
PRQA QA-C_vPRQA QA-C_v5010Partially implemented

Related Vulnerabilities

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

...