-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA15-1-4.cpp
More file actions
219 lines (156 loc) · 3.81 KB
/
A15-1-4.cpp
File metadata and controls
219 lines (156 loc) · 3.81 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
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
// Rule: A15-1-4
// Source line: 23836
// Original file: A15-1-4.cpp
//% $Id: A15-1-4.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <cstdint>
#include <memory>
#include <stdexcept>
extern std::uint32_t F1();
void FVeryBad() noexcept(false)
{
std::logic_error* e = new std::logic_error("Logic Error 1");
// ...
std::uint32_t i = F1();
if (i < 10)
{
throw(*e); // Non-compliant - fVeryBad() is not able to clean-up
// allocated memory
}
// ...
delete e;
}
void FBad() noexcept(false)
{
std::int32_t* x = new std::int32_t(0);
// ...
std::uint32_t i = F1();
if (i < 10)
{
throw std::logic_error("Logic Error 2"); // Non-compliant - exits from
// fBad() without cleaning-up
// allocated resources and
// causes a memory leak
}
else if (i < 20)
{
throw std::runtime_error("Runtime Error 3"); // Non-compliant - exits
// from fBad() without
// cleaning-up allocated
// resources and causes a
// memory leak
}
// ...
delete x;
// Deallocates claimed resource only in the end of fBad() scope
}
void FGood() noexcept(false)
{
std::int32_t* y = new std::int32_t(0);
// ...
std::uint32_t i = F1();
if (i < 10)
{
delete y; // Deletes allocated resource before throwing an exception
throw std::logic_error("Logic Error 4"); // Compliant - deleting y
// variable before exception
// leaves the fGood() scope
}
else if (i < 20)
{
delete y; // Deletes allocated resource before throwing an exception
throw std::runtime_error("Runtime Error 5"); // Compliant - deleting y
// variable before
// exception leaves the
// fGood() scope
}
else if (i < 30)
{
delete y; // Deletes allocated resource before throwing an exception
// again, difficult to maintain
throw std::invalid_argument(
"Invalid Argument 1"); // Compliant - deleting
// y variable before
// exception leaves the
// fGood() scope
}
// ...
delete y;
// Deallocates claimed resource also in the end of fGood() scope
}
void FBest() noexcept(false)
{
std::unique_ptr<std::int32_t> z = std::make_unique<std::int32_t>(0);
// ...
std::uint32_t i = F1();
if (i < 10)
{
throw std::logic_error("Logic Error 6");
// automatic variables, unique_ptrs, too
// Compliant - leaving the
// fBest() scope causes
// deallocation of all
}
else if (i < 20)
{
throw std::runtime_error("Runtime Error 3");
// Compliant - leaving the
// fBest() scope causes
// deallocation of all
// automatic variables,
// unique_ptrs, too
}
else if (i < 30)
{
throw std::invalid_argument(
"Invalid Argument 2"); // Compliant - leaving the fBest() scope
// causes deallocation of all automatic
// variables, unique_ptrs,
// too
}
// ...
// z is deallocated automatically here, too
}
class CRaii // Simple class that follows RAII pattern
{
public:
CRaii(std::int32_t* pointer) noexcept : x(pointer) {}
~CRaii()
{
delete x;
x = nullptr;
}
private:
std::int32_t* x;
};
void FBest2() noexcept(false)
{
CRaii a1(new std::int32_t(10));
// ...
std::uint32_t i = F1();
if (i < 10)
{
throw std::logic_error("Logic Error 7");
// Compliant - leaving the
// fBest2() scope causes a1
// variable deallocation
// automatically
}
else if (i < 20)
{
throw std::runtime_error("Runtime Error 4");
// Compliant - leaving the
// fBest2() scope causes
// a1 variable
// deallocation
// automatically
}
else if (i < 30)
{
throw std::invalid_argument(
"Invalid Argument 3"); // Compliant - leaving the fBest2() scope
// causes a1 variable deallocation
// automatically
}
// ...
// a1 is deallocated automatically here, too
}