Skip to content

Commit f7f8dd4

Browse files
authored
Merge pull request #5156 from geoffw0/modelsbsl
C++: Improve StdSet and StdPair models
2 parents 92df1f7 + 04f15ad commit f7f8dd4

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,26 @@ import semmle.code.cpp.models.interfaces.Taint
77
/**
88
* An instantiation of `std::pair<T1, T2>`.
99
*/
10-
class StdPairClass extends ClassTemplateInstantiation {
11-
StdPairClass() { getTemplate().hasQualifiedName("std", "pair") }
10+
private class StdPair extends ClassTemplateInstantiation {
11+
StdPair() { this.hasQualifiedName(["std", "bsl"], "pair") }
1212
}
1313

14+
/**
15+
* DEPRECATED: This is now called `StdPair` and is a private part of the
16+
* library implementation.
17+
*/
18+
deprecated class StdPairClass = StdPair;
19+
1420
/**
1521
* Any of the single-parameter constructors of `std::pair` that takes a reference to an
1622
* instantiation of `std::pair`. These constructors allow conversion between pair types when the
1723
* underlying element types are convertible.
1824
*/
1925
class StdPairCopyishConstructor extends Constructor, TaintFunction {
2026
StdPairCopyishConstructor() {
21-
this.getDeclaringType() instanceof StdPairClass and
27+
this.getDeclaringType() instanceof StdPair and
2228
this.getNumberOfParameters() = 1 and
23-
this.getParameter(0).getUnspecifiedType().(ReferenceType).getBaseType() instanceof StdPairClass
29+
this.getParameter(0).getUnspecifiedType().(ReferenceType).getBaseType() instanceof StdPair
2430
}
2531

2632
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
@@ -38,7 +44,7 @@ class StdPairCopyishConstructor extends Constructor, TaintFunction {
3844
* Additional model for `std::pair` constructors.
3945
*/
4046
private class StdPairConstructor extends Constructor, TaintFunction {
41-
StdPairConstructor() { this.hasQualifiedName("std", "pair", "pair") }
47+
StdPairConstructor() { this.getDeclaringType() instanceof StdPair }
4248

4349
/**
4450
* Gets the index of a parameter to this function that is a reference to

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

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@
55
import semmle.code.cpp.models.interfaces.Taint
66
import semmle.code.cpp.models.interfaces.Iterator
77

8+
/**
9+
* An instantiation of `std::set` or `std::unordered_set`.
10+
*/
11+
private class StdSet extends ClassTemplateInstantiation {
12+
StdSet() { this.hasQualifiedName(["std", "bsl"], ["set", "unordered_set"]) }
13+
}
14+
815
/**
916
* Additional model for set constructors using iterator inputs.
1017
*/
1118
private class StdSetConstructor extends Constructor, TaintFunction {
12-
StdSetConstructor() {
13-
this.hasQualifiedName("std", "set", "set") or
14-
this.hasQualifiedName("std", "unordered_set", "unordered_set")
15-
}
19+
StdSetConstructor() { this.getDeclaringType() instanceof StdSet }
1620

1721
/**
1822
* Gets the index of a parameter to this function that is an iterator.
@@ -36,7 +40,7 @@ private class StdSetConstructor extends Constructor, TaintFunction {
3640
* The standard set `insert` and `insert_or_assign` functions.
3741
*/
3842
private class StdSetInsert extends TaintFunction {
39-
StdSetInsert() { this.hasQualifiedName("std", ["set", "unordered_set"], "insert") }
43+
StdSetInsert() { this.getClassAndName("insert") instanceof StdSet }
4044

4145
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
4246
// flow from last parameter to qualifier and return value
@@ -53,9 +57,7 @@ private class StdSetInsert extends TaintFunction {
5357
* The standard set `emplace` and `emplace_hint` functions.
5458
*/
5559
private class StdSetEmplace extends TaintFunction {
56-
StdSetEmplace() {
57-
this.hasQualifiedName("std", ["set", "unordered_set"], ["emplace", "emplace_hint"])
58-
}
60+
StdSetEmplace() { this.getClassAndName(["emplace", "emplace_hint"]) instanceof StdSet }
5961

6062
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
6163
// flow from any parameter to qualifier and return value
@@ -76,7 +78,7 @@ private class StdSetEmplace extends TaintFunction {
7678
* The standard set `merge` function.
7779
*/
7880
private class StdSetMerge extends TaintFunction {
79-
StdSetMerge() { this.hasQualifiedName("std", ["set", "unordered_set"], "merge") }
81+
StdSetMerge() { this.getClassAndName("merge") instanceof StdSet }
8082

8183
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
8284
// container1.merge(container2)
@@ -89,7 +91,7 @@ private class StdSetMerge extends TaintFunction {
8991
* The standard set `find` function.
9092
*/
9193
private class StdSetFind extends TaintFunction {
92-
StdSetFind() { this.hasQualifiedName("std", ["set", "unordered_set"], "find") }
94+
StdSetFind() { this.getClassAndName("find") instanceof StdSet }
9395

9496
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
9597
input.isQualifierObject() and
@@ -101,7 +103,7 @@ private class StdSetFind extends TaintFunction {
101103
* The standard set `erase` function.
102104
*/
103105
private class StdSetErase extends TaintFunction {
104-
StdSetErase() { this.hasQualifiedName("std", ["set", "unordered_set"], "erase") }
106+
StdSetErase() { this.getClassAndName("erase") instanceof StdSet }
105107

106108
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
107109
// flow from qualifier to iterator return value
@@ -116,8 +118,7 @@ private class StdSetErase extends TaintFunction {
116118
*/
117119
private class StdSetEqualRange extends TaintFunction {
118120
StdSetEqualRange() {
119-
this.hasQualifiedName("std", ["set", "unordered_set"],
120-
["lower_bound", "upper_bound", "equal_range"])
121+
this.getClassAndName(["lower_bound", "upper_bound", "equal_range"]) instanceof StdSet
121122
}
122123

123124
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {

0 commit comments

Comments
 (0)