CPP: Add a test of placement new for AV Rule 79.ql.

This commit is contained in:
Geoffrey White
2018-09-21 10:47:00 +01:00
parent 4d46385c51
commit c7aa5c169b
2 changed files with 50 additions and 0 deletions

View File

@@ -11,5 +11,8 @@
| ExternalOwners.cpp:49:3:49:20 | ... = ... | Resource a is acquired by class MyScreen but not released anywhere in this class. |
| ListDelete.cpp:21:3:21:21 | ... = ... | Resource first is acquired by class MyThingColection but not released anywhere in this class. |
| NoDestructor.cpp:23:3:23:20 | ... = ... | Resource n is acquired by class MyClass5 but not released anywhere in this class. |
| PlacementNew.cpp:36:3:36:36 | ... = ... | Resource p1 is acquired by class MyTestForPlacementNew but not released anywhere in this class. |
| PlacementNew.cpp:37:3:37:51 | ... = ... | Resource p2 is acquired by class MyTestForPlacementNew but not released anywhere in this class. |
| PlacementNew.cpp:38:3:38:49 | ... = ... | Resource p3 is acquired by class MyTestForPlacementNew but not released anywhere in this class. |
| SelfRegistering.cpp:25:3:25:24 | ... = ... | Resource side is acquired by class MyOwner but not released anywhere in this class. |
| Variants.cpp:23:3:23:13 | ... = ... | Resource f is acquired by class MyClass4 but not released anywhere in this class. |

View File

@@ -0,0 +1,47 @@
typedef unsigned long size_t;
namespace std
{
using ::size_t;
struct nothrow_t {};
extern const nothrow_t nothrow;
}
// nothrow new
void* operator new(std::size_t size, const std::nothrow_t&) throw();
// placement new
void* operator new (std::size_t size, void* ptr) throw();
// ---
class MyClassForPlacementNew
{
public:
MyClassForPlacementNew(int _v) : v(_v) {}
~MyClassForPlacementNew() {}
private:
int v;
};
class MyTestForPlacementNew
{
public:
MyTestForPlacementNew()
{
void *buffer_ptr = buffer;
p1 = new MyClassForPlacementNew(1); // BAD: not released
p2 = new (std::nothrow) MyClassForPlacementNew(2); // BAD: not released
p3 = new (buffer_ptr) MyClassForPlacementNew(3); // GOOD: placement new, not an allocation [FALSE POSITIVE]
}
~MyTestForPlacementNew()
{
}
MyClassForPlacementNew *p1, *p2, *p3;
char buffer[sizeof(MyClassForPlacementNew)];
};