Skip to content

Commit cca0722

Browse files
authored
Merge pull request github#11710 from geoffw0/qldocalloc
C++: Clarify Allocation.qll and Deallocation.qll
2 parents 99286fb + e7ea0d7 commit cca0722

File tree

2 files changed

+45
-39
lines changed

2 files changed

+45
-39
lines changed

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

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,38 @@ import semmle.code.cpp.Function
1212
import semmle.code.cpp.models.Models
1313

1414
/**
15-
* An allocation function such as `malloc`.
15+
* An allocation expression such as call to `malloc` or a `new` expression.
1616
*/
17-
abstract class AllocationFunction extends Function {
17+
abstract class AllocationExpr extends Expr {
1818
/**
19-
* Gets the index of the argument for the allocation size, if any. The actual
20-
* allocation size is the value of this argument multiplied by the result of
19+
* Gets an expression for the allocation size, if any. The actual allocation
20+
* size is the value of this expression multiplied by the result of
2121
* `getSizeMult()`, in bytes.
2222
*/
23-
int getSizeArg() { none() }
23+
Expr getSizeExpr() { none() }
2424

2525
/**
26-
* Gets the index of an argument that multiplies the allocation size given by
27-
* `getSizeArg`, if any.
26+
* Gets a constant multiplier for the allocation size given by `getSizeExpr`,
27+
* in bytes.
2828
*/
2929
int getSizeMult() { none() }
3030

3131
/**
32-
* Gets the index of the input pointer argument to be reallocated, if this
33-
* is a `realloc` function.
32+
* Gets the size of this allocation in bytes, if it is a fixed size and that
33+
* size can be determined.
3434
*/
35-
int getReallocPtrArg() { none() }
35+
int getSizeBytes() { none() }
36+
37+
/**
38+
* Gets the expression for the input pointer argument to be reallocated, if
39+
* this is a `realloc` function.
40+
*/
41+
Expr getReallocPtr() { none() }
42+
43+
/**
44+
* Gets the type of the elements that are allocated, if it can be determined.
45+
*/
46+
Type getAllocatedElementType() { none() }
3647

3748
/**
3849
* Whether or not this allocation requires a corresponding deallocation of
@@ -44,38 +55,30 @@ abstract class AllocationFunction extends Function {
4455
}
4556

4657
/**
47-
* An allocation expression such as call to `malloc` or a `new` expression.
58+
* An allocation function such as `malloc`.
59+
*
60+
* Note: `AllocationExpr` includes calls to allocation functions, so prefer
61+
* to use that class unless you specifically need to reason about functions.
4862
*/
49-
abstract class AllocationExpr extends Expr {
63+
abstract class AllocationFunction extends Function {
5064
/**
51-
* Gets an expression for the allocation size, if any. The actual allocation
52-
* size is the value of this expression multiplied by the result of
65+
* Gets the index of the argument for the allocation size, if any. The actual
66+
* allocation size is the value of this argument multiplied by the result of
5367
* `getSizeMult()`, in bytes.
5468
*/
55-
Expr getSizeExpr() { none() }
69+
int getSizeArg() { none() }
5670

5771
/**
58-
* Gets a constant multiplier for the allocation size given by `getSizeExpr`,
59-
* in bytes.
72+
* Gets the index of an argument that multiplies the allocation size given by
73+
* `getSizeArg`, if any.
6074
*/
6175
int getSizeMult() { none() }
6276

6377
/**
64-
* Gets the size of this allocation in bytes, if it is a fixed size and that
65-
* size can be determined.
66-
*/
67-
int getSizeBytes() { none() }
68-
69-
/**
70-
* Gets the expression for the input pointer argument to be reallocated, if
71-
* this is a `realloc` function.
72-
*/
73-
Expr getReallocPtr() { none() }
74-
75-
/**
76-
* Gets the type of the elements that are allocated, if it can be determined.
78+
* Gets the index of the input pointer argument to be reallocated, if this
79+
* is a `realloc` function.
7780
*/
78-
Type getAllocatedElementType() { none() }
81+
int getReallocPtrArg() { none() }
7982

8083
/**
8184
* Whether or not this allocation requires a corresponding deallocation of

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,26 @@ import semmle.code.cpp.Function
1212
import semmle.code.cpp.models.Models
1313

1414
/**
15-
* A deallocation function such as `free`.
15+
* An deallocation expression such as call to `free` or a `delete` expression.
1616
*/
17-
abstract class DeallocationFunction extends Function {
17+
abstract class DeallocationExpr extends Expr {
1818
/**
19-
* Gets the index of the argument that is freed by this function.
19+
* Gets the expression that is freed by this function.
2020
*/
21-
int getFreedArg() { none() }
21+
Expr getFreedExpr() { none() }
2222
}
2323

2424
/**
25-
* An deallocation expression such as call to `free` or a `delete` expression.
25+
* A deallocation function such as `free`.
26+
*
27+
* Note: `DeallocationExpr` includes calls to deallocation functions, so prefer
28+
* to use that class unless you specifically need to reason about functions.
2629
*/
27-
abstract class DeallocationExpr extends Expr {
30+
abstract class DeallocationFunction extends Function {
2831
/**
29-
* Gets the expression that is freed by this function.
32+
* Gets the index of the argument that is freed by this function.
3033
*/
31-
Expr getFreedExpr() { none() }
34+
int getFreedArg() { none() }
3235
}
3336

3437
/**

0 commit comments

Comments
 (0)