C++: Placement-new tests for MemoryNeverFreed.ql

This commit is contained in:
Jonas Jensen
2018-11-22 10:48:18 +01:00
parent e062851709
commit a17debac3e
2 changed files with 30 additions and 0 deletions

View File

@@ -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 |

View File

@@ -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
}