Versions Compared

Key

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

...

Code Block
bgColor#FFCCCC
langcpp

for (p = head; p != NULL; p = p->next)
    free(p);

...

Code Block
bgColor#ccccff
langcpp

for (p = head; p != NULL; p = q) {
  q = p->next;
  free(p);
}
head = NULL;

...

Code Block
bgColor#FFCCCC
langcpp

int main(int argc, const char *argv[]) {
  char *buff;

  buff = new char[BUFSIZ];
  // ...
  delete[] buff;
  // ...
  strncpy(buff, argv[1], BUFSIZ-1);
}

...

Code Block
bgColor#ccccff
langcpp

int main(int argc, const char *argv[]) {
  char *buff;

  buff = new char[BUFSIZ];
  // ...
  strncpy(buff, argv[1], BUFSIZ-1);
  // ...
  delete[] buff;
  buff = nullptr;
}

...

Code Block
bgColor#ffcccc
langcpp

int main(int argc, const char *argv[]) {
  const char *s = "";

  if (1 < argc) {
    std::unique_ptr<char[]> buff (new char [BUFSIZ]);
    // ...
    s = strncpy(buff.get(), argv[1], BUFSIZ-1);
  }

  std::cout << s << '\n';
}

...

Code Block
bgColor#ccccff
langcpp

int main(int argc, const char *argv[]) {
  std::unique_ptr<char[]> buff;
  const char *s = "";

  if (1 < argc) {
    buff.reset(new char [BUFSIZ]);
    // ...
    s = strncpy(buff.get(), argv[1], BUFSIZ-1);
  }

  std::cout << s << '\n';
}

...

Compass/ROSE can detect violations of the rule.

The Coverity Prevent Version 5.0 Coverity Code Advisor version 7.5 can detect violations of this rule.  The USE_AFTER_FREE checker can detect the specific instances where Memory memory is deallocated more than once or Read/Write to target of a freed pointer.

...