-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathleaking.cpp
More file actions
62 lines (48 loc) · 1.66 KB
/
Copy pathleaking.cpp
File metadata and controls
62 lines (48 loc) · 1.66 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
#include <iostream>
#include <memory_resource>
struct Tracker {
int Id;
char Buffer[16];
Tracker(int i) : Id(i) {}
};
void Leak() {
// Leak 1: We allocate a struct and never deallocate it.
Tracker* Lost = new Tracker(42);
// Leak 2: We buffer overflow: writing 1 char past the end of the array.
for (int i = 0; i <= 16; ++i) {
Lost->Buffer[i] = 'a';
}
std::cout << "Leaked object ID: " << Lost->Id << std::endl;
// To avoid the first leak, you must manually destroy and deallocate
// delete Lost;
};
class PrintingResource : public std::pmr::memory_resource {
protected:
void* do_allocate(std::size_t bytes, std::size_t alignment) override {
std::cout << "Allocating: " << bytes << " bytes\n";
return std::pmr::new_delete_resource()->allocate(bytes, alignment);
}
void do_deallocate(void* p, std::size_t bytes, std::size_t alignment) override {
std::cout << "Freeing: " << bytes << " bytes\n";
std::pmr::new_delete_resource()->deallocate(p, bytes, alignment);
}
bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override {
return this == &other;
}
};
void PMRLeak() {
PrintingResource res;
std::pmr::polymorphic_allocator<Tracker> alloc(&res);
// This replaces 'new Tracker(42)'
Tracker* t = alloc.allocate(1); // Reserves memory
alloc.construct(t, 42); // Calls the constructor
std::cout << "Tracker ID: " << t->Id << "\n";
// To avoid the leak, you must manually destroy and deallocate
// alloc.destroy(t);
// alloc.deallocate(t, 1);
}
int main() {
Leak();
PMRLeak();
return 0;
}