Skip to content

Commit 9b07782

Browse files
authored
Merge pull request github#4634 from geoffw0/modelchanges2
C++: Make classes in models.implementations private
2 parents 3954037 + 71a8ac5 commit 9b07782

34 files changed

+233
-202
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
lgtm,codescanning
2+
* Various classes in `semmle.code.cpp.models.implementations` have been made private. Users should not depend on library implementation details.
3+
* The `OperatorNewAllocationFunction`, `OperatorDeleteDeallocationFunction`, `Iterator` and `Snprintf` classes now have interfaces in `semmle.code.cpp.models.interfaces`.

cpp/ql/src/Critical/NewDelete.qll

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import cpp
66
import semmle.code.cpp.controlflow.SSA
77
import semmle.code.cpp.dataflow.DataFlow
8-
import semmle.code.cpp.models.implementations.Allocation
9-
import semmle.code.cpp.models.implementations.Deallocation
108

119
/**
1210
* Holds if `alloc` is a use of `malloc` or `new`. `kind` is

cpp/ql/src/Security/CWE/CWE-020/SafeExternalAPIFunction.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ abstract class SafeExternalAPIFunction extends Function { }
1313
/** The default set of "safe" external APIs. */
1414
private class DefaultSafeExternalAPIFunction extends SafeExternalAPIFunction {
1515
DefaultSafeExternalAPIFunction() {
16+
// implementation note: this should be based on the properties of public interfaces, rather than accessing implementation classes directly. When we've done that, the three classes referenced here should be made fully private.
1617
this instanceof PureStrFunction or
1718
this instanceof StrLenFunction or
1819
this instanceof PureMemFunction

cpp/ql/src/Security/CWE/CWE-020/ir/SafeExternalAPIFunction.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ abstract class SafeExternalAPIFunction extends Function { }
1313
/** The default set of "safe" external APIs. */
1414
private class DefaultSafeExternalAPIFunction extends SafeExternalAPIFunction {
1515
DefaultSafeExternalAPIFunction() {
16+
// implementation note: this should be based on the properties of public interfaces, rather than accessing implementation classes directly. When we've done that, the three classes referenced here should be made fully private.
1617
this instanceof PureStrFunction or
1718
this instanceof StrLenFunction or
1819
this instanceof PureMemFunction

cpp/ql/src/semmle/code/cpp/commons/Printf.qll

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import semmle.code.cpp.Type
66
import semmle.code.cpp.commons.CommonType
77
import semmle.code.cpp.commons.StringAnalysis
88
import semmle.code.cpp.models.interfaces.FormattingFunction
9-
import semmle.code.cpp.models.implementations.Printf
109

1110
class PrintfFormatAttribute extends FormatAttribute {
1211
PrintfFormatAttribute() { getArchetype() = ["printf", "__printf__"] }

cpp/ql/src/semmle/code/cpp/exprs/Expr.qll

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import semmle.code.cpp.Element
66
private import semmle.code.cpp.Enclosing
77
private import semmle.code.cpp.internal.ResolveClass
88
private import semmle.code.cpp.internal.AddressConstantExpression
9-
private import semmle.code.cpp.models.implementations.Allocation
109

1110
/**
1211
* A C/C++ expression.

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

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import semmle.code.cpp.models.interfaces.Allocation
1010
* An allocation function (such as `malloc`) that has an argument for the size
1111
* in bytes.
1212
*/
13-
class MallocAllocationFunction extends AllocationFunction {
13+
private class MallocAllocationFunction extends AllocationFunction {
1414
int sizeArg;
1515

1616
MallocAllocationFunction() {
@@ -112,7 +112,7 @@ class MallocAllocationFunction extends AllocationFunction {
112112
* An allocation function (such as `alloca`) that does not require a
113113
* corresponding free (and has an argument for the size in bytes).
114114
*/
115-
class AllocaAllocationFunction extends AllocationFunction {
115+
private class AllocaAllocationFunction extends AllocationFunction {
116116
int sizeArg;
117117

118118
AllocaAllocationFunction() {
@@ -137,7 +137,7 @@ class AllocaAllocationFunction extends AllocationFunction {
137137
* An allocation function (such as `calloc`) that has an argument for the size
138138
* and another argument for the size of those units (in bytes).
139139
*/
140-
class CallocAllocationFunction extends AllocationFunction {
140+
private class CallocAllocationFunction extends AllocationFunction {
141141
int sizeArg;
142142
int multArg;
143143

@@ -158,7 +158,7 @@ class CallocAllocationFunction extends AllocationFunction {
158158
* An allocation function (such as `realloc`) that has an argument for the size
159159
* in bytes, and an argument for an existing pointer that is to be reallocated.
160160
*/
161-
class ReallocAllocationFunction extends AllocationFunction {
161+
private class ReallocAllocationFunction extends AllocationFunction {
162162
int sizeArg;
163163
int reallocArg;
164164

@@ -197,7 +197,7 @@ class ReallocAllocationFunction extends AllocationFunction {
197197
* A miscellaneous allocation function that has no explicit argument for
198198
* the size of the allocation.
199199
*/
200-
class SizelessAllocationFunction extends AllocationFunction {
200+
private class SizelessAllocationFunction extends AllocationFunction {
201201
SizelessAllocationFunction() {
202202
exists(string name |
203203
hasGlobalName(name) and
@@ -236,40 +236,6 @@ class SizelessAllocationFunction extends AllocationFunction {
236236
}
237237
}
238238

239-
/**
240-
* An `operator new` or `operator new[]` function that may be associated with `new` or
241-
* `new[]` expressions. Note that `new` and `new[]` are not function calls, but these
242-
* functions may also be called directly.
243-
*/
244-
class OperatorNewAllocationFunction extends AllocationFunction {
245-
OperatorNewAllocationFunction() {
246-
exists(string name |
247-
hasGlobalName(name) and
248-
(
249-
// operator new(bytes, ...)
250-
name = "operator new"
251-
or
252-
// operator new[](bytes, ...)
253-
name = "operator new[]"
254-
)
255-
)
256-
}
257-
258-
override int getSizeArg() { result = 0 }
259-
260-
override predicate requiresDealloc() { not exists(getPlacementArgument()) }
261-
262-
/**
263-
* Gets the position of the placement pointer if this is a placement
264-
* `operator new` function.
265-
*/
266-
int getPlacementArgument() {
267-
getNumberOfParameters() = 2 and
268-
getParameter(1).getType() instanceof VoidPointerType and
269-
result = 1
270-
}
271-
}
272-
273239
/**
274240
* Holds if `sizeExpr` is an expression consisting of a subexpression
275241
* `lengthExpr` multiplied by a constant `sizeof` that is the result of a
@@ -302,7 +268,7 @@ private predicate deconstructSizeExpr(Expr sizeExpr, Expr lengthExpr, int sizeof
302268
/**
303269
* An allocation expression that is a function call, such as call to `malloc`.
304270
*/
305-
class CallAllocationExpr extends AllocationExpr, FunctionCall {
271+
private class CallAllocationExpr extends AllocationExpr, FunctionCall {
306272
AllocationFunction target;
307273

308274
CallAllocationExpr() {
@@ -353,7 +319,7 @@ class CallAllocationExpr extends AllocationExpr, FunctionCall {
353319
/**
354320
* An allocation expression that is a `new` expression.
355321
*/
356-
class NewAllocationExpr extends AllocationExpr, NewExpr {
322+
private class NewAllocationExpr extends AllocationExpr, NewExpr {
357323
NewAllocationExpr() { this instanceof NewExpr }
358324

359325
override int getSizeBytes() { result = getAllocatedType().getSize() }
@@ -366,7 +332,7 @@ class NewAllocationExpr extends AllocationExpr, NewExpr {
366332
/**
367333
* An allocation expression that is a `new []` expression.
368334
*/
369-
class NewArrayAllocationExpr extends AllocationExpr, NewArrayExpr {
335+
private class NewArrayAllocationExpr extends AllocationExpr, NewArrayExpr {
370336
NewArrayAllocationExpr() { this instanceof NewArrayExpr }
371337

372338
override Expr getSizeExpr() {

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

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import semmle.code.cpp.models.interfaces.Deallocation
99
/**
1010
* A deallocation function such as `free`.
1111
*/
12-
class StandardDeallocationFunction extends DeallocationFunction {
12+
private class StandardDeallocationFunction extends DeallocationFunction {
1313
int freedArg;
1414

1515
StandardDeallocationFunction() {
@@ -89,32 +89,10 @@ class StandardDeallocationFunction extends DeallocationFunction {
8989
override int getFreedArg() { result = freedArg }
9090
}
9191

92-
/**
93-
* An `operator delete` or `operator delete[]` function that may be associated
94-
* with `delete` or `delete[]` expressions. Note that `delete` and `delete[]`
95-
* are not function calls, but these functions may also be called directly.
96-
*/
97-
class OperatorDeleteDeallocationFunction extends DeallocationFunction {
98-
OperatorDeleteDeallocationFunction() {
99-
exists(string name |
100-
hasGlobalName(name) and
101-
(
102-
// operator delete(pointer, ...)
103-
name = "operator delete"
104-
or
105-
// operator delete[](pointer, ...)
106-
name = "operator delete[]"
107-
)
108-
)
109-
}
110-
111-
override int getFreedArg() { result = 0 }
112-
}
113-
11492
/**
11593
* An deallocation expression that is a function call, such as call to `free`.
11694
*/
117-
class CallDeallocationExpr extends DeallocationExpr, FunctionCall {
95+
private class CallDeallocationExpr extends DeallocationExpr, FunctionCall {
11896
DeallocationFunction target;
11997

12098
CallDeallocationExpr() { target = getTarget() }
@@ -125,7 +103,7 @@ class CallDeallocationExpr extends DeallocationExpr, FunctionCall {
125103
/**
126104
* An deallocation expression that is a `delete` expression.
127105
*/
128-
class DeleteDeallocationExpr extends DeallocationExpr, DeleteExpr {
106+
private class DeleteDeallocationExpr extends DeallocationExpr, DeleteExpr {
129107
DeleteDeallocationExpr() { this instanceof DeleteExpr }
130108

131109
override Expr getFreedExpr() { result = getExpr() }
@@ -134,7 +112,7 @@ class DeleteDeallocationExpr extends DeallocationExpr, DeleteExpr {
134112
/**
135113
* An deallocation expression that is a `delete []` expression.
136114
*/
137-
class DeleteArrayDeallocationExpr extends DeallocationExpr, DeleteArrayExpr {
115+
private class DeleteArrayDeallocationExpr extends DeallocationExpr, DeleteArrayExpr {
138116
DeleteArrayDeallocationExpr() { this instanceof DeleteArrayExpr }
139117

140118
override Expr getFreedExpr() { result = getExpr() }

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import semmle.code.cpp.models.interfaces.Alias
22
import semmle.code.cpp.models.interfaces.FlowSource
33

4-
class Fread extends AliasFunction, RemoteFlowFunction {
4+
private class Fread extends AliasFunction, RemoteFlowFunction {
55
Fread() { this.hasGlobalName("fread") }
66

77
override predicate parameterNeverEscapes(int n) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import semmle.code.cpp.models.interfaces.FlowSource
66
/**
77
* The standard functions `getdelim`, `getwdelim` and the glibc variant `__getdelim`.
88
*/
9-
class GetDelimFunction extends TaintFunction, AliasFunction, SideEffectFunction, RemoteFlowFunction {
9+
private class GetDelimFunction extends TaintFunction, AliasFunction, SideEffectFunction,
10+
RemoteFlowFunction {
1011
GetDelimFunction() { hasGlobalName(["getdelim", "getwdelim", "__getdelim"]) }
1112

1213
override predicate hasTaintFlow(FunctionInput i, FunctionOutput o) {

0 commit comments

Comments
 (0)