mirror of
https://github.com/github/codeql.git
synced 2026-04-29 18:55:14 +02:00
C++: Placement-new tests for MemoryNeverFreed.ql
This commit is contained in:
@@ -8,3 +8,7 @@
|
||||
| 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:150:3:150:21 | new | This memory is never freed |
|
||||
| test.cpp:153:3:153:17 | new[] | 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 [FALSE POSITIVE]
|
||||
int five = *(int*)buf;
|
||||
|
||||
new(buf) int[1]; // GOOD [FALSE POSITIVE]
|
||||
*(int*)buf = 4;
|
||||
|
||||
new(std::nothrow) int(3); // BAD
|
||||
new(std::nothrow) int[2]; // BAD
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user