DeleteWithNonVirtualDtor.cpp
6.23 KB
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
// RUN: %clang_cc1 -analyze -analyzer-checker=alpha.cplusplus.DeleteWithNonVirtualDtor -std=c++11 -verify -analyzer-output=text %s
struct Virtual {
virtual ~Virtual() {}
};
struct VDerived : public Virtual {};
struct NonVirtual {
~NonVirtual() {}
};
struct NVDerived : public NonVirtual {};
struct NVDoubleDerived : public NVDerived {};
struct Base {
virtual void destroy() = 0;
};
class PrivateDtor final : public Base {
public:
void destroy() { delete this; }
private:
~PrivateDtor() {}
};
struct ImplicitNV {
virtual void f();
};
struct ImplicitNVDerived : public ImplicitNV {};
NVDerived *get();
NonVirtual *create() {
NonVirtual *x = new NVDerived(); // expected-note{{Conversion from derived to base happened here}}
return x;
}
void sink(NonVirtual *x) {
delete x; // expected-warning{{Destruction of a polymorphic object with no virtual destructor}}
// expected-note@-1{{Destruction of a polymorphic object with no virtual destructor}}
}
void sinkCast(NonVirtual *y) {
delete reinterpret_cast<NVDerived*>(y);
}
void sinkParamCast(NVDerived *z) {
delete z;
}
void singleDerived() {
NonVirtual *sd;
sd = new NVDerived(); // expected-note{{Conversion from derived to base happened here}}
delete sd; // expected-warning{{Destruction of a polymorphic object with no virtual destructor}}
// expected-note@-1{{Destruction of a polymorphic object with no virtual destructor}}
}
void singleDerivedArr() {
NonVirtual *sda = new NVDerived[5]; // expected-note{{Conversion from derived to base happened here}}
delete[] sda; // expected-warning{{Destruction of a polymorphic object with no virtual destructor}}
// expected-note@-1{{Destruction of a polymorphic object with no virtual destructor}}
}
void doubleDerived() {
NonVirtual *dd = new NVDoubleDerived(); // expected-note{{Conversion from derived to base happened here}}
delete (dd); // expected-warning{{Destruction of a polymorphic object with no virtual destructor}}
// expected-note@-1{{Destruction of a polymorphic object with no virtual destructor}}
}
void assignThroughFunction() {
NonVirtual *atf = get(); // expected-note{{Conversion from derived to base happened here}}
delete atf; // expected-warning{{Destruction of a polymorphic object with no virtual destructor}}
// expected-note@-1{{Destruction of a polymorphic object with no virtual destructor}}
}
void assignThroughFunction2() {
NonVirtual *atf2;
atf2 = get(); // expected-note{{Conversion from derived to base happened here}}
delete atf2; // expected-warning{{Destruction of a polymorphic object with no virtual destructor}}
// expected-note@-1{{Destruction of a polymorphic object with no virtual destructor}}
}
void createThroughFunction() {
NonVirtual *ctf = create(); // expected-note{{Calling 'create'}}
// expected-note@-1{{Returning from 'create'}}
delete ctf; // expected-warning {{Destruction of a polymorphic object with no virtual destructor}}
// expected-note@-1{{Destruction of a polymorphic object with no virtual destructor}}
}
void deleteThroughFunction() {
NonVirtual *dtf = new NVDerived(); // expected-note{{Conversion from derived to base happened here}}
sink(dtf); // expected-note{{Calling 'sink'}}
}
void singleCastCStyle() {
NVDerived *sccs = new NVDerived();
NonVirtual *sccs2 = (NonVirtual*)sccs; // expected-note{{Conversion from derived to base happened here}}
delete sccs2; // expected-warning{{Destruction of a polymorphic object with no virtual destructor}}
// expected-note@-1{{Destruction of a polymorphic object with no virtual destructor}}
}
void doubleCastCStyle() {
NonVirtual *dccs = new NVDerived();
NVDerived *dccs2 = (NVDerived*)dccs;
dccs = (NonVirtual*)dccs2; // expected-note{{Conversion from derived to base happened here}}
delete dccs; // expected-warning{{Destruction of a polymorphic object with no virtual destructor}}
// expected-note@-1{{Destruction of a polymorphic object with no virtual destructor}}
}
void singleCast() {
NVDerived *sc = new NVDerived();
NonVirtual *sc2 = reinterpret_cast<NonVirtual*>(sc); // expected-note{{Conversion from derived to base happened here}}
delete sc2; // expected-warning{{Destruction of a polymorphic object with no virtual destructor}}
// expected-note@-1{{Destruction of a polymorphic object with no virtual destructor}}
}
void doubleCast() {
NonVirtual *dd = new NVDerived();
NVDerived *dd2 = reinterpret_cast<NVDerived*>(dd);
dd = reinterpret_cast<NonVirtual*>(dd2); // expected-note {{Conversion from derived to base happened here}}
delete dd; // expected-warning {{Destruction of a polymorphic object with no virtual destructor}}
// expected-note@-1{{Destruction of a polymorphic object with no virtual destructor}}
}
void implicitNV() {
ImplicitNV *invd = new ImplicitNVDerived(); // expected-note{{Conversion from derived to base happened here}}
delete invd; // expected-warning{{Destruction of a polymorphic object with no virtual destructor}}
// expected-note@-1{{Destruction of a polymorphic object with no virtual destructor}}
}
void doubleDecl() {
ImplicitNV *dd1, *dd2;
dd1 = new ImplicitNVDerived(); // expected-note{{Conversion from derived to base happened here}}
delete dd1; // expected-warning{{Destruction of a polymorphic object with no virtual destructor}}
// expected-note@-1{{Destruction of a polymorphic object with no virtual destructor}}
}
void virtualBase() {
Virtual *vb = new VDerived();
delete vb; // no-warning
}
void notDerived() {
NonVirtual *nd = new NonVirtual();
delete nd; // no-warning
}
void notDerivedArr() {
NonVirtual *nda = new NonVirtual[3];
delete[] nda; // no-warning
}
void cast() {
NonVirtual *c = new NVDerived();
delete reinterpret_cast<NVDerived*>(c); // no-warning
}
void deleteThroughFunction2() {
NonVirtual *dtf2 = new NVDerived();
sinkCast(dtf2); // no-warning
}
void deleteThroughFunction3() {
NVDerived *dtf3;
dtf3 = new NVDerived();
sinkParamCast(dtf3); // no-warning
}
void stackVar() {
NonVirtual sv2;
delete &sv2; // no-warning
}
// Deleting a polymorphic object with a non-virtual dtor
// is not a problem if it is referenced by its precise type.
void preciseType() {
NVDerived *pt = new NVDerived();
delete pt; // no-warning
}
void privateDtor() {
Base *pd = new PrivateDtor();
pd->destroy(); // no-warning
}