@@ -15,7 +15,7 @@ import semmle.code.cpp.models.interfaces.Iterator
15
15
*/
16
16
private class IteratorTraits extends Class {
17
17
IteratorTraits ( ) {
18
- this .hasQualifiedName ( "std" , "iterator_traits" ) and
18
+ this .hasQualifiedName ( [ "std" , "bsl" ] , "iterator_traits" ) and
19
19
not this instanceof TemplateClass and
20
20
exists ( TypedefType t |
21
21
this .getAMember ( ) = t and
@@ -26,6 +26,14 @@ private class IteratorTraits extends Class {
26
26
Type getIteratorType ( ) { result = this .getTemplateArgument ( 0 ) }
27
27
}
28
28
29
+ /**
30
+ * A type that is deduced to be an iterator because there is a corresponding
31
+ * `std::iterator_traits` instantiation for it.
32
+ */
33
+ private class IteratorByTraits extends Iterator {
34
+ IteratorByTraits ( ) { exists ( IteratorTraits it | it .getIteratorType ( ) = this ) }
35
+ }
36
+
29
37
/**
30
38
* A type which has the typedefs expected for an iterator.
31
39
*/
@@ -36,25 +44,21 @@ private class IteratorByTypedefs extends Iterator, Class {
36
44
this .getAMember ( ) .( TypedefType ) .hasName ( "pointer" ) and
37
45
this .getAMember ( ) .( TypedefType ) .hasName ( "reference" ) and
38
46
this .getAMember ( ) .( TypedefType ) .hasName ( "iterator_category" ) and
39
- not this .hasQualifiedName ( "std" , "iterator_traits" )
47
+ not this .hasQualifiedName ( [ "std" , "bsl" ] , "iterator_traits" )
40
48
}
41
49
}
42
50
43
51
/**
44
52
* The `std::iterator` class.
45
53
*/
46
54
private class StdIterator extends Iterator , Class {
47
- StdIterator ( ) { this .hasQualifiedName ( "std" , "iterator" ) }
55
+ StdIterator ( ) { this .hasQualifiedName ( [ "std" , "bsl" ] , "iterator" ) }
48
56
}
49
57
50
58
/**
51
- * A type that is deduced to be an iterator because there is a corresponding
52
- * `std::iterator_traits` instantiation for it .
59
+ * Gets the `FunctionInput` corresponding to an iterator parameter to
60
+ * user-defined operator `op`, at `index` .
53
61
*/
54
- private class IteratorByTraits extends Iterator {
55
- IteratorByTraits ( ) { exists ( IteratorTraits it | it .getIteratorType ( ) = this ) }
56
- }
57
-
58
62
private FunctionInput getIteratorArgumentInput ( Operator op , int index ) {
59
63
exists ( Type t |
60
64
t =
@@ -155,17 +159,21 @@ private class IteratorSubOperator extends Operator, TaintFunction {
155
159
private class IteratorAssignArithmeticOperator extends Operator , DataFlowFunction , TaintFunction {
156
160
IteratorAssignArithmeticOperator ( ) {
157
161
this .hasName ( [ "operator+=" , "operator-=" ] ) and
158
- this . getDeclaringType ( ) instanceof Iterator
162
+ exists ( getIteratorArgumentInput ( this , 0 ) )
159
163
}
160
164
161
165
override predicate hasDataFlow ( FunctionInput input , FunctionOutput output ) {
162
166
input .isParameter ( 0 ) and
163
167
output .isReturnValue ( )
164
- or
165
- input .isParameterDeref ( 0 ) and output .isReturnValueDeref ( )
166
168
}
167
169
168
170
override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
171
+ input .isParameterDeref ( 0 ) and output .isReturnValueDeref ( )
172
+ or
173
+ // reverse flow from returned reference to the object referenced by the first parameter
174
+ input .isReturnValueDeref ( ) and
175
+ output .isParameterDeref ( 0 )
176
+ or
169
177
input .isParameterDeref ( 1 ) and
170
178
output .isParameterDeref ( 0 )
171
179
}
@@ -177,8 +185,7 @@ private class IteratorAssignArithmeticOperator extends Operator, DataFlowFunctio
177
185
class IteratorPointerDereferenceMemberOperator extends MemberFunction , TaintFunction ,
178
186
IteratorReferenceFunction {
179
187
IteratorPointerDereferenceMemberOperator ( ) {
180
- this .hasName ( "operator*" ) and
181
- this .getDeclaringType ( ) instanceof Iterator
188
+ this .getClassAndName ( "operator*" ) instanceof Iterator
182
189
}
183
190
184
191
override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
@@ -195,8 +202,7 @@ class IteratorPointerDereferenceMemberOperator extends MemberFunction, TaintFunc
195
202
*/
196
203
private class IteratorCrementMemberOperator extends MemberFunction , DataFlowFunction , TaintFunction {
197
204
IteratorCrementMemberOperator ( ) {
198
- this .hasName ( [ "operator++" , "operator--" ] ) and
199
- this .getDeclaringType ( ) instanceof Iterator
205
+ this .getClassAndName ( [ "operator++" , "operator--" ] ) instanceof Iterator
200
206
}
201
207
202
208
override predicate hasDataFlow ( FunctionInput input , FunctionOutput output ) {
@@ -220,10 +226,7 @@ private class IteratorCrementMemberOperator extends MemberFunction, DataFlowFunc
220
226
* A member `operator->` function for an iterator type.
221
227
*/
222
228
private class IteratorFieldMemberOperator extends Operator , TaintFunction {
223
- IteratorFieldMemberOperator ( ) {
224
- this .hasName ( "operator->" ) and
225
- this .getDeclaringType ( ) instanceof Iterator
226
- }
229
+ IteratorFieldMemberOperator ( ) { this .getClassAndName ( "operator->" ) instanceof Iterator }
227
230
228
231
override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
229
232
input .isQualifierObject ( ) and
@@ -236,8 +239,7 @@ private class IteratorFieldMemberOperator extends Operator, TaintFunction {
236
239
*/
237
240
private class IteratorBinaryArithmeticMemberOperator extends MemberFunction , TaintFunction {
238
241
IteratorBinaryArithmeticMemberOperator ( ) {
239
- this .hasName ( [ "operator+" , "operator-" ] ) and
240
- this .getDeclaringType ( ) instanceof Iterator
242
+ this .getClassAndName ( [ "operator+" , "operator-" ] ) instanceof Iterator
241
243
}
242
244
243
245
override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
@@ -252,21 +254,24 @@ private class IteratorBinaryArithmeticMemberOperator extends MemberFunction, Tai
252
254
private class IteratorAssignArithmeticMemberOperator extends MemberFunction , DataFlowFunction ,
253
255
TaintFunction {
254
256
IteratorAssignArithmeticMemberOperator ( ) {
255
- this .hasName ( [ "operator+=" , "operator-=" ] ) and
256
- this .getDeclaringType ( ) instanceof Iterator
257
+ this .getClassAndName ( [ "operator+=" , "operator-=" ] ) instanceof Iterator
257
258
}
258
259
259
260
override predicate hasDataFlow ( FunctionInput input , FunctionOutput output ) {
260
261
input .isQualifierAddress ( ) and
261
262
output .isReturnValue ( )
262
- or
263
- input .isReturnValueDeref ( ) and
264
- output .isQualifierObject ( )
265
263
}
266
264
267
265
override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
268
266
input .isQualifierObject ( ) and
269
267
output .isReturnValueDeref ( )
268
+ or
269
+ // reverse flow from returned reference to the qualifier
270
+ input .isReturnValueDeref ( ) and
271
+ output .isQualifierObject ( )
272
+ or
273
+ input .isParameterDeref ( 0 ) and
274
+ output .isQualifierObject ( )
270
275
}
271
276
}
272
277
@@ -275,10 +280,7 @@ private class IteratorAssignArithmeticMemberOperator extends MemberFunction, Dat
275
280
*/
276
281
private class IteratorArrayMemberOperator extends MemberFunction , TaintFunction ,
277
282
IteratorReferenceFunction {
278
- IteratorArrayMemberOperator ( ) {
279
- this .hasName ( "operator[]" ) and
280
- this .getDeclaringType ( ) instanceof Iterator
281
- }
283
+ IteratorArrayMemberOperator ( ) { this .getClassAndName ( "operator[]" ) instanceof Iterator }
282
284
283
285
override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
284
286
input .isQualifierObject ( ) and
@@ -295,8 +297,7 @@ private class IteratorArrayMemberOperator extends MemberFunction, TaintFunction,
295
297
*/
296
298
private class IteratorAssignmentMemberOperator extends MemberFunction , TaintFunction {
297
299
IteratorAssignmentMemberOperator ( ) {
298
- this .hasName ( "operator=" ) and
299
- this .getDeclaringType ( ) instanceof Iterator and
300
+ this .getClassAndName ( "operator=" ) instanceof Iterator and
300
301
not this instanceof CopyAssignmentOperator and
301
302
not this instanceof MoveAssignmentOperator
302
303
}
@@ -337,7 +338,7 @@ private class BeginOrEndFunction extends MemberFunction, TaintFunction, GetItera
337
338
*/
338
339
private class InserterIteratorFunction extends GetIteratorFunction {
339
340
InserterIteratorFunction ( ) {
340
- this .hasQualifiedName ( "std" , [ "front_inserter" , "inserter" , "back_inserter" ] )
341
+ this .hasQualifiedName ( [ "std" , "bsl" ] , [ "front_inserter" , "inserter" , "back_inserter" ] )
341
342
}
342
343
343
344
override predicate getsIterator ( FunctionInput input , FunctionOutput output ) {
0 commit comments