Versions Compared

Key

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

...

                        - (‘n’ > size of ‘p’ or size of ‘q’) || (‘p’ and ‘q’ are not compatible      compatible)                 // for func(p,q, n)

...

In this noncompliant code example, the size of 'n' could be greater than the size of *p. Also, the effective type of *p (int) is not same as the effective type of *q (float).

Code Block
bgColor#FFcccc
void f3(int *a) {

	float b = 3.14;
	const size_t n = sizeof(*b);
	void *p = a;
	void *q = &b;

	memcpy(p, q, n);
	/* More program code */

}

...

This compliant solution makes sure that the of 'n' is not greater the the minimum of effective sizes of *p and *q.

Code Block
bgColor#ccccff
void f3(int *a) {

	float b = 3.14;
	const size_t n = sizeof(*b);
	void *p = a;
	void *q = &b;

	if (n <= size(*p) && n <= size(*q)) {
		memcpy(p, q, n);
	}

	else {
		/* Handle Error */
	}

}

...