Skip to content

Commit c7aa5c1

Browse files
committed
CPP: Add a test of placement new for AV Rule 79.ql.
1 parent 4d46385 commit c7aa5c1

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

cpp/ql/test/query-tests/jsf/4.10 Classes/AV Rule 79/AV Rule 79.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,8 @@
1111
| ExternalOwners.cpp:49:3:49:20 | ... = ... | Resource a is acquired by class MyScreen but not released anywhere in this class. |
1212
| ListDelete.cpp:21:3:21:21 | ... = ... | Resource first is acquired by class MyThingColection but not released anywhere in this class. |
1313
| NoDestructor.cpp:23:3:23:20 | ... = ... | Resource n is acquired by class MyClass5 but not released anywhere in this class. |
14+
| PlacementNew.cpp:36:3:36:36 | ... = ... | Resource p1 is acquired by class MyTestForPlacementNew but not released anywhere in this class. |
15+
| PlacementNew.cpp:37:3:37:51 | ... = ... | Resource p2 is acquired by class MyTestForPlacementNew but not released anywhere in this class. |
16+
| PlacementNew.cpp:38:3:38:49 | ... = ... | Resource p3 is acquired by class MyTestForPlacementNew but not released anywhere in this class. |
1417
| SelfRegistering.cpp:25:3:25:24 | ... = ... | Resource side is acquired by class MyOwner but not released anywhere in this class. |
1518
| Variants.cpp:23:3:23:13 | ... = ... | Resource f is acquired by class MyClass4 but not released anywhere in this class. |
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
typedef unsigned long size_t;
3+
4+
namespace std
5+
{
6+
using ::size_t;
7+
struct nothrow_t {};
8+
extern const nothrow_t nothrow;
9+
}
10+
11+
// nothrow new
12+
void* operator new(std::size_t size, const std::nothrow_t&) throw();
13+
14+
// placement new
15+
void* operator new (std::size_t size, void* ptr) throw();
16+
17+
// ---
18+
19+
class MyClassForPlacementNew
20+
{
21+
public:
22+
MyClassForPlacementNew(int _v) : v(_v) {}
23+
~MyClassForPlacementNew() {}
24+
25+
private:
26+
int v;
27+
};
28+
29+
class MyTestForPlacementNew
30+
{
31+
public:
32+
MyTestForPlacementNew()
33+
{
34+
void *buffer_ptr = buffer;
35+
36+
p1 = new MyClassForPlacementNew(1); // BAD: not released
37+
p2 = new (std::nothrow) MyClassForPlacementNew(2); // BAD: not released
38+
p3 = new (buffer_ptr) MyClassForPlacementNew(3); // GOOD: placement new, not an allocation [FALSE POSITIVE]
39+
}
40+
41+
~MyTestForPlacementNew()
42+
{
43+
}
44+
45+
MyClassForPlacementNew *p1, *p2, *p3;
46+
char buffer[sizeof(MyClassForPlacementNew)];
47+
};

0 commit comments

Comments
 (0)