Versions Compared

Key

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

...

Code Block
bgColor#ccccff
size_t nsize;
/* initialize nsize */
char *p2;
char *p = (char *)malloc(100);
if (p == NULL) {
  /* Handle Error */
}

/* ... */

p2 = NULL;
if (nsize != 0) {
  p2 = (char *)realloc(p, nsize);
}
if (p2 == NULL) {
  free(p);
  p = NULL;
  return NULL;
}
p = p2;

Automated Detection

The tool Compass/ROSE could detect can some violations of this rule by citing any usage of . Is particular, it warns when when the argument to malloc() where the argument is a variable that has not been previously compared compared against NULL, or is known at compile time to be != or > 0. Often malloc()'s argument will be a multiplication of a variable with a sizeof operator; in which case we still check the variable.

Risk Assessment

Allocating zero bytes can lead to abnormal program termination.

...