Main Content
MISRA C:2012 Rule 21.14
The Standard Library function memcmp
shall not be used to
compare null terminated strings
Description
Rule Definition
The Standard Library function memcmp
shall not be
used to compare null terminated strings.
This rule comes from MISRA C™: 2012 Amendment 1.
Rationale
If memcmp
is used to compare two strings and the length of
either string is less than the number of bytes compared, the strings can appear
different even when they are logically the same. The characters after the null
terminator are compared even though they do not form part of the string.
For instance:
memcmp(string1, string2, sizeof(string1))
string1
is longer than
string2
.Troubleshooting
If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Standard libraries |
Category: Required |
AGC Category: Required |
Version History
Introduced in R2017a