-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathtest2.cpp
More file actions
60 lines (44 loc) · 1 KB
/
test2.cpp
File metadata and controls
60 lines (44 loc) · 1 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
// semmle-extractor-options: -std=gnu++14
typedef unsigned long size_t;
void *malloc(size_t size);
void free(void *ptr);
void* operator new(size_t _Size, void *_Where);
// ---
template<typename T>
class MyTest2Class
{
public:
MyTest2Class()
{
int *a = new int;
free(a); // BAD
int *ptr_b = (int *)malloc(sizeof(int));
int *b = new(ptr_b) int;
free(b); // GOOD
c = new int;
free(c); // BAD
int *ptr_d = (int *)malloc(sizeof(int));
d = new(ptr_d) int;
free(d); // GOOD
}
int *c, *d;
};
MyTest2Class<int> mt2c_i;
// ---
void* operator new(size_t);
void operator delete(void*);
void test_operator_new()
{
void *ptr_new = new int;
void *ptr_opnew = ::operator new(sizeof(int));
void *ptr_malloc = malloc(sizeof(int));
delete ptr_new; // GOOD
::operator delete(ptr_new); // GOOD
free(ptr_new); // BAD
delete ptr_opnew; // GOOD
::operator delete(ptr_opnew); // GOOD
free(ptr_opnew); // BAD
delete ptr_malloc; // BAD
::operator delete(ptr_malloc); // BAD
free(ptr_malloc); // GOOD
}