memcmp compares data stored in strings
after the null terminator
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))
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.
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.
| Group: Programming |
| Language: C | C++ |
| Default: On for handwritten code, off for generated code |
Command-Line Syntax: MEMCMP_STRINGS |
| Impact: Medium |
| CWE ID: 188 |