Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Added the 3rd solution (revised)

...

Note: A possibility of this code being safe would be on architectures where sizeof (*int) is equal to sizeof (*float).

Compliant Solution

...

This compliant solution makes sure that the of 'n' is not greater the the minimum of effective sizes of *p and *q . (write code for compatibility)and the effective types of the two pointers is also same (float).

Code Block
bgColor#ccccff
void f3(intfloat *a, size_t val) {

	float b = 3.14;
	const size_t n = val;
	void *p = a;
	void *q = &b;


	if( (n > sizeof(a)) || (n > sizeof(b)) ) {
		/* Handle error */
	}
	else {
		memcpy(p, q, n);
		/* More program code */
	}

}

Noncompliant Code Example

...

Code Block
bgColor#FFcccc
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' (wchar_t) is same as the type of 'T' (wchar_t). Also, the value of 'n' is not less than the size of 'T'.

...