V2628. MISRA. The pointer arguments to the Standard Library functions memcpy, memmove and memcmp should be pointers to qualified or unqualified versions of compatible types.
This diagnostic rule is based on the MISRA (Motor Industry Software Reliability Association) software development guidelines.
This diagnostic rule is relevant only for C.
Passing pointer arguments of incompatible types to the memcpy
, memmove
, and memcmp
functions may be a sign of an error.
Look at the following code example:
void foo(int s1[CMP_SIZE], float s2[CMP_SIZE])
{
memcpy(s1, s2, CMP_SIZE * sizeof(int));
}
In this example, the arguments of the memcpy
function have different types: s1
is int *
, and s2
is float *
. Note: even though the s1
and s2
parameters are declared as arrays, they are actually pointers due to the peculiarities of the C programming language.
The fixed code:
void foo(int s1[CMP_SIZE], int s2[CMP_SIZE])
{
memcpy(s1, s2, CMP_SIZE * sizeof(int));
}