@@ -16,21 +16,14 @@ private class StdBasicString extends ClassTemplateInstantiation {
16
16
}
17
17
18
18
/**
19
- * Additional model for `std::string` constructors that reference the character
20
- * type of the container, or an iterator. For example construction from
21
- * iterators:
22
- * ```
23
- * std::string b(a.begin(), a.end());
24
- * ```
19
+ * A `std::string` function for which taint should be propagated.
25
20
*/
26
- private class StdStringConstructor extends Constructor , TaintFunction {
27
- StdStringConstructor ( ) { this .getDeclaringType ( ) instanceof StdBasicString }
28
-
21
+ abstract private class StdStringTaintFunction extends TaintFunction {
29
22
/**
30
23
* Gets the index of a parameter to this function that is a string (or
31
24
* character).
32
25
*/
33
- int getAStringParameterIndex ( ) {
26
+ final int getAStringParameterIndex ( ) {
34
27
exists ( Type paramType | paramType = this .getParameter ( result ) .getUnspecifiedType ( ) |
35
28
// e.g. `std::basic_string::CharT *`
36
29
paramType instanceof PointerType
@@ -41,15 +34,28 @@ private class StdStringConstructor extends Constructor, TaintFunction {
41
34
this .getDeclaringType ( ) .getTemplateArgument ( 2 ) .( Type ) .getUnspecifiedType ( )
42
35
or
43
36
// i.e. `std::basic_string::CharT`
44
- this .getParameter ( result ) .getUnspecifiedType ( ) =
45
- this .getDeclaringType ( ) .getTemplateArgument ( 0 ) .( Type ) .getUnspecifiedType ( )
37
+ paramType = this .getDeclaringType ( ) .getTemplateArgument ( 0 ) .( Type ) .getUnspecifiedType ( )
46
38
)
47
39
}
48
40
49
41
/**
50
42
* Gets the index of a parameter to this function that is an iterator.
51
43
*/
52
- int getAnIteratorParameterIndex ( ) { this .getParameter ( result ) .getType ( ) instanceof Iterator }
44
+ final int getAnIteratorParameterIndex ( ) {
45
+ this .getParameter ( result ) .getType ( ) instanceof Iterator
46
+ }
47
+ }
48
+
49
+ /**
50
+ * Additional model for `std::string` constructors that reference the character
51
+ * type of the container, or an iterator. For example construction from
52
+ * iterators:
53
+ * ```
54
+ * std::string b(a.begin(), a.end());
55
+ * ```
56
+ */
57
+ private class StdStringConstructor extends Constructor , StdStringTaintFunction {
58
+ StdStringConstructor ( ) { this .getDeclaringType ( ) instanceof StdBasicString }
53
59
54
60
override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
55
61
// taint flow from any parameter of the value type to the returned object
@@ -68,7 +74,7 @@ private class StdStringConstructor extends Constructor, TaintFunction {
68
74
/**
69
75
* The `std::string` function `c_str`.
70
76
*/
71
- private class StdStringCStr extends TaintFunction {
77
+ private class StdStringCStr extends StdStringTaintFunction {
72
78
StdStringCStr ( ) { this .getClassAndName ( "c_str" ) instanceof StdBasicString }
73
79
74
80
override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
@@ -81,7 +87,7 @@ private class StdStringCStr extends TaintFunction {
81
87
/**
82
88
* The `std::string` function `data`.
83
89
*/
84
- private class StdStringData extends TaintFunction {
90
+ private class StdStringData extends StdStringTaintFunction {
85
91
StdStringData ( ) { this .getClassAndName ( "data" ) instanceof StdBasicString }
86
92
87
93
override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
@@ -99,7 +105,7 @@ private class StdStringData extends TaintFunction {
99
105
/**
100
106
* The `std::string` function `push_back`.
101
107
*/
102
- private class StdStringPush extends TaintFunction {
108
+ private class StdStringPush extends StdStringTaintFunction {
103
109
StdStringPush ( ) { this .getClassAndName ( "push_back" ) instanceof StdBasicString }
104
110
105
111
override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
@@ -112,7 +118,7 @@ private class StdStringPush extends TaintFunction {
112
118
/**
113
119
* The `std::string` functions `front` and `back`.
114
120
*/
115
- private class StdStringFrontBack extends TaintFunction {
121
+ private class StdStringFrontBack extends StdStringTaintFunction {
116
122
StdStringFrontBack ( ) { this .getClassAndName ( [ "front" , "back" ] ) instanceof StdBasicString }
117
123
118
124
override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
@@ -125,7 +131,7 @@ private class StdStringFrontBack extends TaintFunction {
125
131
/**
126
132
* The (non-member) `std::string` function `operator+`.
127
133
*/
128
- private class StdStringPlus extends TaintFunction {
134
+ private class StdStringPlus extends StdStringTaintFunction {
129
135
StdStringPlus ( ) {
130
136
this .hasQualifiedName ( [ "std" , "bsl" ] , "operator+" ) and
131
137
this .getUnspecifiedType ( ) instanceof StdBasicString
@@ -142,31 +148,15 @@ private class StdStringPlus extends TaintFunction {
142
148
}
143
149
144
150
/**
145
- * The `std::string` functions `operator+=`, `append`, `insert` and
146
- * `replace`. All of these functions combine the existing string
147
- * with a new string (or character) from one of the arguments.
151
+ * The `std::string` functions `operator+=`, `append` and `replace`.
152
+ * All of these functions combine the existing string with a new
153
+ * string (or character) from one of the arguments.
148
154
*/
149
- private class StdStringAppend extends TaintFunction {
155
+ private class StdStringAppend extends StdStringTaintFunction {
150
156
StdStringAppend ( ) {
151
- this .getClassAndName ( [ "operator+=" , "append" , "insert" , " replace"] ) instanceof StdBasicString
157
+ this .getClassAndName ( [ "operator+=" , "append" , "replace" ] ) instanceof StdBasicString
152
158
}
153
159
154
- /**
155
- * Gets the index of a parameter to this function that is a string (or
156
- * character).
157
- */
158
- int getAStringParameterIndex ( ) {
159
- this .getParameter ( result ) .getType ( ) instanceof PointerType or // e.g. `std::basic_string::CharT *`
160
- this .getParameter ( result ) .getType ( ) instanceof ReferenceType or // e.g. `std::basic_string &`
161
- this .getParameter ( result ) .getUnspecifiedType ( ) =
162
- this .getDeclaringType ( ) .getTemplateArgument ( 0 ) .( Type ) .getUnspecifiedType ( ) // i.e. `std::basic_string::CharT`
163
- }
164
-
165
- /**
166
- * Gets the index of a parameter to this function that is an iterator.
167
- */
168
- int getAnIteratorParameterIndex ( ) { this .getParameter ( result ) .getType ( ) instanceof Iterator }
169
-
170
160
override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
171
161
// flow from string and parameter to string (qualifier) and return value
172
162
(
@@ -187,26 +177,42 @@ private class StdStringAppend extends TaintFunction {
187
177
}
188
178
189
179
/**
190
- * The standard function `std::string.assign `.
180
+ * The `std::string` function `insert `.
191
181
*/
192
- private class StdStringAssign extends TaintFunction {
193
- StdStringAssign ( ) { this .getClassAndName ( "assign " ) instanceof StdBasicString }
182
+ private class StdStringInsert extends StdStringTaintFunction {
183
+ StdStringInsert ( ) { this .getClassAndName ( "insert " ) instanceof StdBasicString }
194
184
195
185
/**
196
- * Gets the index of a parameter to this function that is a string (or
197
- * character).
186
+ * Holds if the return type is an iterator.
198
187
*/
199
- int getAStringParameterIndex ( ) {
200
- this .getParameter ( result ) .getType ( ) instanceof PointerType or // e.g. `std::basic_string::CharT *`
201
- this .getParameter ( result ) .getType ( ) instanceof ReferenceType or // e.g. `std::basic_string &`
202
- this .getParameter ( result ) .getUnspecifiedType ( ) =
203
- this .getDeclaringType ( ) .getTemplateArgument ( 0 ) .( Type ) .getUnspecifiedType ( ) // i.e. `std::basic_string::CharT`
188
+ predicate hasIteratorReturnValue ( ) { this .getType ( ) instanceof Iterator }
189
+
190
+ override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
191
+ // flow from string and parameter to string (qualifier) and return value
192
+ (
193
+ input .isQualifierObject ( ) or
194
+ input .isParameterDeref ( this .getAStringParameterIndex ( ) ) or
195
+ input .isParameter ( this .getAnIteratorParameterIndex ( ) )
196
+ ) and
197
+ (
198
+ output .isQualifierObject ( )
199
+ or
200
+ if this .hasIteratorReturnValue ( ) then output .isReturnValue ( ) else output .isReturnValueDeref ( )
201
+ )
202
+ or
203
+ // reverse flow from returned reference to the qualifier (for writes to
204
+ // the result)
205
+ not this .hasIteratorReturnValue ( ) and
206
+ input .isReturnValueDeref ( ) and
207
+ output .isQualifierObject ( )
204
208
}
209
+ }
205
210
206
- /**
207
- * Gets the index of a parameter to this function that is an iterator.
208
- */
209
- int getAnIteratorParameterIndex ( ) { this .getParameter ( result ) .getType ( ) instanceof Iterator }
211
+ /**
212
+ * The standard function `std::string.assign`.
213
+ */
214
+ private class StdStringAssign extends StdStringTaintFunction {
215
+ StdStringAssign ( ) { this .getClassAndName ( "assign" ) instanceof StdBasicString }
210
216
211
217
override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
212
218
// flow from parameter to string itself (qualifier) and return value
@@ -229,7 +235,7 @@ private class StdStringAssign extends TaintFunction {
229
235
/**
230
236
* The standard function `std::string.copy`.
231
237
*/
232
- private class StdStringCopy extends TaintFunction {
238
+ private class StdStringCopy extends StdStringTaintFunction {
233
239
StdStringCopy ( ) { this .getClassAndName ( "copy" ) instanceof StdBasicString }
234
240
235
241
override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
@@ -242,7 +248,7 @@ private class StdStringCopy extends TaintFunction {
242
248
/**
243
249
* The standard function `std::string.substr`.
244
250
*/
245
- private class StdStringSubstr extends TaintFunction {
251
+ private class StdStringSubstr extends StdStringTaintFunction {
246
252
StdStringSubstr ( ) { this .getClassAndName ( "substr" ) instanceof StdBasicString }
247
253
248
254
override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
@@ -255,7 +261,7 @@ private class StdStringSubstr extends TaintFunction {
255
261
/**
256
262
* The `std::string` functions `at` and `operator[]`.
257
263
*/
258
- private class StdStringAt extends TaintFunction {
264
+ private class StdStringAt extends StdStringTaintFunction {
259
265
StdStringAt ( ) { this .getClassAndName ( [ "at" , "operator[]" ] ) instanceof StdBasicString }
260
266
261
267
override predicate hasTaintFlow ( FunctionInput input , FunctionOutput output ) {
0 commit comments