Versions Compared

Key

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

...

C library functions that make changes to arrays or objects usually take at least two arguments: i.)  a pointer to the array/object ii.) an integer indicating the number of elements or bytes to be manipulated. If the arguments are supplied improperly during such a function call, the function may cause the pointer to not point to the object at all or point past the end of the object. This would lead to undefined behavior ( ... )

To make sure that this does not happen, programmer must keep in mind the following rules when using such functions:

...

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 */

}

Compliant Solution // (

...

need to work on this)

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)

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 */
	}

}

Noncompliant Code Example

...

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

ARR38-C

high

likely

medium

P18

L1

Related Guidelines

API00-C. Functions should validate their parameters

WG14 Document: N1579 - Rule 5.34 Forming Invalid pointers by library functions.

Bibliography

WG14 Document: N1579 - Rule 5.34 Forming Invalid pointers by library functions.