...
- (â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 | ||
---|---|---|
| ||
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 | ||
---|---|---|
| ||
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 */ } } |
...