Skip to content

Commit db8db86

Browse files
authored
Merge pull request #1141 from geoffw0/newfreebug
CPP: Fix a bug in NewFree.qll
2 parents 37bd472 + 867f357 commit db8db86

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

change-notes/1.21/analysis-cpp.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Improvements to C/C++ analysis
2+
3+
## General improvements
4+
5+
## New queries
6+
7+
| **Query** | **Tags** | **Purpose** |
8+
|-----------------------------|-----------|--------------------------------------------------------------------|
9+
10+
## Changes to existing queries
11+
12+
| **Query** | **Expected impact** | **Change** |
13+
|----------------------------|------------------------|------------------------------------------------------------------|
14+
| Mismatching new/free or malloc/delete (`cpp/new-free-mismatch`) | Fewer false positive results | Fixed an issue where functions were being identified as allocation functions inappropriately. Also affects `cpp/new-array-delete-mismatch` and `cpp/new-delete-array-mismatch`. |
15+
16+
## Changes to QL libraries

cpp/ql/src/Critical/NewDelete.qll

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44
import cpp
55
import semmle.code.cpp.controlflow.SSA
6+
import semmle.code.cpp.dataflow.DataFlow
67

78
/**
89
* Holds if `alloc` is a use of `malloc` or `new`. `kind` is
@@ -46,7 +47,10 @@ predicate allocExprOrIndirect(Expr alloc, string kind) {
4647
alloc.(FunctionCall).getTarget() = rtn.getEnclosingFunction() and
4748
(
4849
allocExprOrIndirect(rtn.getExpr(), kind) or
49-
allocReaches0(rtn.getExpr(), _, kind)
50+
exists(Expr e |
51+
allocExprOrIndirect(e, kind) and
52+
DataFlow::localFlow(DataFlow::exprNode(e), DataFlow::exprNode(rtn.getExpr()))
53+
)
5054
)
5155
)
5256
}

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,3 +371,56 @@ void test12(bool cond)
371371
free(z); // GOOD
372372
}
373373
}
374+
375+
// ---
376+
377+
class MyBuffer13
378+
{
379+
public:
380+
MyBuffer13(int size)
381+
{
382+
buffer = (char *)malloc(size * sizeof(char));
383+
}
384+
385+
~MyBuffer13()
386+
{
387+
free(buffer); // GOOD
388+
}
389+
390+
char *getBuffer() // note: this should not be considered an allocation function
391+
{
392+
return buffer;
393+
}
394+
395+
private:
396+
char *buffer;
397+
};
398+
399+
class MyPointer13
400+
{
401+
public:
402+
MyPointer13(char *_pointer) : pointer(_pointer)
403+
{
404+
}
405+
406+
MyPointer13(MyBuffer13 &buffer) : pointer(buffer.getBuffer())
407+
{
408+
}
409+
410+
char *getPointer() // note: this should not be considered an allocation function
411+
{
412+
return pointer;
413+
}
414+
415+
private:
416+
char *pointer;
417+
};
418+
419+
void test13()
420+
{
421+
MyBuffer13 myBuffer(100);
422+
MyPointer13 myPointer2(myBuffer);
423+
MyPointer13 myPointer3(new char[100]);
424+
425+
delete myPointer3.getPointer(); // GOOD
426+
}

0 commit comments

Comments
 (0)