Memory comparison of strings
memcmp
compares data stored in strings
after the null terminator
Description
This defect occurs when:
You compare two strings byte-by-byte with the
memcmp
function.The number of bytes compared is such that you compare meaningless data stored after the null terminator.
For instance:
memcmp(string1, string2, sizeof(string1))
Risk
The null terminator signifies the end of a string. Comparison of bytes after the null terminator is meaningless. You might reach the false conclusion that two strings are not equal, even if the bytes before the null terminator store the same value.
Fix
Use strcmp
for string comparison. The function
compares strings only up to the null terminator.
If you use memcmp
for a byte-by-byte comparison
of two strings, avoid comparison of bytes after the null terminator.
Determine the number of bytes to compare by using the strlen
function.
Examples
Result Information
Group: Programming |
Language: C | C++ |
Default: On for handwritten code, off for generated code |
Command-Line Syntax: MEMCMP_STRINGS |
Impact: Medium |
Version History
Introduced in R2017a
See Also
Find defects (-checkers)
| Memory comparison of padding data
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)