Skip to content

Commit 917b984

Browse files
authored
Merge pull request github#3050 from geoffw0/mismatching_placement_new
C++: Fix mismatching new/free FP in template code.
2 parents 4355f8d + f84c94b commit 917b984

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

change-notes/1.24/analysis-cpp.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ The following changes in version 1.24 affect C/C++ analysis in all applications.
1818
| No space for zero terminator (`cpp/no-space-for-terminator`) | More true positive results | This query now identifies a wider variety of buffer allocations using the `semmle.code.cpp.models.interfaces.Allocation` library. |
1919
| Memory is never freed (`cpp/memory-never-freed`) | More true positive results | This query now identifies a wider variety of buffer allocations using the `semmle.code.cpp.models.interfaces.Allocation` library. |
2020
| Memory may not be freed (`cpp/memory-may-not-be-freed`) | More true positive results | This query now identifies a wider variety of buffer allocations using the `semmle.code.cpp.models.interfaces.Allocation` library. |
21+
| Mismatching new/free or malloc/delete (`cpp/new-free-mismatch`) | Fewer false positive results | Fixed false positive results in template code. |
2122
| Missing return statement (`cpp/missing-return`) | Fewer false positive results | Functions containing `asm` statements are no longer highlighted by this query. |
2223
| No space for zero terminator (`cpp/no-space-for-terminator`) | More correct results | String arguments to formatting functions are now (usually) expected to be null terminated strings. |
2324
| Hard-coded Japanese era start date (`cpp/japanese-era/exact-era-date`) | | This query is no longer run on LGTM. |

cpp/ql/src/Critical/NewDelete.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import semmle.code.cpp.dataflow.DataFlow
1212
*/
1313
predicate allocExpr(Expr alloc, string kind) {
1414
isAllocationExpr(alloc) and
15+
not alloc.isFromUninstantiatedTemplate(_) and
1516
(
1617
alloc instanceof FunctionCall and
1718
kind = "malloc"

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
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:12:18:18 | 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:13 | new | new |
13
| 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 |
24
| 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 |
35
| 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
31+
}
32+
33+
int *c, *d;
34+
};
35+
36+
MyTest2Class<int> mt2c_i;

0 commit comments

Comments
 (0)