Skip to content

Commit 111da4c

Browse files
committed
C++: Add a model of std::vector::assign.
1 parent 0952fb9 commit 111da4c

File tree

5 files changed

+33
-1
lines changed

5 files changed

+33
-1
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,32 @@ class StdSequenceContainerFrontBack extends TaintFunction {
8888
}
8989
}
9090

91+
/**
92+
* The standard container function `assign`.
93+
*/
94+
class StdSequenceContainerAssign extends TaintFunction {
95+
StdSequenceContainerAssign() {
96+
this.hasQualifiedName("std", ["vector", "deque", "list", "forward_list"], "assign")
97+
}
98+
99+
/**
100+
* Gets the index of a parameter to this function that is a reference to the
101+
* value type of the container.
102+
*/
103+
int getAValueTypeParameterIndex() {
104+
getParameter(result).getUnspecifiedType() = getDeclaringType().getTemplateArgument(0) // i.e. the `T` of this `std::vector<T>`
105+
or
106+
getParameter(result).getUnspecifiedType().(ReferenceType).getBaseType() =
107+
getDeclaringType().getTemplateArgument(0)
108+
}
109+
110+
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
111+
// flow from parameter to string itself (qualifier) and return value
112+
input.isParameterDeref(getAValueTypeParameterIndex()) and
113+
output.isQualifierObject()
114+
}
115+
}
116+
91117
/**
92118
* The standard container `swap` functions.
93119
*/

cpp/ql/test/library-tests/dataflow/taint-tests/localTaint.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2244,8 +2244,12 @@
22442244
| vector.cpp:221:2:221:3 | ref arg v1 | vector.cpp:233:13:233:14 | v1 | |
22452245
| vector.cpp:221:2:221:3 | ref arg v1 | vector.cpp:233:25:233:26 | v1 | |
22462246
| vector.cpp:221:2:221:3 | ref arg v1 | vector.cpp:247:1:247:1 | v1 | |
2247+
| vector.cpp:221:12:221:14 | 100 | vector.cpp:221:2:221:3 | ref arg v1 | TAINT |
2248+
| vector.cpp:221:17:221:17 | 0 | vector.cpp:221:2:221:3 | ref arg v1 | TAINT |
22472249
| vector.cpp:222:2:222:3 | ref arg v2 | vector.cpp:226:7:226:8 | v2 | |
22482250
| vector.cpp:222:2:222:3 | ref arg v2 | vector.cpp:247:1:247:1 | v2 | |
2251+
| vector.cpp:222:12:222:14 | 100 | vector.cpp:222:2:222:3 | ref arg v2 | TAINT |
2252+
| vector.cpp:222:17:222:30 | call to source | vector.cpp:222:2:222:3 | ref arg v2 | TAINT |
22492253
| vector.cpp:223:2:223:3 | ref arg v3 | vector.cpp:227:7:227:8 | v3 | |
22502254
| vector.cpp:223:2:223:3 | ref arg v3 | vector.cpp:234:13:234:14 | v3 | |
22512255
| vector.cpp:223:2:223:3 | ref arg v3 | vector.cpp:234:25:234:26 | v3 | |

cpp/ql/test/library-tests/dataflow/taint-tests/taint.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@
241241
| vector.cpp:171:13:171:13 | call to operator[] | vector.cpp:170:14:170:19 | call to source |
242242
| vector.cpp:180:13:180:13 | call to operator[] | vector.cpp:179:14:179:19 | call to source |
243243
| vector.cpp:201:13:201:13 | call to operator[] | vector.cpp:200:14:200:19 | call to source |
244+
| vector.cpp:226:7:226:8 | v2 | vector.cpp:222:17:222:30 | call to source |
244245
| vector.cpp:227:7:227:8 | v3 | vector.cpp:223:15:223:20 | call to source |
245246
| vector.cpp:255:7:255:8 | v1 | vector.cpp:254:15:254:20 | call to source |
246247
| vector.cpp:256:10:256:13 | call to data | vector.cpp:254:15:254:20 | call to source |

cpp/ql/test/library-tests/dataflow/taint-tests/test_diff.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@
177177
| vector.cpp:171:13:171:13 | vector.cpp:170:14:170:19 | AST only |
178178
| vector.cpp:180:13:180:13 | vector.cpp:179:14:179:19 | AST only |
179179
| vector.cpp:201:13:201:13 | vector.cpp:200:14:200:19 | AST only |
180+
| vector.cpp:226:7:226:8 | vector.cpp:222:17:222:30 | AST only |
180181
| vector.cpp:227:7:227:8 | vector.cpp:223:15:223:20 | AST only |
181182
| vector.cpp:255:7:255:8 | vector.cpp:254:15:254:20 | AST only |
182183
| vector.cpp:256:10:256:13 | vector.cpp:254:15:254:20 | AST only |

cpp/ql/test/library-tests/dataflow/taint-tests/vector.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ void test_vector_assign() {
223223
v3.push_back(source());
224224

225225
sink(v1);
226-
sink(v2); // tainted [NOT DETECTED]
226+
sink(v2); // tainted
227227
sink(v3); // tainted
228228

229229
{

0 commit comments

Comments
 (0)