Versions Compared

Key

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

...

Noncompliant Code Example

This In noncompliant code example assigns a value greater than the size of dynamic memory to the effective type of *p is float while the derived type of the expression 'n' which is then passed to the memset()is int.

Code Block
bgColor#FFcccc
void f2() {
float a[4];
const size_t n= sizeof(int) * 4;
void *p = a;


memset(p, 0, n);
/* More program code */

}

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 value The derived type of 'n' is not greater the size of the dynamic memory pointed to by the pointer 'p':in this solution is also float.

Code Block
bgColor#ccccff
void f2() {
float a[4];
const size_t n= sizeof(float) * 4;
void *p = a;

memset(p, 0, n);
/* More program code */

}

...