Skip to content

Commit 97cdcbe

Browse files
committed
C++: Test for NewFreeMismatch.ql with operator new / delete.
1 parent 5034d40 commit 97cdcbe

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
| test2.cpp:19:3:19:6 | call to free | There is a new/free mismatch between this free and the corresponding $@. | test2.cpp:18:12:18:18 | new | new |
22
| 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:13 | new | new |
3+
| test2.cpp:50:2:50:18 | call to operator delete | There is a new/free mismatch between this free and the corresponding $@. | test2.cpp:45:18:45:24 | new | new |
4+
| test2.cpp:51:2:51:5 | call to free | There is a new/free mismatch between this free and the corresponding $@. | test2.cpp:45:18:45:24 | new | new |
5+
| test2.cpp:53:2:53:17 | delete | There is a malloc/delete mismatch between this delete and the corresponding $@. | test2.cpp:46:20:46:33 | call to operator new | malloc |
6+
| test2.cpp:57:2:57:18 | delete | There is a malloc/delete mismatch between this delete and the corresponding $@. | test2.cpp:47:21:47:26 | call to malloc | malloc |
37
| 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 |
48
| 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 |
59
| 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 |

cpp/ql/test/query-tests/Critical/NewFree/test2.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,27 @@ class MyTest2Class
3434
};
3535

3636
MyTest2Class<int> mt2c_i;
37+
38+
// ---
39+
40+
void* operator new(size_t);
41+
void operator delete(void*);
42+
43+
void test_operator_new()
44+
{
45+
void *ptr_new = new int;
46+
void *ptr_opnew = ::operator new(sizeof(int));
47+
void *ptr_malloc = malloc(sizeof(int));
48+
49+
delete ptr_new; // GOOD
50+
::operator delete(ptr_new); // GOOD [FALSE POSITIVE]
51+
free(ptr_new); // BAD
52+
53+
delete ptr_opnew; // GOOD [FALSE POSITIVE]
54+
::operator delete(ptr_opnew); // GOOD
55+
free(ptr_opnew); // BAD [NOT DETECTED]
56+
57+
delete ptr_malloc; // BAD
58+
::operator delete(ptr_malloc); // BAD [NOT DETECTED]
59+
free(ptr_malloc); // GOOD
60+
}

0 commit comments

Comments
 (0)