Skip to content

Commit 018b0a5

Browse files
committed
C++: Model std::string front, back and push_back.
1 parent 6e734a8 commit 018b0a5

File tree

5 files changed

+38
-3
lines changed

5 files changed

+38
-3
lines changed

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Provides implementation classes modeling `std::string` and other
3-
* instantiations of`std::basic_string`. See `semmle.code.cpp.models.Models`
3+
* instantiations of `std::basic_string`. See `semmle.code.cpp.models.Models`
44
* for usage information.
55
*/
66

@@ -82,6 +82,32 @@ class StdStringData extends TaintFunction {
8282
}
8383
}
8484

85+
/**
86+
* The `std::string` function `push_back`.
87+
*/
88+
class StdStringPush extends TaintFunction {
89+
StdStringPush() { this.hasQualifiedName("std", "basic_string", "push_back") }
90+
91+
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
92+
// flow from parameter to qualifier
93+
input.isParameterDeref(0) and
94+
output.isQualifierObject()
95+
}
96+
}
97+
98+
/**
99+
* The `std::string` functions `front` and `back`.
100+
*/
101+
class StdStringFrontBack extends TaintFunction {
102+
StdStringFrontBack() { this.hasQualifiedName("std", "basic_string", ["front", "back"]) }
103+
104+
override predicate hasTaintFlow(FunctionInput input, FunctionOutput output) {
105+
// flow from object to returned reference
106+
input.isQualifierObject() and
107+
output.isReturnValueDeref()
108+
}
109+
}
110+
85111
/**
86112
* The `std::string` function `operator+`.
87113
*/

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,16 +1243,21 @@
12431243
| string.cpp:510:16:510:20 | call to basic_string | string.cpp:514:2:514:2 | a | |
12441244
| string.cpp:510:16:510:20 | call to basic_string | string.cpp:515:7:515:7 | a | |
12451245
| string.cpp:510:16:510:20 | call to basic_string | string.cpp:516:7:516:7 | a | |
1246+
| string.cpp:512:7:512:7 | a | string.cpp:512:9:512:13 | call to front | TAINT |
12461247
| string.cpp:512:7:512:7 | ref arg a | string.cpp:513:7:513:7 | a | |
12471248
| string.cpp:512:7:512:7 | ref arg a | string.cpp:514:2:514:2 | a | |
12481249
| string.cpp:512:7:512:7 | ref arg a | string.cpp:515:7:515:7 | a | |
12491250
| string.cpp:512:7:512:7 | ref arg a | string.cpp:516:7:516:7 | a | |
1251+
| string.cpp:513:7:513:7 | a | string.cpp:513:9:513:12 | call to back | TAINT |
12501252
| string.cpp:513:7:513:7 | ref arg a | string.cpp:514:2:514:2 | a | |
12511253
| string.cpp:513:7:513:7 | ref arg a | string.cpp:515:7:515:7 | a | |
12521254
| string.cpp:513:7:513:7 | ref arg a | string.cpp:516:7:516:7 | a | |
12531255
| string.cpp:514:2:514:2 | ref arg a | string.cpp:515:7:515:7 | a | |
12541256
| string.cpp:514:2:514:2 | ref arg a | string.cpp:516:7:516:7 | a | |
1257+
| string.cpp:514:14:514:28 | call to source | string.cpp:514:2:514:2 | ref arg a | TAINT |
1258+
| string.cpp:515:7:515:7 | a | string.cpp:515:9:515:13 | call to front | TAINT |
12551259
| string.cpp:515:7:515:7 | ref arg a | string.cpp:516:7:516:7 | a | |
1260+
| string.cpp:516:7:516:7 | a | string.cpp:516:9:516:12 | call to back | TAINT |
12561261
| string.cpp:521:17:521:20 | aa | string.cpp:521:17:521:21 | call to basic_string | TAINT |
12571262
| string.cpp:521:17:521:21 | call to basic_string | string.cpp:528:9:528:9 | a | |
12581263
| string.cpp:521:17:521:21 | call to basic_string | string.cpp:532:8:532:8 | a | |

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,8 +512,8 @@ void test_string_front_back() {
512512
sink(a.front());
513513
sink(a.back());
514514
a.push_back(ns_char::source());
515-
sink(a.front());
516-
sink(a.back()); // tainted [NOT DETECTED]
515+
sink(a.front()); // [FALSE POSITIVE]
516+
sink(a.back()); // tainted
517517
}
518518

519519
void test_string_return_assign() {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@
141141
| string.cpp:491:8:491:9 | s6 | string.cpp:482:18:482:23 | call to source |
142142
| string.cpp:504:7:504:8 | s2 | string.cpp:497:14:497:19 | call to source |
143143
| string.cpp:506:7:506:8 | s4 | string.cpp:497:14:497:19 | call to source |
144+
| string.cpp:515:9:515:13 | call to front | string.cpp:514:14:514:28 | call to source |
145+
| string.cpp:516:9:516:12 | call to back | string.cpp:514:14:514:28 | call to source |
144146
| string.cpp:529:11:529:11 | call to operator+= | string.cpp:529:20:529:25 | call to source |
145147
| string.cpp:530:21:530:21 | call to operator+= | string.cpp:530:24:530:29 | call to source |
146148
| string.cpp:531:25:531:25 | call to operator+= | string.cpp:531:15:531:20 | call to source |

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@
151151
| string.cpp:491:8:491:9 | string.cpp:482:18:482:23 | AST only |
152152
| string.cpp:504:7:504:8 | string.cpp:497:14:497:19 | AST only |
153153
| string.cpp:506:7:506:8 | string.cpp:497:14:497:19 | AST only |
154+
| string.cpp:515:9:515:13 | string.cpp:514:14:514:28 | AST only |
155+
| string.cpp:516:9:516:12 | string.cpp:514:14:514:28 | AST only |
154156
| string.cpp:529:11:529:11 | string.cpp:529:20:529:25 | AST only |
155157
| string.cpp:530:21:530:21 | string.cpp:530:24:530:29 | AST only |
156158
| string.cpp:531:25:531:25 | string.cpp:531:15:531:20 | AST only |

0 commit comments

Comments
 (0)