Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Updated the two pointers + one integer examples to be somewhat more compelling

...

Noncompliant Code Example (Two Pointers + One Integer)

This In this noncompliant code example is noncompliant because , the value of n is incorrectly computed, allowing a possible write read past the end of the object referenced by p q:

Code Block
bgColor#FFcccc
#include <string.h>

 
void f4() {
  char p[40],;
  const char *q) {
  const = "Too short";
  size_t n = sizeof(p); 
  memcpy(p, q, n);
}

...

Compliant Solution (Two Pointers + One Integer)

...

Code Block
bgColor#ccccff
#include <string.h>
 
void f4() {
  char p[40],;
  const char *q, = "Too short";
  size_t n = sizeof(p) < strlen(q) { + 1 ? sizeof(p) : strlen(q) + 1;
  memcpy(p, q, n);
}

One Pointer + Two Integers

...