Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d454c84

Browse files
committed
C++: Test case.
1 parent 155985c commit d454c84

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

cpp/ql/test/query-tests/Critical/NewFree/NewFreeMismatch.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
| test2.cpp:19:3:19:6 | call to free | There is a new/free mismatch between this free and the corresponding $@. | test2.cpp:18:10:18:14 | new | new |
2+
| test2.cpp:26:3:26:6 | call to free | There is a new/free mismatch between this free and the corresponding $@. | test2.cpp:25:7:25:11 | new | new |
3+
| test2.cpp:26:3:26:6 | call to free | There is a new/free mismatch between this free and the corresponding $@. | test2.cpp:25:7:25:11 | new | new |
4+
| test2.cpp:30:3:30:6 | call to free | There is a new/free mismatch between this free and the corresponding $@. | test2.cpp:29:7:29:18 | new | new |
15
| test.cpp:36:2:36:17 | delete | There is a malloc/delete mismatch between this delete and the corresponding $@. | test.cpp:27:18:27:23 | call to malloc | malloc |
26
| test.cpp:41:2:41:5 | call to free | There is a new/free mismatch between this free and the corresponding $@. | test.cpp:26:7:26:17 | new | new |
37
| test.cpp:68:3:68:11 | delete | There is a malloc/delete mismatch between this delete and the corresponding $@. | test.cpp:64:28:64:33 | call to malloc | malloc |
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// semmle-extractor-options: -std=gnu++14
2+
3+
typedef unsigned long size_t;
4+
5+
void *malloc(size_t size);
6+
void free(void *ptr);
7+
8+
void* operator new(size_t _Size, void *_Where);
9+
10+
// ---
11+
12+
template<typename T>
13+
class MyTest2Class
14+
{
15+
public:
16+
MyTest2Class()
17+
{
18+
int *a = new int;
19+
free(a); // BAD
20+
21+
int *ptr_b = (int *)malloc(sizeof(int));
22+
int *b = new(ptr_b) int;
23+
free(b); // GOOD
24+
25+
c = new int;
26+
free(c); // BAD
27+
28+
int *ptr_d = (int *)malloc(sizeof(int));
29+
d = new(ptr_d) int;
30+
free(d); // GOOD [FALSE POSITIVE]
31+
}
32+
33+
int *c, *d;
34+
};
35+
36+
MyTest2Class<int> mt2c_i;

0 commit comments

Comments
 (0)