mirror of
https://github.com/github/codeql.git
synced 2026-05-02 20:25:13 +02:00
Merge pull request #523 from jbj/placement-new-never-freed
C++: Detect non-allocating placement new in cpp/memory-never-freed
This commit is contained in:
@@ -8,3 +8,5 @@
|
||||
| test.cpp:42:18:42:23 | call to malloc | This memory is never freed |
|
||||
| test.cpp:73:18:73:23 | call to malloc | This memory is never freed |
|
||||
| test.cpp:89:18:89:23 | call to malloc | This memory is never freed |
|
||||
| test.cpp:156:3:156:26 | new | This memory is never freed |
|
||||
| test.cpp:157:3:157:26 | new[] | This memory is never freed |
|
||||
|
||||
@@ -130,3 +130,29 @@ int main()
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// --- placement new ---
|
||||
|
||||
namespace std {
|
||||
typedef unsigned long size_t;
|
||||
struct nothrow_t {};
|
||||
extern const nothrow_t nothrow;
|
||||
}
|
||||
|
||||
void* operator new (std::size_t size, void* ptr) noexcept;
|
||||
void* operator new[](std::size_t size, void* ptr) noexcept;
|
||||
void* operator new(std::size_t size, const std::nothrow_t&) noexcept;
|
||||
void* operator new[](std::size_t size, const std::nothrow_t&) noexcept;
|
||||
|
||||
int overloadedNew() {
|
||||
char buf[sizeof(int)];
|
||||
|
||||
new(&buf[0]) int(5); // GOOD
|
||||
int five = *(int*)buf;
|
||||
|
||||
new(buf) int[1]; // GOOD
|
||||
*(int*)buf = 4;
|
||||
|
||||
new(std::nothrow) int(3); // BAD
|
||||
new(std::nothrow) int[2]; // BAD
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user