Skip to content

Commit e5c20b1

Browse files
committed
C++: Add extensible predicate for deallocation.
1 parent a36e393 commit e5c20b1

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

cpp/ql/lib/semmle/code/cpp/models/implementations/Deallocation.qll

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,45 @@ private class StandardDeallocationFunction extends DeallocationFunction {
6464
override int getFreedArg() { result = freedArg }
6565
}
6666

67+
/**
68+
* Holds if `f` is an deallocation function according to the
69+
* extensible `deallocationFunctionModel` predicate.
70+
*/
71+
private predicate isDeallocationFunctionFromModel(
72+
Function f, string namespace, string type, string name
73+
) {
74+
exists(boolean subtypes | deallocationFunctionModel(namespace, type, subtypes, name, _) |
75+
if type = ""
76+
then f.hasQualifiedName(namespace, "", name)
77+
else
78+
exists(Class c |
79+
c.hasQualifiedName(namespace, type) and f.hasQualifiedName(namespace, _, name)
80+
|
81+
if subtypes = true
82+
then f = c.getADerivedClass*().getAMemberFunction()
83+
else f = c.getAMemberFunction()
84+
)
85+
)
86+
}
87+
88+
/**
89+
* A deallocation function modeled via the extensible `deallocationFunctionModel` predicate.
90+
*/
91+
private class DeallocationFunctionFromModel extends DeallocationFunction {
92+
string namespace;
93+
string type;
94+
string name;
95+
96+
DeallocationFunctionFromModel() { isDeallocationFunctionFromModel(this, namespace, type, name) }
97+
98+
final override int getFreedArg() {
99+
exists(string freedArg |
100+
deallocationFunctionModel(namespace, type, _, name, freedArg) and
101+
result = freedArg.toInt()
102+
)
103+
}
104+
}
105+
67106
/**
68107
* An deallocation expression that is a function call, such as call to `free`.
69108
*/

cpp/ql/lib/semmle/code/cpp/models/interfaces/Deallocation.qll

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ abstract class DeallocationFunction extends Function {
3434
int getFreedArg() { none() }
3535
}
3636

37+
/**
38+
* Holds if an external deallocation model exists for the given parameters.
39+
*/
40+
extensible predicate deallocationFunctionModel(
41+
string namespace, string type, boolean subtypes, string name, string freedArg
42+
);
43+
3744
/**
3845
* An `operator delete` or `operator delete[]` function that may be associated
3946
* with `delete` or `delete[]` expressions. Note that `delete` and `delete[]`

0 commit comments

Comments
 (0)