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
|
// RUN: %check_clang_tidy %s bugprone-suspicious-memory-comparison %t \
// RUN: -- -- -target x86_64-unknown-unknown
namespace std {
typedef __SIZE_TYPE__ size_t;
int memcmp(const void *lhs, const void *rhs, size_t count);
} // namespace std
namespace sei_cert_example_oop57_cpp {
class C {
int i;
public:
virtual void f();
};
void f(C &c1, C &c2) {
if (!std::memcmp(&c1, &c2, sizeof(C))) {
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: comparing object representation of non-standard-layout type 'sei_cert_example_oop57_cpp::C'; consider using a comparison operator instead
}
}
} // namespace sei_cert_example_oop57_cpp
namespace inner_padding_64bit_only {
struct S {
int x;
int *y;
};
void test() {
S a, b;
std::memcmp(&a, &b, sizeof(S));
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: comparing object representation of type 'inner_padding_64bit_only::S' which does not have a unique object representation; consider comparing the members of the object manually
}
} // namespace inner_padding_64bit_only
namespace padding_in_base {
class Base {
char c;
int i;
};
class Derived : public Base {};
class Derived2 : public Derived {};
void testDerived() {
Derived a, b;
std::memcmp(&a, &b, sizeof(Base));
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: comparing object representation of type 'padding_in_base::Derived' which does not have a unique object representation; consider comparing the members of the object manually
std::memcmp(&a, &b, sizeof(Derived));
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: comparing object representation of type 'padding_in_base::Derived' which does not have a unique object representation; consider comparing the members of the object manually
}
void testDerived2() {
Derived2 a, b;
std::memcmp(&a, &b, sizeof(Base));
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: comparing object representation of type 'padding_in_base::Derived2' which does not have a unique object representation; consider comparing the members of the object manually
std::memcmp(&a, &b, sizeof(Derived2));
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: comparing object representation of type 'padding_in_base::Derived2' which does not have a unique object representation; consider comparing the members of the object manually
}
} // namespace padding_in_base
namespace no_padding_in_base {
class Base {
int a, b;
};
class Derived : public Base {};
class Derived2 : public Derived {};
void testDerived() {
Derived a, b;
std::memcmp(&a, &b, sizeof(Base));
std::memcmp(&a, &b, sizeof(Derived));
}
void testDerived2() {
Derived2 a, b;
std::memcmp(&a, &b, sizeof(char));
std::memcmp(&a, &b, sizeof(Base));
std::memcmp(&a, &b, sizeof(Derived2));
}
} // namespace no_padding_in_base
namespace non_standard_layout {
class C {
private:
int x;
public:
int y;
};
void test() {
C a, b;
std::memcmp(&a, &b, sizeof(C));
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: comparing object representation of non-standard-layout type 'non_standard_layout::C'; consider using a comparison operator instead
}
} // namespace non_standard_layout
namespace static_ignored {
struct S {
static char c;
int i;
};
void test() {
S a, b;
std::memcmp(&a, &b, sizeof(S));
}
} // namespace static_ignored
namespace operator_void_ptr {
struct S {
operator void *() const;
};
void test() {
S s;
std::memcmp(s, s, sizeof(s));
}
} // namespace operator_void_ptr
namespace empty_struct {
struct S {};
void test() {
S a, b;
std::memcmp(&a, &b, sizeof(S));
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: comparing object representation of type 'empty_struct::S' which does not have a unique object representation; consider comparing the members of the object manually
}
} // namespace empty_struct
namespace empty_field {
struct Empty {};
struct S {
Empty e;
};
void test() {
S a, b;
std::memcmp(&a, &b, sizeof(S));
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: comparing object representation of type 'empty_field::S' which does not have a unique object representation; consider comparing the members of the object manually
}
} // namespace empty_field
namespace no_unique_address_attribute {
struct Empty {};
namespace no_padding {
struct S {
char c;
[[no_unique_address]] Empty e;
};
void test() {
S a, b;
std::memcmp(&a, &b, sizeof(S));
}
} // namespace no_padding
namespace multiple_empties_same_type {
struct S {
char c;
[[no_unique_address]] Empty e1, e2;
};
void test() {
S a, b;
std::memcmp(&a, &b, sizeof(S));
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: comparing object representation of type 'no_unique_address_attribute::multiple_empties_same_type::S' which does not have a unique object representation; consider comparing the members of the object manually
}
} // namespace multiple_empties_same_type
namespace multiple_empties_different_types {
struct Empty2 {};
struct S {
char c;
[[no_unique_address]] Empty e1;
[[no_unique_address]] Empty2 e2;
};
void test() {
S a, b;
std::memcmp(&a, &b, sizeof(S));
}
} // namespace multiple_empties_different_types
} // namespace no_unique_address_attribute
namespace alignment {
struct S {
char x;
alignas(sizeof(int)) char y[sizeof(int)];
};
void test() {
S a, b;
std::memcmp(&a, &b, sizeof(S));
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: comparing object representation of type 'alignment::S' which does not have a unique object representation; consider comparing the members of the object manually
}
} // namespace alignment
namespace no_warning_in_template {
template <typename T>
int compare(const T *l, const T *r) {
return std::memcmp(l, r, sizeof(T));
}
void test() {
int a, b;
compare(&a, &b);
}
} // namespace no_warning_in_template
namespace warning_in_template {
template <typename T>
int compare(const T *l, const T *r) {
return std::memcmp(l, r, sizeof(T));
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: comparing object representation of type 'float' which does not have a unique object representation; consider comparing the values manually
}
void test() {
float a, b;
compare(&a, &b);
}
} // namespace warning_in_template
|