MISRA C++:2023 Rule 24.5.2
The C++ Standard Library functions memcpy
,
memmove
, memcmp
from
<cstring>
shall not be used
Since R2024b
Description
Rule Definition
The C++ Standard Library functions memcpy
,
memmove
, memcmp
from
<cstring>
shall not be used.
Rationale
The functions memcpy()
, memmove()
, and
memcmp()
from the <cstring>
or
<string.h>
headers are intended to operate on memory at a bit
level. Use of these functions can result in undefined or unexpected behavior:
Using
memcpy()
to copy a block of memory to another block results in undefined behavior if the blocks overlap.Using
memmove()
ormemcpy()
on two blocks of memory can result in undefined behavior if the blocks are potentially overlapping.Using
memmove()
ormemcpy()
on two blocks of memory can result in undefined behavior if the blocks are not trivially copyable.Using
memcmp()
to compare memory blocks can result in unexpected behavior. For example, two objects that are logically equal can fail an equality test done bymemcmp
because of differences in how the objects are represented in memory. Specifically:0.0
and-0.0
can fail an equality check because these floating-point numbers are represented differently in the memory.Identical class objects can fail an equality check because of differences in padding bits or unions having different active members.
Buffers or containers that contain identical content can fail an quality check if the content does not occupy the whole buffer and the irrelevant parts of the buffers differ.
Because using the functions memcpy()
,
memmove()
, and memcmp()
can result in undefined
or unexpected behavior, avoid using these functions.
Polyspace Implementation
Polyspace® reports a violation of this rule if your code uses the names
memcpy
, memmove
, memcmp
from
the header files <cstring>
or <string.h>
.
For example, any of these uses results in a violation of this rule:
You call these functions.
You take the address of these functions to store in a
std::function
object or to assign to a function pointer.You use a macro that contains these names.
User-defined functions named memcpy
, memmove
,
or memcmp
are not reported as violations.
Troubleshooting
If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Strings Library |
Category: Required |
Version History
Introduced in R2024b