Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: added Rose checker algorithm

...

Code Block
bgColor#ccccff
size_t nsize;
/* initialize nsize */
char *p2;
char *p = (char *)malloc(100);
/* ... */

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 violations of this rule by citing any usage of malloc() where the argument is a variable that has not been previously compared 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.

...