Skip to content

Commit 3f4ebd2

Browse files
committed
C++: Move models into models dir.
1 parent e0651b2 commit 3f4ebd2

File tree

3 files changed

+82
-51
lines changed

3 files changed

+82
-51
lines changed

cpp/ql/src/semmle/code/cpp/MemberFunction.qll

Lines changed: 5 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
*/
55

66
import cpp
7-
import semmle.code.cpp.models.interfaces.DataFlow
8-
import semmle.code.cpp.models.interfaces.Taint
97

108
/**
119
* A C++ function declared as a member of a class [N4140 9.3]. This includes
@@ -164,7 +162,7 @@ class ConstMemberFunction extends MemberFunction {
164162
* };
165163
* ```
166164
*/
167-
class Constructor extends MemberFunction, TaintFunction {
165+
class Constructor extends MemberFunction {
168166
Constructor() { functions(underlyingElement(this), _, 2) }
169167

170168
override string getCanonicalQLClass() { result = "Constructor" }
@@ -194,16 +192,6 @@ class Constructor extends MemberFunction, TaintFunction {
194192
ConstructorInit getInitializer(int i) {
195193
exprparents(unresolveElement(result), i, underlyingElement(this))
196194
}
197-
198-
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
199-
// taint flow from any constructor argument to the returned object
200-
exists(int idx |
201-
input.isParameter(idx) and
202-
output.isReturnValue() and
203-
not this.(CopyConstructor).hasDataFlow(input, output) and // don't duplicate where we have data flow
204-
not this.(MoveConstructor).hasDataFlow(input, output) // don't duplicate where we have data flow
205-
)
206-
}
207195
}
208196

209197
/**
@@ -278,7 +266,7 @@ private predicate hasMoveSignature(MemberFunction f) {
278266
* desired instead, see the member predicate
279267
* `mayNotBeCopyConstructorInInstantiation`.
280268
*/
281-
class CopyConstructor extends Constructor, DataFlowFunction {
269+
class CopyConstructor extends Constructor {
282270
CopyConstructor() {
283271
hasCopySignature(this) and
284272
(
@@ -310,12 +298,6 @@ class CopyConstructor extends Constructor, DataFlowFunction {
310298
getDeclaringType() instanceof TemplateClass and
311299
getNumberOfParameters() > 1
312300
}
313-
314-
override predicate hasDataFlow(FunctionInput input, FunctionOutput output) {
315-
// data flow from the first constructor argument to the returned object
316-
input.isParameter(0) and
317-
output.isReturnValue()
318-
}
319301
}
320302

321303
/**
@@ -341,7 +323,7 @@ class CopyConstructor extends Constructor, DataFlowFunction {
341323
* desired instead, see the member predicate
342324
* `mayNotBeMoveConstructorInInstantiation`.
343325
*/
344-
class MoveConstructor extends Constructor, DataFlowFunction {
326+
class MoveConstructor extends Constructor {
345327
MoveConstructor() {
346328
hasMoveSignature(this) and
347329
(
@@ -373,12 +355,6 @@ class MoveConstructor extends Constructor, DataFlowFunction {
373355
getDeclaringType() instanceof TemplateClass and
374356
getNumberOfParameters() > 1
375357
}
376-
377-
override predicate hasDataFlow(FunctionInput input, FunctionOutput output) {
378-
// data flow from the first constructor argument to the returned object
379-
input.isParameter(0) and
380-
output.isReturnValue()
381-
}
382358
}
383359

384360
/**
@@ -467,7 +443,7 @@ class ConversionOperator extends MemberFunction, ImplicitConversionFunction {
467443
* takes exactly one parameter of type `T`, `T&`, `const T&`, `volatile
468444
* T&`, or `const volatile T&`.
469445
*/
470-
class CopyAssignmentOperator extends Operator, TaintFunction {
446+
class CopyAssignmentOperator extends Operator {
471447
CopyAssignmentOperator() {
472448
hasName("operator=") and
473449
(
@@ -482,17 +458,6 @@ class CopyAssignmentOperator extends Operator, TaintFunction {
482458
}
483459

484460
override string getCanonicalQLClass() { result = "CopyAssignmentOperator" }
485-
486-
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
487-
// taint flow from argument to self
488-
input.isParameterDeref(0) and
489-
output.isQualifierObject()
490-
or
491-
// taint flow from argument to return value
492-
input.isParameterDeref(0) and
493-
output.isReturnValueDeref()
494-
// TODO: it would be more accurate to model copy assignment as data flow
495-
}
496461
}
497462

498463
/**
@@ -510,7 +475,7 @@ class CopyAssignmentOperator extends Operator, TaintFunction {
510475
* takes exactly one parameter of type `T&&`, `const T&&`, `volatile T&&`,
511476
* or `const volatile T&&`.
512477
*/
513-
class MoveAssignmentOperator extends Operator, TaintFunction {
478+
class MoveAssignmentOperator extends Operator {
514479
MoveAssignmentOperator() {
515480
hasName("operator=") and
516481
hasMoveSignature(this) and
@@ -519,15 +484,4 @@ class MoveAssignmentOperator extends Operator, TaintFunction {
519484
}
520485

521486
override string getCanonicalQLClass() { result = "MoveAssignmentOperator" }
522-
523-
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
524-
// taint flow from argument to self
525-
input.isParameterDeref(0) and
526-
output.isQualifierObject()
527-
or
528-
// taint flow from argument to return value
529-
input.isParameterDeref(0) and
530-
output.isReturnValueDeref()
531-
// TODO: it would be more accurate to model move assignment as data flow
532-
}
533487
}

cpp/ql/src/semmle/code/cpp/models/Models.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ private import implementations.Fread
44
private import implementations.Gets
55
private import implementations.IdentityFunction
66
private import implementations.Inet
7+
private import implementations.MemberFunction
78
private import implementations.Memcpy
89
private import implementations.Memset
910
private import implementations.Printf
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* Provides models for C++ constructors and user-defined operators.
3+
*/
4+
5+
import cpp
6+
import semmle.code.cpp.models.interfaces.DataFlow
7+
import semmle.code.cpp.models.interfaces.Taint
8+
9+
/**
10+
* Model for C++ constructors (including copy and move constructors).
11+
*/
12+
class ConstructorModel extends Constructor, TaintFunction {
13+
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
14+
// taint flow from any constructor argument to the returned object
15+
exists(int idx |
16+
input.isParameter(idx) and
17+
output.isReturnValue() and
18+
not this.(CopyConstructorModel).hasDataFlow(input, output) and // don't duplicate where we have data flow
19+
not this.(MoveConstructorModel).hasDataFlow(input, output) // don't duplicate where we have data flow
20+
)
21+
}
22+
}
23+
24+
/**
25+
* Model for C++ copy constructors.
26+
*/
27+
class CopyConstructorModel extends CopyConstructor, DataFlowFunction {
28+
override predicate hasDataFlow(FunctionInput input, FunctionOutput output) {
29+
// data flow from the first constructor argument to the returned object
30+
input.isParameter(0) and
31+
output.isReturnValue()
32+
}
33+
}
34+
35+
/**
36+
* Model for C++ move constructors.
37+
*/
38+
class MoveConstructorModel extends MoveConstructor, DataFlowFunction {
39+
override predicate hasDataFlow(FunctionInput input, FunctionOutput output) {
40+
// data flow from the first constructor argument to the returned object
41+
input.isParameter(0) and
42+
output.isReturnValue()
43+
}
44+
}
45+
46+
/**
47+
* Model for C++ copy assignment operators.
48+
*/
49+
class CopyAssignmentOperatorModel extends CopyAssignmentOperator, TaintFunction {
50+
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
51+
// taint flow from argument to self
52+
input.isParameterDeref(0) and
53+
output.isQualifierObject()
54+
or
55+
// taint flow from argument to return value
56+
input.isParameterDeref(0) and
57+
output.isReturnValueDeref()
58+
// TODO: it would be more accurate to model copy assignment as data flow
59+
}
60+
}
61+
62+
/**
63+
* Model for C++ move assignment operators.
64+
*/
65+
class MoveAssignmentOperatorModel extends MoveAssignmentOperator, TaintFunction {
66+
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
67+
// taint flow from argument to self
68+
input.isParameterDeref(0) and
69+
output.isQualifierObject()
70+
or
71+
// taint flow from argument to return value
72+
input.isParameterDeref(0) and
73+
output.isReturnValueDeref()
74+
// TODO: it would be more accurate to model move assignment as data flow
75+
}
76+
}

0 commit comments

Comments
 (0)