Skip to content

Commit a8c31c6

Browse files
committed
C++: Add AllocationFunction/AllocationExpr.requiresDealloc().
1 parent 25dc2ad commit a8c31c6

2 files changed

Lines changed: 39 additions & 6 deletions

File tree

cpp/ql/src/semmle/code/cpp/models/implementations/Allocation.qll

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,6 @@ class MallocAllocationFunction extends AllocationFunction {
7878
// CoTaskMemAlloc(size)
7979
name = "CoTaskMemAlloc" and sizeArg = 0
8080
or
81-
// alloca(size)
82-
name = "alloca" and sizeArg = 0
83-
or
84-
// __builtin_alloca(size)
85-
name = "__builtin_alloca" and sizeArg = 0
86-
or
8781
// kmem_alloc(size, flags)
8882
name = "kmem_alloc" and sizeArg = 0
8983
or
@@ -96,6 +90,31 @@ class MallocAllocationFunction extends AllocationFunction {
9690
override int getSizeArg() { result = sizeArg }
9791
}
9892

93+
/**
94+
* An allocation function (such as `alloca`) that does not require a
95+
* corresponding free (and has an argument for the size in bytes).
96+
*/
97+
class AllocaAllocationFunction extends AllocationFunction {
98+
int sizeArg;
99+
100+
AllocaAllocationFunction() {
101+
exists(string name |
102+
hasGlobalName(name) and
103+
(
104+
// alloca(size)
105+
name = "alloca" and sizeArg = 0
106+
or
107+
// __builtin_alloca(size)
108+
name = "__builtin_alloca" and sizeArg = 0
109+
)
110+
)
111+
}
112+
113+
override int getSizeArg() { result = sizeArg }
114+
115+
predicate requiresDealloc() { none() }
116+
}
117+
99118
/**
100119
* An allocation function (such as `calloc`) that has an argument for the size
101120
* and another argument for the size of those units (in bytes).
@@ -243,6 +262,8 @@ class CallAllocationExpr extends AllocationExpr, FunctionCall {
243262
override int getSizeBytes() { result = getSizeExpr().getValue().toInt() * getSizeMult() }
244263

245264
override Expr getReallocPtr() { result = getArgument(target.getReallocPtrArg()) }
265+
266+
override predicate requiresDealloc() { target.requiresDealloc() }
246267
}
247268

248269
/**

cpp/ql/src/semmle/code/cpp/models/interfaces/Allocation.qll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ abstract class AllocationFunction extends Function {
3333
* is a `realloc` function.
3434
*/
3535
int getReallocPtrArg() { none() }
36+
37+
/**
38+
* Whether or not this allocation requires a corresponding deallocation of
39+
* some sort (most do, but `alloca` for example does not).
40+
*/
41+
predicate requiresDealloc() { any() }
3642
}
3743

3844
/**
@@ -63,4 +69,10 @@ abstract class AllocationExpr extends Expr {
6369
* this is a `realloc` function.
6470
*/
6571
Expr getReallocPtr() { none() }
72+
73+
/**
74+
* Whether or not this allocation requires a corresponding deallocation of
75+
* some sort (most do, but `alloca` for example does not).
76+
*/
77+
predicate requiresDealloc() { any() }
6678
}

0 commit comments

Comments
 (0)