Skip to content

Commit 86268d4

Browse files
authored
C++: Refactor StdContainer.qll.
1 parent 9b8d94d commit 86268d4

File tree

1 file changed

+105
-12
lines changed

1 file changed

+105
-12
lines changed

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

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

8+
/**
9+
* The `std::array` template class.
10+
*/
11+
private class Array extends Class {
12+
Array() { this.hasQualifiedName(["std", "bsl"], "array") }
13+
}
14+
15+
/**
16+
* The `std::deque` template class.
17+
*/
18+
private class Deque extends Class {
19+
Deque() { this.hasQualifiedName(["std", "bsl"], "deque") }
20+
}
21+
22+
/**
23+
* The `std::forward_list` template class.
24+
*/
25+
private class ForwardList extends Class {
26+
ForwardList() { this.hasQualifiedName(["std", "bsl"], "forward_list") }
27+
}
28+
29+
/**
30+
* The `std::list` template class.
31+
*/
32+
private class List extends Class {
33+
List() { this.hasQualifiedName(["std", "bsl"], "list") }
34+
}
35+
36+
/**
37+
* The `std::vector` template class.
38+
*/
39+
private class Vector extends Class {
40+
Vector() { this.hasQualifiedName(["std", "bsl"], "vector") }
41+
}
42+
843
/**
944
* Additional model for standard container constructors that reference the
1045
* value type of the container (that is, the `T` in `std::vector<T>`). For
@@ -15,7 +50,10 @@ import semmle.code.cpp.models.interfaces.Iterator
1550
*/
1651
private class StdSequenceContainerConstructor extends Constructor, TaintFunction {
1752
StdSequenceContainerConstructor() {
18-
this.getDeclaringType().hasQualifiedName("std", ["vector", "deque", "list", "forward_list"])
53+
this.getDeclaringType() instanceof Vector or
54+
this.getDeclaringType() instanceof Deque or
55+
this.getDeclaringType() instanceof List or
56+
this.getDeclaringType() instanceof ForwardList
1957
}
2058

2159
/**
@@ -50,7 +88,13 @@ private class StdSequenceContainerConstructor extends Constructor, TaintFunction
5088
* The standard container function `data`.
5189
*/
5290
private class StdSequenceContainerData extends TaintFunction {
53-
StdSequenceContainerData() { this.hasQualifiedName("std", ["array", "vector"], "data") }
91+
StdSequenceContainerData() {
92+
this.hasName("data") and
93+
(
94+
this.getDeclaringType() instanceof Vector or
95+
this.getDeclaringType() instanceof Array
96+
)
97+
}
5498

5599
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
56100
// flow from container itself (qualifier) to return value
@@ -69,8 +113,19 @@ private class StdSequenceContainerData extends TaintFunction {
69113
*/
70114
private class StdSequenceContainerPush extends TaintFunction {
71115
StdSequenceContainerPush() {
72-
this.hasQualifiedName("std", ["vector", "deque", "list"], "push_back") or
73-
this.hasQualifiedName("std", ["deque", "list", "forward_list"], "push_front")
116+
this.hasName("push_back") and
117+
(
118+
this.getDeclaringType() instanceof Array or
119+
this.getDeclaringType() instanceof Deque or
120+
this.getDeclaringType() instanceof List
121+
)
122+
or
123+
this.hasName("push_front") and
124+
(
125+
this.getDeclaringType() instanceof Deque or
126+
this.getDeclaringType() instanceof ForwardList or
127+
this.getDeclaringType() instanceof List
128+
)
74129
}
75130

76131
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
@@ -85,8 +140,22 @@ private class StdSequenceContainerPush extends TaintFunction {
85140
*/
86141
private class StdSequenceContainerFrontBack extends TaintFunction {
87142
StdSequenceContainerFrontBack() {
88-
this.hasQualifiedName("std", ["array", "vector", "deque", "list", "forward_list"], "front") or
89-
this.hasQualifiedName("std", ["array", "vector", "deque", "list"], "back")
143+
this.hasName("front") and
144+
(
145+
this.getDeclaringType() instanceof Array or
146+
this.getDeclaringType() instanceof Deque or
147+
this.getDeclaringType() instanceof ForwardList or
148+
this.getDeclaringType() instanceof List or
149+
this.getDeclaringType() instanceof Vector
150+
)
151+
or
152+
this.hasName("back") and
153+
(
154+
this.getDeclaringType() instanceof Array or
155+
this.getDeclaringType() instanceof Deque or
156+
this.getDeclaringType() instanceof List or
157+
this.getDeclaringType() instanceof Vector
158+
)
90159
}
91160

92161
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
@@ -101,8 +170,15 @@ private class StdSequenceContainerFrontBack extends TaintFunction {
101170
*/
102171
private class StdSequenceContainerInsert extends TaintFunction {
103172
StdSequenceContainerInsert() {
104-
this.hasQualifiedName("std", ["vector", "deque", "list"], "insert") or
105-
this.hasQualifiedName("std", "forward_list", "insert_after")
173+
this.hasName("insert") and
174+
(
175+
this.getDeclaringType() instanceof Deque or
176+
this.getDeclaringType() instanceof List or
177+
this.getDeclaringType() instanceof Vector
178+
)
179+
or
180+
this.hasName("insert_after") and
181+
this.getDeclaringType() instanceof ForwardList
106182
}
107183

108184
/**
@@ -138,7 +214,13 @@ private class StdSequenceContainerInsert extends TaintFunction {
138214
*/
139215
private class StdSequenceContainerAssign extends TaintFunction {
140216
StdSequenceContainerAssign() {
141-
this.hasQualifiedName("std", ["vector", "deque", "list", "forward_list"], "assign")
217+
this.hasName("assign") and
218+
(
219+
this.getDeclaringType() instanceof Deque or
220+
this.getDeclaringType() instanceof ForwardList or
221+
this.getDeclaringType() instanceof List or
222+
this.getDeclaringType() instanceof Vector
223+
)
142224
}
143225

144226
/**
@@ -170,7 +252,12 @@ private class StdSequenceContainerAssign extends TaintFunction {
170252
*/
171253
private class StdSequenceContainerAt extends TaintFunction {
172254
StdSequenceContainerAt() {
173-
this.hasQualifiedName("std", ["vector", "array", "deque"], ["at", "operator[]"])
255+
this.hasName(["at", "operator[]"]) and
256+
(
257+
this.getDeclaringType() instanceof Array or
258+
this.getDeclaringType() instanceof Deque or
259+
this.getDeclaringType() instanceof Vector
260+
)
174261
}
175262

176263
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
@@ -188,7 +275,10 @@ private class StdSequenceContainerAt extends TaintFunction {
188275
* The standard vector `emplace` function.
189276
*/
190277
class StdVectorEmplace extends TaintFunction {
191-
StdVectorEmplace() { this.hasQualifiedName("std", "vector", "emplace") }
278+
StdVectorEmplace() {
279+
this.hasName("emplace") and
280+
this.getDeclaringType() instanceof Vector
281+
}
192282

193283
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
194284
// flow from any parameter except the position iterator to qualifier and return value
@@ -205,7 +295,10 @@ class StdVectorEmplace extends TaintFunction {
205295
* The standard vector `emplace_back` function.
206296
*/
207297
class StdVectorEmplaceBack extends TaintFunction {
208-
StdVectorEmplaceBack() { this.hasQualifiedName("std", "vector", "emplace_back") }
298+
StdVectorEmplaceBack() {
299+
this.hasName("emplace_back") and
300+
this.getDeclaringType() instanceof Vector
301+
}
209302

210303
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
211304
// flow from any parameter to qualifier

0 commit comments

Comments
 (0)