1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
// RUN: %check_clang_tidy %s bugprone-suspicious-memory-comparison %t \
// RUN: -- -- -target x86_64-unknown-unknown -std=c99
typedef __SIZE_TYPE__ size_t;
int memcmp(const void *lhs, const void *rhs, size_t count);
// Examples from cert rule exp42-c
struct S {
char c;
int i;
char buffer[13];
};
void exp42_c_noncompliant(const struct S *left, const struct S *right) {
if ((left && right) && (0 == memcmp(left, right, sizeof(struct S)))) {
// CHECK-MESSAGES: :[[@LINE-1]]:32: warning: comparing object representation of type 'struct S' which does not have a unique object representation; consider comparing the members of the object manually
}
}
void exp42_c_compliant(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))) {
}
}
#pragma pack(push, 1)
struct Packed_S {
char c;
int i;
char buffer[13];
};
#pragma pack(pop)
void compliant_packed(const struct Packed_S *left,
const struct Packed_S *right) {
if ((left && right) && (0 == memcmp(left, right, sizeof(struct Packed_S)))) {
// no-warning
}
}
// Examples from cert rule flp37-c
struct S2 {
int i;
float f;
};
int flp37_c_noncompliant(const struct S2 *s1, const struct S2 *s2) {
if (!s1 && !s2)
return 1;
else if (!s1 || !s2)
return 0;
return 0 == memcmp(s1, s2, sizeof(struct S2));
// CHECK-MESSAGES: :[[@LINE-1]]:15: warning: comparing object representation of type 'struct S2' which does not have a unique object representation; consider comparing the members of the object manually
}
int flp37_c_compliant(const struct S2 *s1, const struct S2 *s2) {
if (!s1 && !s2)
return 1;
else if (!s1 || !s2)
return 0;
return s1->i == s2->i && s1->f == s2->f;
// no-warning
}
void Test_Float(void) {
float a, b;
memcmp(&a, &b, sizeof(float));
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: comparing object representation of type 'float' which does not have a unique object representation; consider comparing the values manually
}
void TestArray_Float(void) {
float a[3], b[3];
memcmp(a, b, sizeof(a));
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: comparing object representation of type 'float' which does not have a unique object representation; consider comparing the values manually
}
struct PredeclaredType;
void Test_PredeclaredType(const struct PredeclaredType *lhs,
const struct PredeclaredType *rhs) {
memcmp(lhs, rhs, 1); // no-warning: predeclared type
}
struct NoPadding {
int x;
int y;
};
void Test_NoPadding(void) {
struct NoPadding a, b;
memcmp(&a, &b, sizeof(struct NoPadding));
}
void TestArray_NoPadding(void) {
struct NoPadding a[3], b[3];
memcmp(a, b, 3 * sizeof(struct NoPadding));
}
struct TrailingPadding {
int i;
char c;
};
void Test_TrailingPadding(void) {
struct TrailingPadding a, b;
memcmp(&a, &b, sizeof(struct TrailingPadding));
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: comparing object representation of type 'struct TrailingPadding' which does not have a unique object representation; consider comparing the members of the object manually
memcmp(&a, &b, sizeof(int)); // no-warning: not comparing entire object
memcmp(&a, &b, 2 * sizeof(int));
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: comparing object representation of type 'struct TrailingPadding' which does not have a unique object representation; consider comparing the members of the object manually
}
struct TrailingPadding2 {
int i[2];
char c;
};
void Test_TrailingPadding2(void) {
struct TrailingPadding2 a, b;
memcmp(&a, &b, 2 * sizeof(int)); // no-warning: not comparing entire object
memcmp(&a, &b, sizeof(struct TrailingPadding2));
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: comparing object representation of type 'struct TrailingPadding2' which does not have a unique object representation; consider comparing the members of the object manually
}
void Test_UnknownCount(size_t count) {
struct TrailingPadding a, b;
memcmp(&a, &b, count); // no-warning: unknown count value
}
void Test_ExplicitVoidCast(void) {
struct TrailingPadding a, b;
memcmp((void *)&a, (void *)&b,
sizeof(struct TrailingPadding)); // no-warning: explicit cast
}
void TestArray_TrailingPadding(void) {
struct TrailingPadding a[3], b[3];
memcmp(a, b, 3 * sizeof(struct TrailingPadding));
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: comparing object representation of type 'struct TrailingPadding' which does not have a unique object representation; consider comparing the members of the object manually
}
struct InnerPadding {
char c;
int i;
};
void Test_InnerPadding(void) {
struct InnerPadding a, b;
memcmp(&a, &b, sizeof(struct InnerPadding));
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: comparing object representation of type 'struct InnerPadding' which does not have a unique object representation; consider comparing the members of the object manually
}
struct Bitfield_TrailingPaddingBytes {
int x : 10;
int y : 6;
};
void Test_Bitfield_TrailingPaddingBytes(void) {
struct Bitfield_TrailingPaddingBytes a, b;
memcmp(&a, &b, sizeof(struct S));
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: comparing object representation of type 'struct Bitfield_TrailingPaddingBytes' which does not have a unique object representation; consider comparing the members of the object manually
}
struct Bitfield_TrailingPaddingBits {
int x : 10;
int y : 20;
};
void Test_Bitfield_TrailingPaddingBits(void) {
struct Bitfield_TrailingPaddingBits a, b;
memcmp(&a, &b, sizeof(struct Bitfield_TrailingPaddingBits));
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: comparing object representation of type 'struct Bitfield_TrailingPaddingBits' which does not have a unique object representation; consider comparing the members of the object manually
}
struct Bitfield_InnerPaddingBits {
char x : 2;
int : 0;
char y : 8;
};
void Test_Bitfield_InnerPaddingBits(void) {
struct Bitfield_InnerPaddingBits a, b;
memcmp(&a, &b, sizeof(struct Bitfield_InnerPaddingBits));
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: comparing object representation of type 'struct Bitfield_InnerPaddingBits' which does not have a unique object representation; consider comparing the members of the object manually
}
struct Bitfield_NoPadding {
int i : 10;
int j : 10;
int k : 10;
int l : 2;
};
_Static_assert(sizeof(struct Bitfield_NoPadding) == sizeof(int),
"Bit-fields should line up perfectly");
void Test_Bitfield_NoPadding(void) {
struct Bitfield_NoPadding a, b;
memcmp(&a, &b, sizeof(struct Bitfield_NoPadding)); // no-warning
}
struct Bitfield_TrailingUnnamed {
int i[2];
int : 0;
};
void Bitfield_TrailingUnnamed(void) {
struct Bitfield_TrailingUnnamed a, b;
memcmp(&a, &b, 2 * sizeof(int)); // no-warning
memcmp(&a, &b, sizeof(struct Bitfield_TrailingUnnamed)); // no-warning
}
struct PaddingAfterUnion {
union {
unsigned short a;
short b;
} x;
int y;
};
void Test_PaddingAfterUnion(void) {
struct PaddingAfterUnion a, b;
memcmp(&a, &b, sizeof(struct PaddingAfterUnion));
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: comparing object representation of type 'struct PaddingAfterUnion' which does not have a unique object representation; consider comparing the members of the object manually
}
struct Union_NoPadding {
union {
int a;
unsigned int b;
} x;
int y;
};
void Test_Union_NoPadding(void) {
struct Union_NoPadding a, b;
memcmp(&a, &b, 2 * sizeof(int));
memcmp(&a, &b, sizeof(struct Union_NoPadding));
}
union UnionWithPaddingInNestedStruct {
int i;
struct {
int i;
char c;
} x;
};
void Test_UnionWithPaddingInNestedStruct(void) {
union UnionWithPaddingInNestedStruct a, b;
memcmp(&a, &b, sizeof(union UnionWithPaddingInNestedStruct));
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: comparing object representation of type 'union UnionWithPaddingInNestedStruct' which does not have a unique object representation; consider comparing the members of the object manually
}
struct PaddingInNested {
struct TrailingPadding x;
char y;
};
void Test_PaddingInNested(void) {
struct PaddingInNested a, b;
memcmp(&a, &b, sizeof(struct PaddingInNested));
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: comparing object representation of type 'struct PaddingInNested' which does not have a unique object representation; consider comparing the members of the object manually
}
struct PaddingAfterNested {
struct {
char a;
char b;
} x;
int y;
};
void Test_PaddingAfterNested(void) {
struct PaddingAfterNested a, b;
memcmp(&a, &b, sizeof(struct PaddingAfterNested));
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: comparing object representation of type 'struct PaddingAfterNested' which does not have a unique object representation; consider comparing the members of the object manually
}
struct AtomicMember {
_Atomic(int) x;
};
void Test_AtomicMember(void) {
// FIXME: this is a false positive as the list of objects with unique object
// representations is incomplete.
struct AtomicMember a, b;
memcmp(&a, &b, sizeof(struct AtomicMember));
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: comparing object representation of type 'struct AtomicMember' which does not have a unique object representation; consider comparing the members of the object manually
}
|