Skip to content

Commit a17deba

Browse files
committed
C++: Placement-new tests for MemoryNeverFreed.ql
1 parent e062851 commit a17deba

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

cpp/ql/test/query-tests/Critical/MemoryFreed/MemoryNeverFreed.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@
88
| test.cpp:42:18:42:23 | call to malloc | This memory is never freed |
99
| test.cpp:73:18:73:23 | call to malloc | This memory is never freed |
1010
| test.cpp:89:18:89:23 | call to malloc | This memory is never freed |
11+
| test.cpp:150:3:150:21 | new | This memory is never freed |
12+
| test.cpp:153:3:153:17 | new[] | This memory is never freed |
13+
| test.cpp:156:3:156:26 | new | This memory is never freed |
14+
| test.cpp:157:3:157:26 | new[] | This memory is never freed |

cpp/ql/test/query-tests/Critical/MemoryFreed/test.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,29 @@ int main()
130130

131131
return 0;
132132
}
133+
134+
// --- placement new ---
135+
136+
namespace std {
137+
typedef unsigned long size_t;
138+
struct nothrow_t {};
139+
extern const nothrow_t nothrow;
140+
}
141+
142+
void* operator new (std::size_t size, void* ptr) noexcept;
143+
void* operator new[](std::size_t size, void* ptr) noexcept;
144+
void* operator new(std::size_t size, const std::nothrow_t&) noexcept;
145+
void* operator new[](std::size_t size, const std::nothrow_t&) noexcept;
146+
147+
int overloadedNew() {
148+
char buf[sizeof(int)];
149+
150+
new(&buf[0]) int(5); // GOOD [FALSE POSITIVE]
151+
int five = *(int*)buf;
152+
153+
new(buf) int[1]; // GOOD [FALSE POSITIVE]
154+
*(int*)buf = 4;
155+
156+
new(std::nothrow) int(3); // BAD
157+
new(std::nothrow) int[2]; // BAD
158+
}

0 commit comments

Comments
 (0)