...
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 */ } |
Compliant Solution // (check this)
This compliant solution makes sure that the of 'n' is not greater the the minimum of effective sizes of *p and *q.
...
In this noncompliant code example, the size value of 'n' could be is greater than the size of *p. Also, the effective type of *p (int'T' i.e. sizeof (wchar_t). But, the derived type of expression 'n' (wchar_t *) is not same as the effective type of *q (float)'T' i.e. wchar_t.
Code Block | ||
---|---|---|
| ||
wchar_t *f7() { const wchar_t *p = L"Hello, World!"; const size_t n = sizeof(p) * (wcslen(p) + 1); wchar_t *q = (wchar_t *)malloc(n); return q; } |
Compliant Solution
This compliant solution makes sure that the derived type of 'n' is not greater the the minimum of effective sizes of *p and *q(wchar_t) is same as the type of 'T' (wchar_t). Also, the value of 'n' is not less than the size of 'T'.
Code Block | ||
---|---|---|
| ||
wchar_t *f7() {
const wchar_t *p = L"Hello, World!";
const size_t n = sizeof(wchar_t) * (wcslen(p) + 1);
wchar_t *q = (wchar_t *)malloc(n);
return q;
}
|
...