CERT-EXP42-C
In this section:
Synopsis
Do not compare padding data.
Enabled by default
Yes
Severity/Certainty
Medium/High

Full description
Padding values are unspecified, attempting a byte-by-byte comparison between structures can lead to incorrect results.
Coding standards
- CERT EXP42-C
Do not compare padding data
Code examples
The following code example fails the check and will give a warning:
#include <string.h>
struct s {
char c;
int i;
char buffer[13];
};
void compare(const struct s *left, const struct s *right) {
if ((left && right) &&
(0 == memcmp(left, right, sizeof(struct s)))) {
/* ... */
}
}
The following code example passes the check and will not give a warning about this issue:
#include <string.h>
struct s {
char c;
int i;
char buffer[13];
};
void compare(const struct s *left, const struct s *right) {
if ((left && right) &&
(left->c == right->c) &&
(left->i == right->i) &&
(0 == memcmp(left->buffer, right->buffer, 13))) {
/* ... */
}
}