Skip to content

Commit cc72fc8

Browse files
committed
Merge branch 'main' into flask-clean-models
2 parents 9a42f2f + f3814c6 commit cc72fc8

File tree

440 files changed

+3678
-1471
lines changed

Some content is hidden

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

440 files changed

+3678
-1471
lines changed

CONTRIBUTING.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ If you have an idea for a query that you would like to share with other CodeQL u
4949

5050
- The query must have at least one true positive result on some revision of a real project.
5151

52-
Experimental queries and libraries may not be actively maintained as the [supported](docs/supported-queries.md) libraries evolve. They may also be changed in backwards-incompatible ways or may be removed entirely in the future without deprecation warnings.
52+
6. **Query help files and unit tests**
53+
54+
- Query help (`.qhelp`) files and unit tests are optional (but strongly encouraged!) for queries in the `experimental` directories. For more information about contributing query help files and unit tests, see [Supported CodeQL queries and libraries](docs/supported-queries.md).
55+
56+
Experimental queries and libraries may not be actively maintained as the supported libraries evolve. They may also be changed in backwards-incompatible ways or may be removed entirely in the future without deprecation warnings.
5357

5458
After the experimental query is merged, we welcome pull requests to improve it. Before a query can be moved out of the `experimental` subdirectory, it must satisfy [the requirements for being a supported query](docs/supported-queries.md).
5559

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) {

cpp/ql/test/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# C/C++ CodeQL tests
22

3-
This document provides additional information about the C/C++ CodeQL Tests located in `cpp/ql/test`. See [Contributing to CodeQL](/CONTRIBUTING.md) for general information about contributing to this repository.
3+
This document provides additional information about the C/C++ CodeQL tests located in `cpp/ql/test`. The principles under "Copying code", below, also apply to any other C/C++ code in this repository, such as examples linked from query `.qhelp` files in `cpp/ql/src`. For more general information about contributing to this repository, see [Contributing to CodeQL](/CONTRIBUTING.md).
44

55
The tests can be run through Visual Studio Code. Advanced users may also use the `codeql test run` command.
66

0 commit comments

Comments
 (0)