Skip to content

Commit 3082d70

Browse files
committed
Merge branch 'main' into fix-dataflow-regression-const-member-function
2 parents 908f24d + 8716cbd commit 3082d70

File tree

237 files changed

+611
-100
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

237 files changed

+611
-100
lines changed

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

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import semmle.code.cpp.models.interfaces.Iterator
1515
*/
1616
private class IteratorTraits extends Class {
1717
IteratorTraits() {
18-
this.hasQualifiedName("std", "iterator_traits") and
18+
this.hasQualifiedName(["std", "bsl"], "iterator_traits") and
1919
not this instanceof TemplateClass and
2020
exists(TypedefType t |
2121
this.getAMember() = t and
@@ -26,6 +26,14 @@ private class IteratorTraits extends Class {
2626
Type getIteratorType() { result = this.getTemplateArgument(0) }
2727
}
2828

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+
2937
/**
3038
* A type which has the typedefs expected for an iterator.
3139
*/
@@ -36,25 +44,21 @@ private class IteratorByTypedefs extends Iterator, Class {
3644
this.getAMember().(TypedefType).hasName("pointer") and
3745
this.getAMember().(TypedefType).hasName("reference") and
3846
this.getAMember().(TypedefType).hasName("iterator_category") and
39-
not this.hasQualifiedName("std", "iterator_traits")
47+
not this.hasQualifiedName(["std", "bsl"], "iterator_traits")
4048
}
4149
}
4250

4351
/**
4452
* The `std::iterator` class.
4553
*/
4654
private class StdIterator extends Iterator, Class {
47-
StdIterator() { this.hasQualifiedName("std", "iterator") }
55+
StdIterator() { this.hasQualifiedName(["std", "bsl"], "iterator") }
4856
}
4957

5058
/**
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`.
5361
*/
54-
private class IteratorByTraits extends Iterator {
55-
IteratorByTraits() { exists(IteratorTraits it | it.getIteratorType() = this) }
56-
}
57-
5862
private FunctionInput getIteratorArgumentInput(Operator op, int index) {
5963
exists(Type t |
6064
t =
@@ -155,17 +159,21 @@ private class IteratorSubOperator extends Operator, TaintFunction {
155159
private class IteratorAssignArithmeticOperator extends Operator, DataFlowFunction, TaintFunction {
156160
IteratorAssignArithmeticOperator() {
157161
this.hasName(["operator+=", "operator-="]) and
158-
this.getDeclaringType() instanceof Iterator
162+
exists(getIteratorArgumentInput(this, 0))
159163
}
160164

161165
override predicate hasDataFlow(FunctionInput input, FunctionOutput output) {
162166
input.isParameter(0) and
163167
output.isReturnValue()
164-
or
165-
input.isParameterDeref(0) and output.isReturnValueDeref()
166168
}
167169

168170
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
169177
input.isParameterDeref(1) and
170178
output.isParameterDeref(0)
171179
}
@@ -177,8 +185,7 @@ private class IteratorAssignArithmeticOperator extends Operator, DataFlowFunctio
177185
class IteratorPointerDereferenceMemberOperator extends MemberFunction, TaintFunction,
178186
IteratorReferenceFunction {
179187
IteratorPointerDereferenceMemberOperator() {
180-
this.hasName("operator*") and
181-
this.getDeclaringType() instanceof Iterator
188+
this.getClassAndName("operator*") instanceof Iterator
182189
}
183190

184191
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
@@ -195,8 +202,7 @@ class IteratorPointerDereferenceMemberOperator extends MemberFunction, TaintFunc
195202
*/
196203
private class IteratorCrementMemberOperator extends MemberFunction, DataFlowFunction, TaintFunction {
197204
IteratorCrementMemberOperator() {
198-
this.hasName(["operator++", "operator--"]) and
199-
this.getDeclaringType() instanceof Iterator
205+
this.getClassAndName(["operator++", "operator--"]) instanceof Iterator
200206
}
201207

202208
override predicate hasDataFlow(FunctionInput input, FunctionOutput output) {
@@ -220,10 +226,7 @@ private class IteratorCrementMemberOperator extends MemberFunction, DataFlowFunc
220226
* A member `operator->` function for an iterator type.
221227
*/
222228
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 }
227230

228231
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
229232
input.isQualifierObject() and
@@ -236,8 +239,7 @@ private class IteratorFieldMemberOperator extends Operator, TaintFunction {
236239
*/
237240
private class IteratorBinaryArithmeticMemberOperator extends MemberFunction, TaintFunction {
238241
IteratorBinaryArithmeticMemberOperator() {
239-
this.hasName(["operator+", "operator-"]) and
240-
this.getDeclaringType() instanceof Iterator
242+
this.getClassAndName(["operator+", "operator-"]) instanceof Iterator
241243
}
242244

243245
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
@@ -252,21 +254,24 @@ private class IteratorBinaryArithmeticMemberOperator extends MemberFunction, Tai
252254
private class IteratorAssignArithmeticMemberOperator extends MemberFunction, DataFlowFunction,
253255
TaintFunction {
254256
IteratorAssignArithmeticMemberOperator() {
255-
this.hasName(["operator+=", "operator-="]) and
256-
this.getDeclaringType() instanceof Iterator
257+
this.getClassAndName(["operator+=", "operator-="]) instanceof Iterator
257258
}
258259

259260
override predicate hasDataFlow(FunctionInput input, FunctionOutput output) {
260261
input.isQualifierAddress() and
261262
output.isReturnValue()
262-
or
263-
input.isReturnValueDeref() and
264-
output.isQualifierObject()
265263
}
266264

267265
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
268266
input.isQualifierObject() and
269267
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()
270275
}
271276
}
272277

@@ -275,10 +280,7 @@ private class IteratorAssignArithmeticMemberOperator extends MemberFunction, Dat
275280
*/
276281
private class IteratorArrayMemberOperator extends MemberFunction, TaintFunction,
277282
IteratorReferenceFunction {
278-
IteratorArrayMemberOperator() {
279-
this.hasName("operator[]") and
280-
this.getDeclaringType() instanceof Iterator
281-
}
283+
IteratorArrayMemberOperator() { this.getClassAndName("operator[]") instanceof Iterator }
282284

283285
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
284286
input.isQualifierObject() and
@@ -295,8 +297,7 @@ private class IteratorArrayMemberOperator extends MemberFunction, TaintFunction,
295297
*/
296298
private class IteratorAssignmentMemberOperator extends MemberFunction, TaintFunction {
297299
IteratorAssignmentMemberOperator() {
298-
this.hasName("operator=") and
299-
this.getDeclaringType() instanceof Iterator and
300+
this.getClassAndName("operator=") instanceof Iterator and
300301
not this instanceof CopyAssignmentOperator and
301302
not this instanceof MoveAssignmentOperator
302303
}
@@ -337,7 +338,7 @@ private class BeginOrEndFunction extends MemberFunction, TaintFunction, GetItera
337338
*/
338339
private class InserterIteratorFunction extends GetIteratorFunction {
339340
InserterIteratorFunction() {
340-
this.hasQualifiedName("std", ["front_inserter", "inserter", "back_inserter"])
341+
this.hasQualifiedName(["std", "bsl"], ["front_inserter", "inserter", "back_inserter"])
341342
}
342343

343344
override predicate getsIterator(FunctionInput input, FunctionOutput output) {

0 commit comments

Comments
 (0)